forked from emericg/OpenSubtitlesDownload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenSubtitlesDownload.py
executable file
·718 lines (596 loc) · 31.3 KB
/
OpenSubtitlesDownload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# OpenSubtitlesDownload.py / Version 3.6
# This software is designed to help you find and download subtitles for your favorite videos!
# You can browse the official website:
# https://emericg.github.io/OpenSubtitlesDownload
# You can browse the project's GitHub page:
# https://github.com/emericg/OpenSubtitlesDownload
# Learn much more about OpenSubtitlesDownload.py on its wiki:
# https://github.com/emericg/OpenSubtitlesDownload/wiki
# Copyright (c) 2017 by Emeric GRANGE <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Contributors / special thanks:
# jeroenvdw for his work on the 'subtitles automatic selection' and the 'search by filename'
# Gui13 for his work on the arguments parsing
# Tomáš Hnyk <[email protected]> for his work on the 'multiple language' feature
# Carlos Acedo <[email protected]> for his work on the original script
import os
import re
import sys
import struct
import mimetypes
import subprocess
import argparse
import time
if sys.version_info >= (3,0):
import shutil
import urllib.request
from xmlrpc.client import ServerProxy, Error
else: # python2
import urllib2
from xmlrpclib import ServerProxy, Error
# ==== Opensubtitles.org server settings =======================================
# XML-RPC server domain for opensubtitles.org:
osd_server = ServerProxy('http://api.opensubtitles.org/xml-rpc')
# You can use your opensubtitles.org account to avoid "in-subtitles" advertisment and bypass download limits
# Be careful about your password security, it will be stored right here in plain text...
# You can also change opensubtitles.org language, it will be used for error codes and stuff
osd_username = ''
osd_password = ''
osd_language = 'en'
# ==== Language settings =======================================================
# 1/ Change the search language by using any supported 3-letter (ISO 639-2) language codes:
# > Supported ISO codes: http://www.opensubtitles.org/addons/export_languages.php
# 2/ Search for subtitles in several languages at once by using multiple language codes separated by a comma:
# > Exemple: opt_languages = ['eng,fre']
opt_languages = ['eng']
# Write 2-letter language code (ex: _en) at the end of the subtitles file. 'on', 'off' or 'auto'.
# If you are regularly searching for several language at once, you sould use 'on'.
opt_language_suffix = 'auto'
opt_language_separator = '_'
# ==== Search settings =========================================================
# Subtitles search and selection mode. Can be overridden at run time with '-a' argument.
# - manual (in case of multiple results, let you choose the subtitles you want)
# - auto (automatically select the best fitted subtitles)
opt_search_mode = 'manual'
# If the search by movie hash fails, search by file name will be used as backup.
opt_search_byname = 'on'
# Search and download a subtitles even if a subtitles file already exists.
opt_search_overwrite = 'on'
# ==== GUI settings ============================================================
# Select your GUI. Can be overridden at run time with '--gui=xxx' argument.
# - auto (autodetection, fallback on CLI)
# - gnome (GNOME/GTK based environments, using 'zenity' backend)
# - kde (KDE/Qt based environments, using 'kdialog' backend)
# - cli (Command Line Interface)
opt_gui = 'auto'
# Change the subtitles selection GUI size:
opt_gui_width = 720
opt_gui_height = 320
# Various GUI options. You can set them to 'on', 'off' or 'auto'.
opt_selection_language = 'auto'
opt_selection_hi = 'auto'
opt_selection_rating = 'off'
opt_selection_count = 'off'
# Enables extra output. Can be overridden at run time with '-v' argument.
opt_verbose = 'off'
# ==== Super Print =============================================================
# priority: info, warning, error
# title: only for zenity messages
# message: full text, with tags and breaks (tag cleanup for terminal)
# verbose: is this message important?
def superPrint(priority, title, message):
"""Print messages through terminal, zenity or kdialog"""
if opt_gui == 'gnome':
if title:
subprocess.call(['zenity', '--' + priority, '--title=' + title, '--text=' + message])
else:
subprocess.call(['zenity', '--' + priority, '--text=' + message])
else:
# Clean up formating tags from the zenity messages
message = message.replace("\n\n", "\n")
message = message.replace("<i>", "")
message = message.replace("</i>", "")
message = message.replace("<b>", "")
message = message.replace("</b>", "")
message = message.replace('\\"', '"')
# Print message
if opt_gui == 'kde':
if priority == 'warning':
priority = 'sorry'
elif priority == 'info':
priority = 'msgbox'
if title:
subprocess.call(['kdialog', '--' + priority, '--title=' + title, '--text=' + message])
else:
subprocess.call(['kdialog', '--' + priority, '--text=' + message])
else: # CLI
print(">> " + message)
# ==== Check file path & type ==================================================
def checkFileValidity(path):
"""Check mimetype and/or file extension to detect valid video file"""
if os.path.isfile(path) == False:
superPrint("error", "File type error!", "This is not a file:\n<i>" + path + "</i>")
return False
fileMimeType, encoding = mimetypes.guess_type(path)
if fileMimeType == None:
fileExtension = path.rsplit('.', 1)
if fileExtension[1] not in ['avi', 'mp4', 'mov', 'mkv', 'mk3d', 'webm', \
'ts', 'mts', 'm2ts', 'ps', 'vob', 'evo', 'mpeg', 'mpg', \
'm1v', 'm2p', 'm2v', 'm4v', 'movhd', 'movx', 'qt', \
'mxf', 'ogg', 'ogm', 'ogv', 'rm', 'rmvb', 'flv', 'swf', \
'asf', 'wm', 'wmv', 'wmx', 'divx', 'x264', 'xvid']:
superPrint("error", "File type error!", "This file is not a video (unknown mimetype AND invalid file extension):\n<i>" + path + "</i>")
return False
else:
fileMimeType = fileMimeType.split('/', 1)
if fileMimeType[0] != 'video':
superPrint("error", "File type error!", "This file is not a video (unknown mimetype):\n<i>" + path + "</i>")
return False
return True
# ==== Check subtitles presence ================================================
def checkSubtitlesExists(path):
"""Check if a subtitles already exists for the current file"""
for ext in ['srt', 'sub', 'sbv', 'smi', 'ssa', 'ass', 'usf']:
subPath = path.rsplit('.', 1)[0] + '.' + ext
if os.path.isfile(subPath) == True:
superPrint("info", "Subtitles already downloaded!", "A subtitles file already exists for this file:\n<i>" + subPath + "</i>")
return True
# With language code? Only check the first language (and probably using the wrong language suffix format)
if ((opt_language_suffix == 'on') or (opt_language_suffix == 'auto')):
if len(opt_languages) == 1:
splitted_languages_list = opt_languages[0].split(',')
else:
splitted_languages_list = opt_languages
subPath = path.rsplit('.', 1)[0] + opt_language_separator + splitted_languages_list[0] + '.' + ext
if os.path.isfile(subPath) == True:
superPrint("info", "Subtitles already downloaded!", "A subtitles file already exists for this file:\n<i>" + subPath + "</i>")
return True
return False
# ==== Hashing algorithm =======================================================
# Info: http://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes
# This particular implementation is coming from SubDownloader: http://subdownloader.net/
def hashFile(path):
"""Produce a hash for a video file: size + 64bit chksum of the first and
last 64k (even if they overlap because the file is smaller than 128k)"""
try:
longlongformat = 'Q' # unsigned long long little endian
bytesize = struct.calcsize(longlongformat)
format = "<%d%s" % (65536//bytesize, longlongformat)
f = open(path, "rb")
filesize = os.fstat(f.fileno()).st_size
hash = filesize
if filesize < 65536 * 2:
superPrint("error", "File size error!", "File size error while generating hash for this file:\n<i>" + path + "</i>")
return "SizeError"
buffer = f.read(65536)
longlongs = struct.unpack(format, buffer)
hash += sum(longlongs)
f.seek(-65536, os.SEEK_END) # size is always > 131072
buffer = f.read(65536)
longlongs = struct.unpack(format, buffer)
hash += sum(longlongs)
hash &= 0xFFFFFFFFFFFFFFFF
f.close()
returnedhash = "%016x" % hash
return returnedhash
except IOError:
superPrint("error", "I/O error!", "Input/Output error while generating hash for this file:\n<i>" + path + "</i>")
return "IOError"
# ==== Gnome selection window ==================================================
def selectionGnome(subtitlesList):
"""Gnome subtitles selection window using zenity"""
searchMode = 'moviehash'
subtitlesSelected = ''
subtitlesItems = ''
columnLn = ''
columnHi = ''
columnRate = ''
columnCount = ''
# Generate selection window content
for item in subtitlesList['data']:
if item['MatchedBy'] != 'moviehash':
searchMode = item['MatchedBy']
subtitlesItems += '"' + item['SubFileName'] + '" '
if opt_selection_hi == 'on':
columnHi = '--column="HI" '
if item['SubHearingImpaired'] == '1':
subtitlesItems += '"✔" '
else:
subtitlesItems += '"" '
if opt_selection_language == 'on':
columnLn = '--column="Language" '
subtitlesItems += '"' + item['LanguageName'] + '" '
if opt_selection_rating == 'on':
columnRate = '--column="Rating" '
subtitlesItems += '"' + item['SubRating'] + '" '
if opt_selection_count == 'on':
columnCount = '--column="Downloads" '
subtitlesItems += '"' + item['SubDownloadsCnt'] + '" '
# Spawn zenity "list" dialog
if searchMode == 'moviehash':
process_subtitlesSelection = subprocess.Popen('zenity --width=' + str(opt_gui_width) + ' --height=' + str(opt_gui_height) + \
' --list --title="Synchronized subtitles for: ' + videoTitle + '"' + \
' --text="<b>Title:</b> ' + videoTitle + '\n<b>Filename:</b> ' + videoFileName + '"' + \
' --column="Available subtitles (synchronized)" ' + columnHi + columnLn + columnRate + columnCount + subtitlesItems,
shell=True, stdout=subprocess.PIPE)
else:
process_subtitlesSelection = subprocess.Popen('zenity --width=' + str(opt_gui_width) + ' --height=' + str(opt_gui_height) + \
' --list --title="Subtitles found!"' + \
' --text="<b>Filename:</b> ' + videoFileName + '\n<b>>> These results comes from search by file name (not using movie hash) and may be unreliable...</b>"' + \
' --column="Available subtitles" ' + columnHi + columnLn + columnRate + columnCount + subtitlesItems,
shell=True, stdout=subprocess.PIPE)
# Get back the result
result_subtitlesSelection = process_subtitlesSelection.communicate()
# The results contain a subtitles?
if result_subtitlesSelection[0]:
if sys.version_info >= (3,0):
subtitlesSelected = str(result_subtitlesSelection[0], 'utf-8').strip("\n")
else: # python2
subtitlesSelected = str(result_subtitlesSelection[0]).strip("\n")
# Hack against recent zenity version?
if len(subtitlesSelected.split("|")) > 1:
if subtitlesSelected.split("|")[0] == subtitlesSelected.split("|")[1]:
subtitlesSelected = subtitlesSelected.split("|")[0]
else:
if process_subtitlesSelection.returncode == 0:
subtitlesSelected = subtitlesList['data'][0]['SubFileName']
# Return the result
return subtitlesSelected
# ==== KDE selection window ====================================================
def selectionKde(subtitlesList):
"""KDE subtitles selection window using kdialog"""
return selectionAuto(subtitlesList)
# ==== CLI selection mode ======================================================
def selectionCLI(subtitlesList):
"""Command Line Interface, subtitles selection inside your current terminal"""
subtitlesIndex = 0
subtitlesItem = ''
# Print video infos
print("\n>> Title: " + videoTitle)
print(">> Filename: " + videoFileName)
# Print subtitles list on the terminal
print(">> Available subtitles:")
for item in subtitlesList['data']:
subtitlesIndex += 1
subtitlesItem = '"' + item['SubFileName'] + '" '
if opt_selection_hi == 'on':
if item['SubHearingImpaired'] == '1':
subtitlesItem += '> "HI" '
if opt_selection_language == 'on':
subtitlesItem += '> "LanguageName: ' + item['LanguageName'] + '" '
if opt_selection_rating == 'on':
subtitlesItem += '> "SubRating: ' + item['SubRating'] + '" '
if opt_selection_count == 'on':
subtitlesItem += '> "SubDownloadsCnt: ' + item['SubDownloadsCnt'] + '" '
print("\033[93m[" + str(subtitlesIndex) + "]\033[0m " + subtitlesItem)
# Ask user selection
print("\033[91m[0]\033[0m Cancel search")
sub_selection = -1
while( sub_selection < 0 or sub_selection > subtitlesIndex ):
try:
sub_selection = int(input(">> Enter your choice (0-" + str(subtitlesIndex) + "): "))
except:
sub_selection = -1
# Return the result
if sub_selection == 0:
print("Cancelling search...")
return
else:
return subtitlesList['data'][sub_selection-1]['SubFileName']
# ==== Automatic selection mode ================================================
def selectionAuto(subtitlesList):
"""Automatic subtitles selection using filename match"""
if len(opt_languages) == 1:
splitted_languages_list = list(reversed(opt_languages[0].split(',')))
else:
splitted_languages_list = opt_languages
videoFileParts = videoFileName.replace('-','.').replace(' ','.').replace('_','.').lower().split('.')
maxScore = -1
for subtitle in subtitlesList['data']:
score = 0
# points to respect languages priority
score += splitted_languages_list.index(subtitle['SubLanguageID']) * 100
# extra point if the sub is found by hash
if subtitle['MatchedBy'] == 'moviehash':
score += 1
# points for filename mach
subFileParts = subtitle['SubFileName'].replace('-','.').replace(' ','.').replace('_','.').lower().split('.');
for subPart in subFileParts:
for filePart in videoFileParts:
if subPart == filePart:
score += 1
if score > maxScore:
maxScore = score
subtitlesSelected = subtitle['SubFileName']
return subtitlesSelected
# ==== Check dependencies ======================================================
def dependencyChecker():
"""Check the availability of tools used as dependencies"""
if sys.version_info >= (3,3):
for tool in ['gunzip', 'wget']:
path = shutil.which(tool)
if path is None:
superPrint("error", "Missing dependency!", "The <b>'" + tool + "'</b> tool is not available, please install it!")
return False
return True
# ==== Main program (execution starts here) ====================================
# ==============================================================================
# ==== Argument parsing
# Get OpenSubtitlesDownload.py script path
execPath = str(sys.argv[0])
# Setup parser
parser = argparse.ArgumentParser(prog='OpenSubtitlesDownload.py',
description='This software is designed to help you find and download subtitles for your favorite videos!',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-g', '--gui', help="Select the GUI you want from: auto, kde, gnome, cli (default: auto)")
parser.add_argument('-a', '--auto', help="Automatically choose and download best fitted subtitles from the search results (default: disabled)", action='store_true')
parser.add_argument('-v', '--verbose', help="Enables verbose output (default: disabled)", action='store_true')
parser.add_argument('-l', '--lang', help="Specify the language in which the subtitles should be downloaded (default: eng).\nSyntax:\n-l eng,fre : search in both language\n-l eng -l fre : download both language", nargs='?', action='append')
parser.add_argument('filePathListArg', help="The video file(s) for which subtitles should be searched and downloaded", nargs='+')
# Only use ArgumentParser if we have arguments...
if len(sys.argv) > 1:
# Parsing
result = parser.parse_args()
# Handle results
if result.gui:
opt_gui = result.gui
if result.auto:
opt_search_mode = 'auto'
if result.verbose:
opt_verbose = 'on'
if result.lang:
if opt_languages != result.lang:
opt_languages = result.lang
opt_selection_language = 'on'
if opt_language_suffix != 'off':
opt_language_suffix = 'on'
# ==== GUI auto detection
if opt_gui == 'auto':
# Note: "ps cax" only output the first 15 characters of the executable's names
ps = str(subprocess.Popen(['ps', 'cax'], stdout=subprocess.PIPE).communicate()[0]).split('\n')
for line in ps:
if ('gnome-session' in line) or ('cinnamon-sessio' in line) or ('mate-session' in line) or ('xfce4-session' in line):
opt_gui = 'gnome'
break
elif ('ksmserver' in line):
opt_gui = 'kde'
break
# Fallback
if opt_gui not in ['gnome', 'kde', 'cli']:
opt_gui = 'cli'
opt_search_mode = 'auto'
print("Unknown GUI, falling back to an automatic CLI mode")
# ==== Check for the necessary tools (must be done after GUI auto detection)
if dependencyChecker() == False:
sys.exit(1)
# ==== Get valid video paths
videoPathList = []
if 'result' in locals():
# Go through the paths taken from arguments, and extract only valid video paths
for i in result.filePathListArg:
filePath = os.path.abspath(i)
if checkFileValidity(filePath):
videoPathList.append(filePath)
else:
# No filePathListArg from the arg parser? Try selected file(s) from nautilus environment variables:
# $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS (only for local storage)
# $NAUTILUS_SCRIPT_SELECTED_URIS
if opt_gui == 'gnome':
# Try to get file(s) provided by nautilus
filePathListEnv = os.environ.get('NAUTILUS_SCRIPT_SELECTED_URIS')
if filePathListEnv != None:
# Check file(s) type and validity
for filePath in filePathListEnv.splitlines():
# Work a little bit of magic (Make sure we have a clean and absolute path, even from an URI)
filePath = os.path.abspath(os.path.basename(filePath))
if sys.version_info >= (3,0):
filePath = urllib.request.url2pathname(filePath)
else: # python2
filePath = urllib2.url2pathname(filePath)
if checkFileValidity(filePath):
videoPathList.append(filePath)
# ==== Instances dispatcher
# If videoPathList is empty, abort!
if len(videoPathList) == 0:
parser.print_help()
sys.exit(1)
# Check if the subtitles exists videoPathList
if opt_search_overwrite == 'off':
for videoPathDispatch in videoPathList:
if checkSubtitlesExists(videoPathDispatch) == True:
videoPathList.remove(videoPathDispatch)
# If videoPathList is empty, exit!
if len(videoPathList) == 0:
sys.exit(0)
# The first video file will be processed by this instance
videoPath = videoPathList[0]
videoPathList.pop(0)
# The remaining file(s) are dispatched to new instance(s) of this script
for videoPathDispatch in videoPathList:
# Handle current options
command = execPath + " -g " + opt_gui
if opt_search_mode == 'auto':
command += " -a "
if opt_verbose == 'on':
command += " -v "
if not (len(opt_languages) == 1 and opt_languages[0] == 'eng'):
for resultlangs in opt_languages:
command += " -l " + resultlangs
# Split command string
command_splitted = command.split()
# The videoPath filename can contain spaces, but we do not want to split that, so add it right after the split
command_splitted.append(videoPathDispatch)
if opt_gui == 'cli' and opt_search_mode == 'manual':
# Synchronous call
process_videoDispatched = subprocess.call(command_splitted)
else:
# Asynchronous call
process_videoDispatched = subprocess.Popen(command_splitted)
# Do not spawn too many instances at the same time
time.sleep(0.33)
# ==== Search and download subtitles
try:
try:
# Connection to opensubtitles.org server
session = osd_server.LogIn(osd_username, osd_password, osd_language, 'opensubtitles-download 3.6')
except Exception:
# Retry once, it could be a momentary overloaded server?
time.sleep(3)
try:
# Connection to opensubtitles.org server
session = osd_server.LogIn(osd_username, osd_password, osd_language, 'opensubtitles-download 3.6')
except Exception:
# Failed connection attempts?
superPrint("error", "Connection error!", "Unable to reach opensubtitles.org servers!\n\nPlease check:\n- Your Internet connection status\n- www.opensubtitles.org availability\n- Your downloads limit (200 subtitles per 24h)\nThe subtitles search and download service is powered by opensubtitles.org. Be sure to donate if you appreciate the service provided!")
sys.exit(1)
# Connection refused?
if session['status'] != '200 OK':
superPrint("error", "Connection error!", "Opensubtitles.org servers refused the connection: " + session['status'] + ".\n\nPlease check:\n- Your Internet connection status\n- www.opensubtitles.org availability\n- Your 200 downloads per 24h limit")
sys.exit(1)
searchLanguage = 0
searchLanguageResult = 0
videoTitle = 'Unknown video title'
videoHash = hashFile(videoPath)
videoSize = os.path.getsize(videoPath)
videoFileName = os.path.basename(videoPath)
# Count languages marked for this search
for SubLanguageID in opt_languages:
searchLanguage += len(SubLanguageID.split(','))
# Search for available subtitles using file hash and size
for SubLanguageID in opt_languages:
searchList = []
searchList.append({'sublanguageid':SubLanguageID, 'moviehash':videoHash, 'moviebytesize':str(videoSize)})
try:
subtitlesList = osd_server.SearchSubtitles(session['token'], searchList)
except Exception:
# Retry once, we are already connected, the server is probably momentary overloaded
time.sleep(3)
try:
subtitlesList = osd_server.SearchSubtitles(session['token'], searchList)
except Exception:
superPrint("error", "Search error!", "Unable to reach opensubtitles.org servers!\n<b>Search error</b>")
# No results using search by hash? Retry with filename
if (not subtitlesList['data']) and (opt_search_byname == 'on'):
searchList = []
searchList.append({'sublanguageid':SubLanguageID, 'query':videoFileName})
try:
subtitlesList = osd_server.SearchSubtitles(session['token'], searchList)
except Exception:
# Retry once, we are already connected, the server is probably momentary overloaded
time.sleep(3)
try:
subtitlesList = osd_server.SearchSubtitles(session['token'], searchList)
except Exception:
superPrint("error", "Search error!", "Unable to reach opensubtitles.org servers!\n<b>Search error</b>")
# Parse the results of the XML-RPC query
if subtitlesList['data']:
# Mark search as successful
searchLanguageResult += 1
subtitlesSelected = ''
# If there is only one subtitles, auto-select it (only when matched by file hash, except in CLI mode)
if (len(subtitlesList['data']) == 1) and (opt_gui == 'cli' or subtitlesList['data'][0]['MatchedBy'] == 'moviehash'):
subtitlesSelected = subtitlesList['data'][0]['SubFileName']
# Get video title
videoTitle = subtitlesList['data'][0]['MovieName']
# Title and filename may need string sanitizing to avoid zenity/kdialog handling errors
if opt_gui != 'cli':
videoTitle = videoTitle.replace('"', '\\"')
videoTitle = videoTitle.replace("'", "\'")
videoTitle = videoTitle.replace('`', '\`')
videoTitle = videoTitle.replace("&", "&")
videoFileName = videoFileName.replace('"', '\\"')
videoFileName = videoFileName.replace("'", "\'")
videoFileName = videoFileName.replace('`', '\`')
videoFileName = videoFileName.replace("&", "&")
# If there is more than one subtitles and opt_search_mode != 'auto',
# then let the user decide which one will be downloaded
if subtitlesSelected == '':
# Automatic subtitles selection?
if opt_search_mode == 'auto':
subtitlesSelected = selectionAuto(subtitlesList)
else:
# Go through the list of subtitles and handle 'auto' settings activation
for item in subtitlesList['data']:
if opt_selection_language == 'auto':
if searchLanguage > 1:
opt_selection_language = 'on'
if opt_selection_hi == 'auto':
if item['SubHearingImpaired'] == '1':
opt_selection_hi = 'on'
if opt_selection_rating == 'auto':
if item['SubRating'] != '0.0':
opt_selection_rating = 'on'
if opt_selection_count == 'auto':
opt_selection_count = 'on'
# Spaw selection window
if opt_gui == 'gnome':
subtitlesSelected = selectionGnome(subtitlesList)
elif opt_gui == 'kde':
subtitlesSelected = selectionKde(subtitlesList)
else: # CLI
subtitlesSelected = selectionCLI(subtitlesList)
# If a subtitles has been selected at this point, download it!
if subtitlesSelected:
subIndex = 0
subIndexTemp = 0
# Select the subtitles file to download
for item in subtitlesList['data']:
if item['SubFileName'] == subtitlesSelected:
subIndex = subIndexTemp
break
else:
subIndexTemp += 1
subLangId = opt_language_separator + subtitlesList['data'][subIndex]['ISO639']
subLangName = subtitlesList['data'][subIndex]['LanguageName']
subURL = subtitlesList['data'][subIndex]['SubDownloadLink']
subPath = videoPath.rsplit('.', 1)[0] + '.' + subtitlesList['data'][subIndex]['SubFormat']
# Write language code into the filename?
if ((opt_language_suffix == 'on') or
(opt_language_suffix == 'auto' and searchLanguageResult > 1)):
subPath = videoPath.rsplit('.', 1)[0] + subLangId + '.' + subtitlesList['data'][subIndex]['SubFormat']
# Escape non-alphanumeric characters from the subtitles path
subPath = re.escape(subPath)
# Download and unzip the selected subtitles (with progressbar)
if opt_gui == 'gnome':
process_subtitlesDownload = subprocess.call("(wget -q -O - " + subURL + " | gunzip > " + subPath + ") 2>&1" + ' | (zenity --auto-close --progress --pulsate --title="Downloading subtitles, please wait..." --text="Downloading <b>' + subtitlesList['data'][subIndex]['LanguageName'] + '</b> subtitles for <b>' + videoTitle + '</b>...")', shell=True)
elif opt_gui == 'kde':
process_subtitlesDownload = subprocess.call("(wget -q -O - " + subURL + " | gunzip > " + subPath + ") 2>&1", shell=True)
else: # CLI
print(">> Downloading '" + subtitlesList['data'][subIndex]['LanguageName'] + "' subtitles for '" + videoTitle + "'")
process_subtitlesDownload = subprocess.call("wget -nv -O - " + subURL + " | gunzip > " + subPath, shell=True)
# If an error occur, say so
if process_subtitlesDownload != 0:
superPrint("error", "Subtitling error!", "An error occurred while downloading or writing <b>" + subtitlesList['data'][subIndex]['LanguageName'] + "</b> subtitles for <b>" + videoTitle + "</b>.")
osd_server.LogOut(session['token'])
sys.exit(1)
# Print a message if no subtitles have been found, for any of the languages
if searchLanguageResult == 0:
superPrint("info", "No subtitles found for: " + videoFileName, '<b>No subtitles found</b> for this video:\n<i>' + videoFileName + '</i>')
# Disconnect from opensubtitles.org server, then exit
if session['token']: osd_server.LogOut(session['token'])
sys.exit(0)
except (OSError, IOError, RuntimeError, TypeError, NameError, KeyError):
# Do not warn about remote disconnection # bug/feature of python 3.5
if "http.client.RemoteDisconnected" in str(sys.exc_info()[0]):
sys.exit(1)
# An unknown error occur, let's apologize before exiting
superPrint("error", "Unknown error!", "OpenSubtitlesDownload encountered an <b>unknown error</b>, sorry about that...\n\n" + \
"Error: <b>" + str(sys.exc_info()[0]).replace('<', '[').replace('>', ']') + "</b>\n\n" + \
"Just to be safe, please check:\n- www.opensubtitles.org availability\n- Your downloads limit (200 subtitles per 24h)\n- Your Internet connection status\n- That are using the latest version of this software ;-)")
# Disconnect from opensubtitles.org server, then exit
if session['token']: osd_server.LogOut(session['token'])
sys.exit(1)