-
Notifications
You must be signed in to change notification settings - Fork 2
/
osdown.py
executable file
·111 lines (88 loc) · 2.97 KB
/
osdown.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
#!/usr/bin/env python
from pythonopensubtitles.opensubtitles import OpenSubtitles
from pythonopensubtitles.utils import File
import ConfigParser
import urllib
from tempfile import mktemp
import gzip
from numbers import Number
import os
config = ConfigParser.RawConfigParser()
config.read(os.path.realpath(__file__)[:-2] + 'cfg')
osmgr = OpenSubtitles()
osmgr.login(config.get('Login', 'user'), config.get('Login', 'pass'))
def find_subtitles(path, langid='all'):
''' Get a list of subtitles for a given file '''
try:
f = File(path)
hash = f.get_hash()
assert type(hash) == str
size = f.size
assert isinstance(size, Number)
data = osmgr.search_subtitles([{'sublanguageid': langid,
'moviehash': hash,
'moviebytesize': size}])
assert type(data) == list
return data
except:
return []
def download_subtitle(subtitle, file_name=None):
print 'Downloading %s...' % subtitle['SubFileName']
tempfile = mktemp()
urllib.urlretrieve(subtitle['SubDownloadLink'], tempfile)
if file_name is None:
file_name = subtitle['SubFileName']
with gzip.open(tempfile, 'rb') as gz, open(file_name, 'wb') as f:
f.write(gz.read())
os.remove(tempfile)
def choose_subtitles(subtitles):
while True:
i = 0
print 'There are several subtitles available:'
for s in subtitles:
i += 1
print '%d:\t%s' % (i, s['SubFileName'])
print 'Enter your choice:',
c = raw_input()
if not c.isdigit():
print 'Invalid input!'
continue
c = int(c)
if c < 1 or c > i:
print 'Invalid option!'
continue
return subtitles[c-1]
def usage():
import sys
print "%s <filename> [<langid>]" % sys.argv[0]
def main():
import sys
from glob import glob
if len(sys.argv) < 2:
usage()
return
langid = config.get('Language', 'langid')
rename = config.getboolean('General', 'rename')
if len(sys.argv) >= 3:
langid = sys.argv[2]
files = glob(sys.argv[1].replace('[','[[]'))
should_use_menu = False
if len(files) == 1:
should_use_menu = not config.getboolean('General', 'download_first')
else:
should_use_menu = not config.getboolean('General', 'download_first_on_multiple')
for f in files:
subtitles = find_subtitles(f, langid)
if len(subtitles) == 0:
print "No subtitles found for '%s'!" % f
continue
to_download = subtitles[0]
if len(subtitles) > 1 and should_use_menu:
to_download = choose_subtitles(subtitles)
file_name = None
if rename:
file_name = '%s%s' % (os.path.splitext(f)[0],
os.path.splitext(to_download['SubFileName'])[1])
download_subtitle(to_download, file_name)
if __name__ == '__main__':
main()