-
Notifications
You must be signed in to change notification settings - Fork 0
/
horus.py
executable file
·205 lines (162 loc) · 5.88 KB
/
horus.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
#!/usr/bin/python
import hashlib
import os
import signal
import sys
import urllib2
import mimetypes
import pysrt
from optparse import OptionParser
USER_AGENT = "SubDB/1.0 (horus/0.1; http://www.github.com/romeupalos/horus)"
API_URL = "http://api.thesubdb.com/"
ADS_WORDS = ['opensubtitles', 'legendei.com', 'sfdownload.com']
# this hash function receives the name of the file and returns the hash code
def get_hash(name):
read_size = 64 * 1024
with open(name, 'rb') as f:
data = f.read(read_size)
f.seek(-read_size, os.SEEK_END)
data += f.read(read_size)
return hashlib.md5(data).hexdigest()
def signal_handler(signal, frame):
exit(0)
def check_file_size(target_file):
try:
file_size = os.path.getsize(target_file)
except OSError as e:
print "Error: (" + repr(e.errno) + ") ", e
return False
if file_size < (128 * 1024):
return False
else:
return True
def remove_ads_and_save(sub_contents, path):
sub_contents = sub_contents.decode('iso-8859-15')
srt_sub = pysrt.from_string(sub_contents)
index = 0
while index < len(srt_sub):
srt_sub[index].index = index + 1
sub_item = srt_sub[index]
if True in [True for word in ADS_WORDS if word in sub_item.text.lower()]:
del srt_sub[index]
else:
index += 1
srt_sub.save(path, encoding='utf-8')
def download_sub(videofile, overwrite, language, keep_ads):
if not check_file_size(videofile):
print "File is too small"
return 1
hash = get_hash(videofile)
print "hash is: " + hash
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', USER_AGENT)]
try:
conn = opener.open(API_URL + "?action=search&hash=" + hash)
except urllib2.HTTPError as e:
print repr(e.code) + ": " + e.reason
return 2
response = conn.read()
print "available languages: " + response
# language as None means to download all languages available
if language is None:
for available_language in response.split(','):
download_sub_for_language(opener, videofile, hash, overwrite, available_language, keep_ads)
# Download if for desired language
else :
if response.find(language) == -1:
print "language is not available"
return 3
download_sub_for_language(opener, videofile, hash, overwrite, language, keep_ads)
def download_sub_for_language(opener, videofile, hash, overwrite, language, keep_ads):
try:
conn = opener.open(API_URL + "?action=download&hash=" + hash + "&language=" + language)
except urllib2.HTTPError as e:
print repr(e.code) + ": " + e.reason
return 4
sub_filename = os.path.splitext(videofile)[0] + "." + language + ".srt"
# Check if the file exists
if os.path.isfile(sub_filename):
if overwrite is True:
resp = 'y'
elif overwrite is False:
resp = 'n'
else:
sys.stdout.write("Error: subtitle already exists. Overwrite? (y/N): ")
resp = sys.stdin.readline()
if resp[0] is not 'y':
print "Skipping %s: File exists." % sub_filename
return 5
if keep_ads:
with open(sub_filename, 'w') as sub_file:
sub_file.write(conn.read())
else:
remove_ads_and_save(conn.read(), sub_filename)
print sub_filename + " downloaded"
return 0
def isVideo(path):
mime_type = mimetypes.guess_type(path)[0]
if mime_type is not None:
return mime_type.split('/')[0] == 'video'
return False
def download_sub_recursive(path):
if os.path.isdir(path):
for f in os.listdir(path):
download_sub_recursive(os.path.join(path, f))
else:
fileext = os.path.splitext(path)[1]
if options.ignoreMimeType is True or isVideo(path):
print "Downloading subs for " + os.path.basename(path)
# None language tells the script to download All Languages
language = None if options.all_languages else options.language
overwrite = True if options.yesToAll else False if options.noToAll else None
download_sub(path, overwrite, language, options.keep_ads)
else:
print "Skipping " + path
# Capture Ctrl + C (SIGINT)
signal.signal(signal.SIGINT, signal_handler)
parser = OptionParser()
parser.add_option("-y",
"--yes-to-all",
action="store_true",
dest="yesToAll",
default=False,
help="Overwrite without asking.")
parser.add_option("-n",
"--no-to-all",
action="store_true",
dest="noToAll",
default=False,
help="Don't overwrite any file")
parser.add_option("-i",
"--ignore-mime",
action="store_true",
dest="ignoreMimeType",
default=False,
help="Download subtitles for non video files")
parser.add_option("-l",
"--language",
metavar="LANGUAGE",
dest="language",
default='pt',
help="language to download")
parser.add_option("-a",
"--all-languages",
metavar="ALL_LANGUAGES",
dest="all_languages",
default=False,
action="store_true",
help="download all languages")
parser.add_option("-k",
"--keep-ads",
metavar="KEEP_ADS",
dest="keep_ads",
default=False,
action="store_true",
help="don't remove ads from subtitles")
(options, args) = parser.parse_args()
if len(args) < 1:
print "Missing files."
exit(1)
for path in args:
download_sub_recursive(path)
print "All done;"