-
Notifications
You must be signed in to change notification settings - Fork 2
/
pycavane.py
372 lines (291 loc) · 12 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python
# coding: utf-8
"""
Pycavane
A library to scrap the website www.cuevana.tv
Author: Roger Duran <[email protected]>
Contributor: j0hn <[email protected]>
"""
import re
from util import UrlOpen
from memo import Memoized
HOST = 'http://www.cuevana.tv'
MOVIES_URL = HOST + '/peliculas/lista/letra=%s&page=%s'
SHOWS_URL = HOST + '/series/'
SHOW_INFO_URL = HOST + '/list_search_info.php?episodio=%s'
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_MOVIE_URL = HOST + '/player/source?id=%s'
PLAYER_SEASON_URL = PLAYER_MOVIE_URL + '&tipo=s'
SOURCE_GET = HOST + '/player/source_get'
SUB_URL_MOVIE = HOST + '/files/sub/%s_%s.srt'
SUB_URL_SHOW = HOST + '/files/s/sub/%s_%s.srt'
SUB_URL_SHOW = HOST + '/download_sub?file=s/sub/%s_%s.srt'
SUB_URL_MOVIE = HOST + '/download_sub?file=sub/%s_%s.srt'
SEARCH_URL = HOST + '/buscar/?q=%s&cat=titulo'
SHOWS_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\'>(.*?)</span>\s?(.*?)</li>')
MOVIES_RE = re.compile(r'<tr class=\'row[1-2]\'>.*?<div class=\'tit\'><a '\
'href=\'/peliculas/([0-9]*?)/.*?/\'>(.*?)</a></div>.*?<div '\
'class=\'font11\'>(.*?)<div class=\'reparto\'>', re.DOTALL)
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>')
SOURCE_RE = re.compile("goSource\('([a-zA-Z0-9]*?)','([a-zA-Z]*?)'\)")
SEARCH_RE = re.compile('<div class=\'tit\'><a href=\'(.*?)\'>' \
'(.*?) \(.*\)</a></div>')
MAYBE_MEANT = re.compile("Quizás quiso decir: <a href='.*?'>(.*?)</a>")
SHOW_INFO_IMAGE_RE = re.compile('<img src="(.*?)" border="0" />')
SHOW_INFO_DESCRIPTION_RE = re.compile('<div>(.*)<div class="sep"></div>', re.DOTALL)
SHOW_INFO_CAST_RE = re.compile('<a href=\'/buscar/\?q=.*?&cat=actor\'>(.*?)</a>')
SHOW_INFO_GENERE_RE = re.compile('<b>Género:</b>(.*?)<br />')
SHOW_INFO_LANGUAGE_RE = re.compile('<b>Idioma:</b>(.*?)<br />')
# Favorites support
FAVORITES_URL = HOST + '/user_fav.php'
ADD_FAVORITES_URL = HOST + '/botlink_fav.php?id=%s&serie=%s'
FAVORITE_SHOWS_RE = re.compile('<a href=\'/series/[0-9]*/[^/]*/\'>([^<]*)</a>')
FAVORITE_MOVIES_RE = re.compile('<a href=\'/peliculas/[0-9]*/[^/]*/\'>([^<]*)</a>')
# Bookmarks support
BOOKMARK_MOVIE_URL = HOST + '/user_marcadores.php?tipo=pelicula'
BOOKMARK_MOVIE_ADD_URL = HOST + '/botlink_book.php?id=%s'
BOOKMARK_MOVIE_DEL_URL = HOST + '/user_marcadores.php?tipo=pelicula&eliminar=true&id=%s'
BOOKMARK_MOVIE_RE = re.compile('<a href=\'/peliculas/.+?>(.+?)</a>')
BOOKMARK_SHOW_URL = HOST + '/user_marcadores.php'
BOOKMARK_SHOW_ADD_URL = HOST + '/botlink_book.php?id=%s&serie=true'
BOOKMARK_SHOW_DEL_URL = HOST + '/user_marcadores.php?tipo=serie&eliminar=true&id=%s'
BOOKMARK_SHOW_RE = re.compile('<a href=\'/series/.+?>(.+?)</a>')
NEXT_MOVIES_URL = HOST + "/peliculas/proximas/page=%d"
NEXT_MOVIES_RE = re.compile(r'<tr class=\'row[1-2]\'>.*?<div class=\'tit\'><a '\
'href=\'/peliculas/([0-9]*?)/.*?/\'>(.*?)</a></div>.*?<div '\
'class=\'font11\'>(.*?)<div class=\'reparto\'>', re.DOTALL)
URL_OPEN = UrlOpen() # Setup a function with cookies support
class Pycavane(object):
"""
Provides a simple api to obtain data from cuevana
"""
def __init__(self, cache_dir='/tmp/', cache_lifetime=60*60*6):
"""
Does the inicialization and login of the website.
"""
Memoized.set_cache_dir(cache_dir)
Memoized.set_lifetime(cache_lifetime)
self.logged = False
def login(self, username, password):
data = {'usuario': username, 'password': password,
'ingresar': True, 'recordarme': 'si'}
ret = URL_OPEN('http://www.cuevana.tv/login_get.php', data=data)
if username not in ret:
raise Exception('Login fail, check username and password')
self.logged = True
@Memoized
def get_movies(self, letter='num', page=0):
"""
Returns a list with (id, name, description) of all the movies starting
with `letter` or all the movies in case letter isn't set.
"""
if not self.logged:
raise Exception('Must be logged to retrive movies')
all_movies = []
while True:
page += 1
page_data = URL_OPEN(MOVIES_URL % (letter, page))
moov = MOVIES_RE.findall(page_data)
if not moov:
break
all_movies += moov
return all_movies
@Memoized
def get_next_movies(self, page=1):
data = URL_OPEN(NEXT_MOVIES_URL % page)
return NEXT_MOVIES_RE.findall(data)
def movie_by_name(self, name):
"""
Returns a tuple with (id, name) of the movie
based on the name.
"""
movies = self.search_title(name)[0]
found = False
for movie in movies:
if movie[1] == name:
found = movie
break
assert found != False
return (movie[0], movie[1])
@Memoized
def episodes_by_season(self, show, season_name):
"""
Returns a list with (id, episode_number, episode_name) of
the episodes from `show` at `seasson_name`
"""
seasons = self.seasson_by_show(show)
for season in seasons:
if season[1] == season_name:
return self.get_episodes(season)
@Memoized
def episode_by_name(self, name, show, seasson):
episode_found = None
for episode in self.episodes_by_season(show, seasson):
if episode[2] == name:
episode_found = episode
break
return episode_found
@Memoized
def seasson_by_show(self, name):
"""
Retruns a list with (id, name) of the currently avaliable
seassons from a certain show based only on the name.
"""
show = self.show_by_name(name)
if show:
return self.get_seassons(show)
return []
@Memoized
def show_by_name(self, name):
"""
Returns a tuple with (id, name) of the show
based on the name.
"""
for show in self.get_shows():
if show[1] == name:
return show
@Memoized
def get_shows(self, name=None):
"""
Returns a list with (id, name) of all the
currently avaliable shows.
"""
series = SHOWS_RE.findall(URL_OPEN(SHOWS_URL))
if name:
series = [serie for serie in series \
if name.lower() in serie[1].lower()]
return series
@Memoized
def get_episode_info(self, episode):
"""
Returns a tuple with (image, episode_name, description,
cast, genere, language) of the show with the given episode.
"""
page_data = URL_OPEN(SHOW_INFO_URL % episode[0])
name = episode[2]
image = HOST + SHOW_INFO_IMAGE_RE.findall(page_data)[0]
desc = SHOW_INFO_DESCRIPTION_RE.findall(page_data)[0].strip()
cast = SHOW_INFO_CAST_RE.findall(page_data)
genere = SHOW_INFO_GENERE_RE.findall(page_data)[0].strip()
language = SHOW_INFO_LANGUAGE_RE.findall(page_data)[0].strip()
return (image, name, desc, cast, genere, language)
@Memoized
def get_seassons(self, serie):
"""
Returns a list with (id, name) of the seassons of `serie`
"""
seassons = SEASSON_RE.findall(URL_OPEN(SEASSONS_URL % serie[0]))
return seassons
@Memoized
def get_episodes(self, seasson):
"""
Returns a list with (id, episode_number, episode_name) of
the episodes of `seasson`.
"""
episodes = EPISODE_RE.findall(URL_OPEN(EPISODES_URL % seasson[0]))
return episodes
@Memoized
def get_direct_links(self, episode, host=None, movie=False):
"""
Returns a list with (name, link) of all avaliable host
sources for the episode.
"""
if movie:
url = PLAYER_MOVIE_URL
else:
url = PLAYER_SEASON_URL
data = URL_OPEN(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(self, episode, lang='ES', filename=None, movie=False):
"""
Downloads the subtitle of the episode.
"""
if filename:
filename += '.srt'
if movie:
url = SUB_URL_MOVIE
else:
url = SUB_URL_SHOW
return URL_OPEN(url % (episode[0], lang), filename=filename)
@Memoized
def search_title(self, query):
"""
Returns a list with a tuple (result_id, result_name, result_is_movie)
with the results of the search.
"""
search_list = []
query = query.replace(" ", "+")
page_data = URL_OPEN(SEARCH_URL % query)
results = SEARCH_RE.findall(page_data)
maybe_meant = MAYBE_MEANT.findall(page_data)
if maybe_meant:
maybe_meant = maybe_meant[0]
for i in results:
url = i[0].split("/")
result_is_movie = url[1] == "peliculas"
result_id = url[2]
result_name = i[1]
search_list.append((result_id, result_name, result_is_movie))
result = (search_list, maybe_meant)
return result
def get_favorite_series(self):
if not self.logged:
return []
rc = URL_OPEN(FAVORITES_URL, data={'tipo': 'serie'})
return FAVORITE_SHOWS_RE.findall(URL_OPEN(FAVORITES_URL,
data={'tipo': 'serie'}))
def get_favorite_movies(self):
if not self.logged:
return []
return FAVORITE_MOVIES_RE.findall(URL_OPEN(FAVORITES_URL,
data={'tipo': 'pelicula'}))
def add_favorite(self, name, is_movie):
if is_movie:
idnum, _ = self.movie_by_name(name)
URL_OPEN(ADD_FAVORITES_URL % (idnum, 'false'))
else:
idnum, _ = self.show_by_name(name)
URL_OPEN(ADD_FAVORITES_URL % (idnum, 'true'))
def del_favorite(self, name, is_movie):
if is_movie:
idnum, _ = self.movie_by_name(name)
URL_OPEN(FAVORITES_URL,
data={'tipo':'pelicula', 'eliminar':'true', 'id': idnum})
else:
idnum, _ = self.show_by_name(name)
URL_OPEN(FAVORITES_URL,
data={'tipo':'serie', 'eliminar':'true', 'id': idnum})
def get_movie_bookmarks(self):
return BOOKMARK_MOVIE_RE.findall(URL_OPEN(BOOKMARK_MOVIE_URL))
def get_show_bookmarks(self):
return BOOKMARK_SHOW_RE.findall(URL_OPEN(BOOKMARK_SHOW_URL))
def add_movie_bookmark(self, name):
idnum, _ = self.movie_by_name(name)
URL_OPEN(BOOKMARK_MOVIE_ADD_URL % idnum)
def del_movie_bookmark(self, name):
idnum, _ = self.movie_by_name(name)
URL_OPEN(BOOKMARK_MOVIE_DEL_URL % idnum)
def add_show_bookmark(self, name, show, seasson):
idnum, _, _ = self.episode_by_name(name, show, seasson)
URL_OPEN(BOOKMARK_SHOW_ADD_URL % idnum)
def del_show_bookmark(self, name, show, seasson):
idnum, _, _ = self.episode_by_name(name, show, seasson)
URL_OPEN(BOOKMARK_SHOW_DEL_URL % idnum)