-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget_subtitles.py
More file actions
executable file
·40 lines (36 loc) · 1.15 KB
/
get_subtitles.py
File metadata and controls
executable file
·40 lines (36 loc) · 1.15 KB
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
#! /usr/bin/python
import hashlib
import os
import requests
import sys
def get_hash(name):
readsize = 64 * 1024
with open(name, 'rb') as f:
size = os.path.getsize(name)
data = f.read(readsize)
f.seek(-readsize, os.SEEK_END)
data += f.read(readsize)
return hashlib.md5(data).hexdigest()
def main():
if len(sys.argv) != 2:
print "Usage: %s <path_to_movie_file>" % sys.argv[0]
sys.exit(0)
headers = {'User-Agent' : "SubDB/1.0 (SubtitleScript/1.0; http://github.com/vickyg3/scripts)"}
filename = sys.argv[1].strip()
filehash = get_hash(filename)
# search
search_url = "http://api.thesubdb.com/?action=search&hash=%(filehash)s"
data = requests.get(search_url % locals(), headers = headers).text
if "en" not in data.split(","):
print "nothing found :("
sys.exit(0)
# download
srt_filename = "%s.srt" % os.path.splitext(filename)[0]
download_url = "http://api.thesubdb.com/?action=download&hash=%(filehash)s&language=en"
data = requests.get(download_url % locals(), headers = headers).text
f = open(srt_filename, 'w')
f.write(data.encode('utf-8'))
f.close()
print "done :)"
if __name__ == "__main__":
main()