-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpdl.py
197 lines (167 loc) · 6.6 KB
/
dpdl.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
#!/usr/bin/python
import re
import sys
import os
import cookielib
import mechanize
import rarfile
from BeautifulSoup import BeautifulSoup
"""
Divxplanet Altyazi indirme aparati
"""
def getmediaUrl(mediaArgs):
if len(mediaArgs) > 1:
query = "site:divxplanet.com inurl:sub/m intitle:\"%s\" intitle:\"(%s)\"" % (mediaArgs[0], mediaArgs[1])
else:
query = "site:divxplanet.com inurl:sub/m intitle:\"%s\"" % (mediaArgs[0])
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
# br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) '
'Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.open("http://www.google.com")
# Select the search box and search for 'foo'
br.select_form('f')
br.form['q'] = query
br.submit()
page = br.response().read()
soup = BeautifulSoup(page)
linkdictionary = []
query.replace(" ", "-")
for li in soup.findAll('li', attrs={'class': 'g'}):
sLink = li.find('a')
sSpan = li.find('span', attrs={'class': 'st'})
if sLink:
linkurl = re.search(r"/url\?q=(http://divxplanet.com/sub/m/[0-9]{3,8}/.*.\.html).*", sLink["href"])
if linkurl:
linkdictionary.append(
{"text": sSpan.getText().encode('ascii', 'ignore'), "name": mediaArgs[0], "url": linkurl.group(1)})
if len(linkdictionary) == 1:
return linkdictionary[0]["url"]
else:
i = 0
for l in linkdictionary:
print i, l["url"]
print l["text"]
i += 1
j = raw_input("Hangi Dizi?")
return linkdictionary[int(j)]["url"]
def search_subtitles(title, season, episode): # standard input
# Build an adequate string according to media type
tvurl = getmediaUrl([title])
print "looking at", tvurl
divpname = re.search(r"http://divxplanet.com/sub/m/[0-9]{3,8}/(.*.)\.html", tvurl).group(1)
season = int(season)
episode = int(episode)
# Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
# br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) '
'Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
url = br.open(tvurl)
html = url.read()
soup = BeautifulSoup(html)
subtitles_list = []
# /sub/s/281212/Hannibal.html
for link in soup.findAll('a', href=re.compile("\/sub\/s\/.*.\/%s.html" % divpname)):
addr = link.get('href')
info = link.parent.parent.nextSibling.nextSibling.findAll("td", colspan="3")
if info:
tse = info[0].div.findAll("b", text="%d" % season)
tep = info[0].div.findAll("b", text="%02d" % episode)
lantext = link.parent.find("br")
lan = link.parent.parent.findAll("img", title=re.compile("^.*. (subtitle|altyazi)"))
if tse and tep and lan and lantext:
language = lan[0]["title"]
if language[0] == "e":
language = "English"
lan_short = "en"
else:
language = "Turkish"
lan_short = "tr"
subtitles_list.append({'link': addr,
'movie': title,
'description': "%s" % (info[1].contents[0]),
'filename': "%s S%02dE%02d.%s" % (title, season, episode, lan_short),
'language_flag': "flags/%s.gif" % lan_short,
'language_name': language,
'sync': False,
'rating': "0"})
br.close()
return subtitles_list
def download_subtitles(subtitles_list, pos): # standard input
dlurl = "http://divxplanet.com%s" % subtitles_list[pos]["link"]
# Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
# br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) '
'Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.open(dlurl)
br.select_form(name="dlform")
br.submit()
f = open(subtitles_list[pos]["filename"] + ".rar", "wb")
f.write(br.response().get_data())
f.close()
rf = rarfile.RarFile(subtitles_list[pos]["filename"] + ".rar")
for r in rf.infolist():
if re.search(r"(sub|srt)", r.filename):
rf.extract(r)
print "Downloaded: ", r.filename
os.remove(subtitles_list[pos]["filename"] + ".rar")
br.close()
def main(argv):
if argv:
if len(argv) == 3:
season = int(argv[1])
episode = int(argv[2])
if season > 0 and episode > 0:
sublist = search_subtitles(argv[0], season, episode)
i = 0
for sub in sublist:
print i, sub["language_name"], sub["description"]
i += 1
dlsub = raw_input("Hangi Altyazi?")
download_subtitles(sublist, int(dlsub))
else:
print "Kullanim: python dpdl.py 'Dizi Adi' sezon_no bolum_no"
print "Ornek: python dpdl.py 'game of thrones' 3 3"
else:
print "Kullanim: python dpdl.py 'Dizi Adi' sezon_no bolum_no"
print "Ornek: python dpdl.py 'game of thrones' 3 3"
if __name__ == "__main__":
main(sys.argv[1:])