forked from DiGMi/OsDown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osdown.py
executable file
·126 lines (89 loc) · 3.16 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
########## Imports
from pythonopensubtitles.opensubtitles import OpenSubtitles
from pythonopensubtitles.utils import File
import ConfigParser
from numbers import Number
import gzip
import rarfile
import zipfile
from tempfile import mktemp
import os
from os import path
import urllib
########## Constants
RAR_EXT = [ ".rar" ]
ZIP_EXT = [ ".zip" ]
COMPRESSED_EXTS = RAR_EXT + ZIP_EXT
VID_EXTS = [ ".avi", ".h264", ".hdmov", ".mkv", ".mov", ".mp4", ".ogv", ".xvid" ]
########## Configuration and setup
config = ConfigParser.RawConfigParser()
config.read(os.path.realpath(__file__)[:-2] + 'cfg')
osmgr = OpenSubtitles()
osmgr.login(config.get('Login', 'user'), config.get('Login', 'pass'))
########## Main functions
def download_subtitle(subtitle):
print 'Downloading %s...' % subtitle['SubFileName']
tempfile = mktemp()
urllib.urlretrieve(subtitle['SubDownloadLink'], tempfile)
with gzip.open(tempfile, 'rb') as gz, open(subtitle['SubFileName'], 'wb') as f:
f.write(gz.read())
os.remove(tempfile)
def extract_videos(compressed_file):
# Select the right API to extract the files out of the archive.
klass = extract_dict[file_ext(compressed_file)]
archive = klass(compressed_file)
videos = [video for video in archive.namelist() if file_ext(video) in VID_EXTS]
print "Extracting videos (using %s) %s..." % (str(klass), ", ".join(videos))
map(archive.extract, videos, ["."] * len(videos))
return videos
def find_subtitles(path, langid='all'):
''' Get a list of subtitles for a given file '''
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
def main():
import sys
if len(sys.argv) < 2:
usage()
return
filename = sys.argv[1]
langid = config.get('Language', 'langid')
if len(sys.argv) >= 3:
langid = sys.argv[2]
if file_ext(filename) in COMPRESSED_EXTS:
videos = extract_videos(filename)
map(process_subtitle, videos, [langid] * len(videos))
else:
process_subtitle(filename, langid)
def process_subtitle(filename, langid):
subtitle = find_subtitles(filename, langid)[0]
download_subtitle(subtitle)
rename_subtitle(filename, subtitle['SubFileName'])
def rename_subtitle(vid_filename, sub_filename):
print "Renaming subtitle %s..." % sub_filename
vid_name = file_name(vid_filename)
sub_ext = file_ext(sub_filename)
name = vid_name + sub_ext
os.rename(sub_filename, name)
########## General Utilities
# Compressed files extraction libraries
extract_class = [ rarfile.RarFile, zipfile.ZipFile ]
extract_dict = dict(zip(COMPRESSED_EXTS, extract_class))
def file_ext(filename):
return path.splitext(filename)[1]
def file_name(filename):
return path.splitext(filename)[0]
# Command-line Interface
def usage():
import sys
print "%s <filename> [<langid>]" % sys.argv[0]
if __name__ == '__main__':
main()