forked from Roger/pycavane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pycavane.py
181 lines (140 loc) · 5.03 KB
/
pycavane.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
import re
import sys
import time
import urllib
import urllib2
headers = {'User-Agent': 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; '\
'rv:1.9.2.10) Gecko/20100928 Firefox/3.6.1'}
host = 'http://www.cuevana.tv'
series_url = host+'/series/'
seassons_url = host+'/list_search_id.php?serie=%s'
episodes_url = host+'/list_search_id.php?temporada=%s'
episode_url = host+"/series/%s/%s/%s/"
player_url = host+'/player/source?id=%s&subs=,ES,EN&onstart=yes&tipo=s&sub_pre=ES'
source_get = host+'/player/source_get'
sub_url = host+'/files/s/sub/%s_%s.srt'
series_re = re.compile('serieslist.push\(\{id:([0-9]*),nombre:"(.*?)"\}\);')
seasson_re = re.compile('<li onclick=\'listSeries\(2,"([0-9]*)"\)\'>(.*?)</li>')
episode_re = re.compile('<li onclick=\'listSeries\(3,"([0-9]*)"\)\'>'\
'<span class=\'nume\'>([0-9]*)</span>(.*?)</li>')
mega_id_re = re.compile('goSource\((.*?)\',\'megaupload\'\)')
captcha_re = re.compile('<img src="(http\:\/\/.*megaupload\.com\/'\
'gencap.php\?.*\.gif)"')
fname_re = re.compile('font-size:22px; font-weight:bold;">(.*?)</font><br>')
megalink_re = re.compile('<a href="(.*?)" class="down_butt1"onclick')
source_re = re.compile("goSource\('([a-zA-Z0-9]*?)','([a-zA-Z]*?)'\)")
def retry(callback):
''' Retry decorator '''
def deco(url, data=None, filename=None, handle=False):
tried = 0
while tried < 3:
try:
return callback(url, data, filename, handle)
except Exception, error:
tried += 1
time.sleep(1)
raise Exception, 'Can\'t download "%s" error: "%s"' % (url, error)
return deco
@retry
def url_open(url, data=None, filename=None, handle=False):
if data:
request = urllib2.Request(url, urllib.urlencode(data), headers)
else:
request = urllib2.Request(url, headers=headers)
rc = urllib2.urlopen(request)
if handle:
return rc
if filename:
local = open(filename, 'wb')
text = ''
size = 0
lastsize = 0
while True:
buffer = rc.read(1024)
if buffer == '':
break
if filename:
size += len(buffer)
local.write(buffer)
if (size - 1024*4) > lastsize:
sys.stdout.write(str(size/1024) + "kb downloaded...\r")
sys.stdout.flush()
lastsize = size
else:
text += buffer
if filename:
local.close()
print
return text
def get_series(name=None):
series = series_re.findall(url_open(series_url))
if name:
series = [s for s in series if name.lower() in s[1].lower()]
return series
def get_seassons(serie):
seassons = seasson_re.findall(url_open(seassons_url % serie[0]))
return seassons
def get_episodes(seasson):
episodes = episode_re.findall(url_open(episodes_url % seasson[0]))
return episodes
def get_megalink(link):
megalink = megalink_re.findall(url_open(link))
if megalink:
time.sleep(45)
return megalink[0]
return None
def get_direct_links(episode, host=None):
data = url_open(player_url % episode[0])
hosts = []
for key, value in source_re.findall(data):
if not host or value == host:
url = url_open(source_get, data=[('key', key), ('host', value),
('vars', '&id=9555&subs=,ES,EN&tipo=s&sub_pre=ES')])
# before http are ugly chars
url = url[url.find('http:'):].split('&id')[0]
if host:
return (value, url)
hosts.append((value, url))
return hosts
def get_subtitle(episode, lang='ES', filename=None):
if filename:
filename += '.srt'
return url_open(sub_url % (episode[0], lang), filename=filename)
def main():
lang = 'ES'
if len(sys.argv) > 2:
print 'Usage %s ["Name Of Serie"]' % sys.argv[0]
exit()
name = None
if len(sys.argv) == 2:
name = sys.argv[1]
series = get_series(name)
if not series:
print 'Serie not found "%s"' % name
exit()
for key, value in enumerate(series, 1):
print key, value[1]
serie_id = int(raw_input('>> '))
serie = series[serie_id-1]
seassons = get_seassons(serie)
for key, value in enumerate(seassons, 1):
print key, value[1]
seasson_id = int(raw_input('>> '))
seasson = seassons[seasson_id-1]
episodes = get_episodes(seasson)
print 0, 'All'
for key, value in enumerate(episodes, 1):
print key, value[2]
episode_id = int(raw_input('>> '))
if episode_id:
episodes = [episodes[episode_id-1]]
for episode in episodes:
print '"%s" - "%s" - "%s"' % (serie[1], seasson[1], episode[2])
print 'Subtitle', sub_url % (episode[0], lang)
print
for host, url in get_direct_links(episode):
print '%s: %s' % (host, url)
# Download Subtitle
#url_open(sub_url % (episode[0], lang), filename=filename+'.srt')
if __name__ == '__main__':
main()