-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsubtitle-downloader.py
143 lines (136 loc) · 4.1 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
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
""" Code Written by ~KK~
To Automate the boring process of renaming files for your TV Shows library
Useful for organising bulk media files or frequent updation of
your XBMC library (Like Kodi, Plex and OSMC)
"""
def downloadsub(keyword,location,chance):
import os
from bs4 import BeautifulSoup
import googlesearch as gs
import requests
LANGUAGE_CODES = {
"Arabic": "ar",
"Chinese": "zh",
"Danish": "da",
"Dutch": "nl",
"English": "en",
"Esperanto": "eo",
"Finnish": "fi",
"French": "fr",
"German": "de",
"Greek": "el",
"Hebrew": "he",
"Hindi": "hi",
"Irish": "ga",
"Italian": "it",
"Japanese": "ja",
"Korean": "ko",
"Mongolian": "mn",
"Norwegian": "no",
"Persian": "fa",
"Polish": "pl",
"Portuguese": "pt",
"Russian": "ru",
"Spanish": "es",
"Swedish": "sv",
"Thai": "th",
"Urdu": "ur",
"Vietnamese": "vi",
"Welsh": "cy",
"Yiddish": "yi",
"Zulu": "zu"
}
q = keyword
temp = q
x = location
q = q + " English Subtitle SubScene"
tdisp = "Googling - "+q+" "
print(tdisp)
possible = []
for j in gs.search(q, tld="com", num=10, stop=1, pause=3):
sp = "https://subscene.com/subtitles/"
n = len(sp)
if j[:n] == sp:
possible.append(j)
fin = ""
eng_flag = 0
for p in possible:
c = 0
ind_count = 0
for i in p:
if i == '/':
c += 1
if c==5:
eng = "english/"
print("Being Checked - ",p[(ind_count+1):(ind_count+9)]," ")
if(p[(ind_count+1):(ind_count+9)] == eng):
eng_flag = 1
ind_count += 1
if (c == 6) and (eng_flag == 1):
fin = p
break
url = fin
tdisp = "Found URL for this movie as - " + url + " "
print(tdisp)
if not bool(url):
tdisp = "Couldn't find movie from the filename - " + temp + "\n\n"
print(tdisp)
nnn = len(temp)
nnn = (3 * nnn) / 4
nnn = int(nnn)
if nnn >= int(chance / 2):
tdisp = "Retrying with 75% of file name"
print(tdisp)
q = temp[:nnn]
downloadsub(q, x, chance)
return
else:
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, 'html.parser')
ssample = "/subtitles/english-text/"
ssn = len(ssample)
final = ""
n = ssn
for link in soup.find_all('a'):
s = link.get('href')
if s[:n] == ssample:
final = "https://subscene.com" + s
urll = final
if bool(urll):
tdisp = "Downloading from URL - " + urll + " "
print(tdisp)
r = requests.get(urll, allow_redirects=True)
location = x + "\\" + temp + ".zip"
tdisp = "Placing at location - " + location + "\n\n"
print(tdisp)
open(location, 'wb').write(r.content)
else:
tdisp = "Couldn't find movie from this filename - " + temp + "\n\n"
print(tdisp)
nnn=len(temp)
nnn=(3*nnn)/4
nnn = int(nnn)
if nnn>=int(chance/2):
tdisp = "Retrying...(With 75% of file name)\n"
print(tdisp)
q = temp[:nnn]
downloadsub(q,x,chance)
return(str(location))
def unZip(path_to_zip_file,directory_to_extract_to):
import zipfile
zip_ref = zipfile.ZipFile(path_to_zip_file, 'r')
zip_ref.extractall(directory_to_extract_to)
zip_ref.close()
if __name__ == '__main__':
import os
try:os.mkdir(".cache")
except:pass
path_ = os.getcwd()+"\\.cache"
files = os.listdir(path_)
for file in files:
if (file.endswith(".mp4") or file.endswith(".mkv")):
downloadsub(file[:-4],path_,5)
elif (file.endswith(".zip")):
unZip(file,path_)
os.remove(file)