forked from HarshCasper/Rotten-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubtitle_downloader.py
42 lines (35 loc) · 1.07 KB
/
subtitle_downloader.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
import urllib
import os
import hashlib
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():
userAgent = 'SubDB1.0 (seema1711/0.1; https://github.com/seema1711/subtitle-downloader)'
moviePath = sys.argv[1]
movieName = os.path.join(os.getcwd(), moviePath)
language = 'en'
action = 'download'
baseURL = 'http://api.thesubdb.com/?'
hashed = get_hash(movieName)
content = {
'action': action,
'hash': hashed,
'language': language,
}
url = baseURL + urllib.urlencode(content)
request = urllib.Request(url)
request.add_header('User-Agent', userAgent)
response = urllib.urlopen(request)
subtitles = response.read()
index = movieName.rfind('.')
fileName = movieName[0:index] + '.srt'
with open(fileName, 'w') as f:
f.write(subtitles)
print("Subtitle Downloaded!!")