This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moviecrawler.py
1068 lines (750 loc) · 33 KB
/
moviecrawler.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
# moviecrawler.py
# The MIT License (MIT)
#
# Copyright (c) 2012 Roberto Zoia
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
# Revision history
#
# 2012-05-04
# Changed from urllib2 to urllib3.
# urllib3 opens just an http connection to each site and reuses it. This results
# in an important speed improvement (dropped total page generation time from 50sec to 30sec)
#
import os
import urllib3
from urllib3.exceptions import HTTPError, TimeoutError, MaxRetryError
import requests
import re
import datetime
import pytz
from bs4 import BeautifulSoup
from models import TheaterChain, Theater, Movie
import tools
import title_except
#
# Para usar una terminología clara, definimos:
# Cadena: UVK, Cinemark Perú, Cineplanet
# Cines de la cadena: UVK Larcomar, UVK Caminos del Inca
# Cartelera: películas que pasan en uno de los cines
# Película: úna película que pasan en un cine
#
class MovieCrawler(object):
"""docstring for Cine"""
def __init__(self, cadena="", tag=""):
self.tag = tag
self.cadena = cadena
# url de la página del cine
self.url = ""
# encoding usado para la salida de las funciones
self.encoding = 'utf-8'
# connection timeout for fetching pages
self.timeout = 12.0
# regex que catpura los horarios de un string
self.reHorarios = re.compile(r"""\d{1,2}:\d{2}\s*(?:am|pm|a\.m\.|p\.m\.)*""")
## prefijos y sufijos que la cadena de cines usa en los títulos de sus películas
# prefijos que indican subtitulación de la película
self.prefix_subtitles = {
'doblada': [],
'subtitulada': [],
}
self.suffix_subtitles = {
'doblada': [],
'subtitulada': [],
}
# prefijos y sufijos que indican resolúción/calidad de la película
# las clases derivadas deben modificar esta data para reflejar la cadena de cines específica
self.prefix_resolutions = {
'HD': [],
'3D': [],
}
self.suffix_resolutions = {
'HD': [],
'3D': [],
}
self.suffix_dbox = []
self.suffix_discard = []
self.movie_cache = {}
def getTheaterChainMovies(self):
"""
Returns a TheaterChain object with theaters,
movies and showtimes fetched from the theater chain
website
"""
theater_chain = TheaterChain(
name = self.cadena,
tag = self.tag,
theaters = self.get_cartelera_cines_de_cadena()
)
return theater_chain
def get_cines_cadena(self):
"""
Devuelve una lista que contiene los locales de la cadena de cines en el siguiente
formato:
[
(u'UVK Larcomar', 0,
r'http://www.uvkmulticines.com/multicines/cine/UVK-LARCOMAR'),
(u'UVK Platino Basadre', 0,
r'http://www.uvkmulticines.com/multicines/cine/UVK-PLATiNO-BASADRE-'),
(u'[Cine]', [idCine], [url de la página de la cartelera de ese cine])
(...)
]
Hay cadenas de cines que usan un id para identificar la página de cartelera de sus cines.
Otras usan un id para identificar la cartelera de cada cine.
Si una cadena usa ids, entonces get_cines_cadena devuelve None en vez de un url
Si una cadena usa urls, entonces get_cines_cadena devuelve idCine = 0
"""
pass
def get_programacion_cine(self, idCine=0, url=None):
"""
Devuelve la programación del cine.
Este método acepta tanto idCines como url como parámetros. Las clases
derivadas de Cine deben implementar la funcionalidad respectiva, según
la cadena de cines use ids o urls para identificar las páginas de carteleras.
"""
pass
def get_cartelera_cines_de_cadena(self):
"""
Returns a list of Theater objects.
"""
result = []
for t in self.get_cines_cadena():
cine, idCine, urlCine = t
theater = Theater(name=cine)
if idCine == 0:
theater.movies = self.get_programacion_cine(url=urlCine)
else:
theater.movies = self.get_programacion_cine(idCine=idCine)
# print "Theaters for %s: %s" % (self.tag, theater)
result.append(theater)
return result
#
# Utility methods
#
def is_movie_translated(self, s):
result = False
for r in self.suffix_subtitles['doblada']:
if s.find(r) != -1:
result = True
break
if not result:
for r in self.prefix_subtitles['doblada']:
if s.find(r) != -1:
result = True
break
return result
def is_movie_subtitled(self, s):
"""
Returns true if movie is subtitled.
Uses theater-specific prefixes and
"""
result = False
for r in self.suffix_subtitles['subtitulada']:
if s.find(r) != -1:
result = True
break
# Un temita es que en varios casos, si la película no dice nada,
# por defecto es subtitulada.
if not result:
result = not self.is_movie_translated(s)
return result
def is_movie_HD(self, s):
result = False
for r in self.suffix_resolutions['HD']:
if s.find(r) != -1:
result = True
break
if not result:
for r in self.prefix_resolutions['HD']:
if s.find(r) != -1:
result = True
break
return result
def is_movie_3D(self, s):
result = False
for r in self.suffix_resolutions['3D']:
if s.find(r) != -1:
result = True
break
if not result:
for r in self.prefix_resolutions['3D']:
if s.find(r) != -1:
result = True
break
return result
def is_movie_dbox(self, s):
result = False
if self.suffix_dbox:
for r in self.suffix_dbox:
if s.find(r) != -1:
result = True
break
return result
def purify_movie_name(self, s):
iterList = [
self.suffix_resolutions, self.prefix_resolutions,
self.suffix_subtitles, self.prefix_subtitles,
]
for listElm in iterList:
for l in listElm.itervalues():
for t in l:
s = s.replace("-", "")
s = " ".join(s.split()) # purge multiple spaces
s = s.replace(t, "")
for t in self.suffix_discard:
s = s.replace(t, "")
if self.suffix_dbox:
for r in self.suffix_dbox:
s = s.replace(r, "")
s = title_except.title_except(s.lower())
return s.strip()
def sort_movies(self, movies):
"""
Sorts movie dict by movie name.
"""
def convert_showtime_to_timeobject(self, showtime):
"""
Takes a showtime as string and returns a 24H formated time.
Handle quirks in movie chains showtimes.
"""
suffixes = [('pm', 12), ('p.m.', 12), ('am',0), ('a.m.',0)]
offset = 12 # by default, all showtimes are pm, even if not stated
for sfx, off in suffixes:
if sfx in showtime:
showtime = showtime.replace(sfx, '')
offset = off
# split hours, minutes
hours, minutes = showtime.split(':')
hours = int(hours)
minutes = int(minutes)
if (hours == 12) and (offset == 12): offset = 0
try:
result = datetime.time(int(hours)+offset, int(minutes)).strftime('%H:%M')
except ValueError:
result = datetime.time(int(hours), int(minutes)).strftime('%H:%M')
return result
def grab_horarios(self, s):
horarios = [ self.convert_showtime_to_timeobject(t) for t in self.reHorarios.findall(s)]
horarios.sort()
return horarios
def replace_accents(self, s):
replacements = [(u'á','a'), (u'é','e'), (u'í', 'i'), (u'ó','o'),(u'ú','u'),
(u'Á','A'), (u'É', 'E'), (u'Í','I'), (u'Ó','O'), (u'Ú','U'),
]
for a, b in replacements:
s = s.replace(a, b)
return s
class MovieCrawlerCineStar(MovieCrawler):
# http://www.cinestar.com.pe/
pass
class MovieCrawlerMovieTime(MovieCrawler):
# http://www.movietime.com.pe/
pass
class MovieCrawlerMultiCinesPlazaJM(MovieCrawler):
# http://www.multicinesplazajesusmaria.com/
pass
class MovieCrawlerCinerama(MovieCrawler):
# http://www.cinerama.com.pe/
def __init__(self):
super(MovieCrawlerCinerama, self).__init__(cadena=u"Cinerama", tag="CRAMA")
self.url = r"http://www.cinerama.com.pe/"
self.encoding = 'utf-8'
self.conn = urllib3.connection_from_url(self.url, timeout = self.timeout)
def get_cines_cadena(self):
url = r'http://www.cinerama.com.pe/cines.php'
retries = 3
while retries > 0:
try:
r = self.conn.request(
'GET',
url,
headers = urllib3.make_headers(user_agent=wanderer())
)
break
except TimeoutError:
retries = retries - 1
if retries > 0:
if r.status == 200:
html = r.data.decode(self.encoding, errors='replace')
soup = BeautifulSoup(html)
theaters = [
(
c.next.text.strip(),
os.path.join(self.url, c.next['href'])
) for c in soup.find_all(
'p', class_='titulo-tienda'
)]
return theaters
else:
return []
def get_cartelera_cines_de_cadena(self):
theaters = []
t_theaters = self.get_cines_cadena()
for t in t_theaters:
theater_name, url = t
theater = Theater(name = theater_name.capitalize())
theater.movies = self.get_programacion_cine(url=url)
theaters.append(theater)
return theaters
def get_programacion_cine(self, idCine=0, url=None):
retries = 3
while retries > 0:
try:
r = self.conn.request(
'GET',
url,
headers = urllib3.make_headers(user_agent=wanderer())
)
break
except TimeoutError:
retries = retries - 1
if retries > 0:
if r.status == 200:
html = r.data.decode(self.encoding, errors='replace')
soup = BeautifulSoup(html)
m_titles = [m.a.string.strip() for m in soup.find_all(
'div', class_='titcarte') if m.a]
m_showtimes = []
for m in soup.find_all('div', class_='horasprof'):
if m.string:
m_showtimes.append(m.string.strip())
else:
m_showtimes.append(None)
movies = []
for i in range(0, len(m_titles)):
# This is to handle case when no showtimes available for movie
if m_showtimes[i]:
movie = Movie(
name = self.purify_movie_name(m_titles[i]),
showtimes = self.grab_horarios(m_showtimes[i]),
# La página web de Cinerama no da mayor información
isSubtitled = True,
isTranslated = False,
isHD = True,
is3D = False,
isDbox = False,
)
movies.append(movie)
return movies
else:
return []
else:
return []
#
# Cinépolis
#
class MovieCrawlerCinepolis(MovieCrawler):
def __init__(self):
super(MovieCrawlerCinepolis, self).__init__(cadena=u"Cinépolis", tag="CPOL")
self.url = r"http://www.cinepolis.com.pe"
self.encoding = 'utf-8'
self.suffix_subtitles['doblada'] = [
u'Dob',
]
self.suffix_subtitles['subtitulada'] = [
]
self.suffix_resolutions['HD'] = [
u'Dig',
]
self.conn = urllib3.connection_from_url(self.url, timeout = self.timeout)
def get_cines_cadena(self):
pass
def get_programacion_cine(self):
pass
def get_cartelera_cines_de_cadena(self):
"""
Returns a list of Theater objects.
"""
def get_movies(url):
retries = 3
result = []
while retries > 0:
try:
r = self.conn.request(
'GET',
url,
headers = urllib3.make_headers(user_agent=wanderer())
)
break
except TimeoutError:
retries = retries - 1
if retries > 0:
if r.status == 200:
# Page readed ok
html = r.data.decode(self.encoding, errors='replace')
soup = BeautifulSoup(html)
theater_names = [t.string for t in soup.find_all(
'span', class_='TitulosBlanco'
)]
theater_codes = [ t['id'][4:] for t in soup.find_all(
'a', id=re.compile('^Cine*')
)]
for i in range(0, len(theater_names)):
theater = Theater(name=theater_names[i])
movies = []
tmp = soup.find_all('a', id=re.compile(
'^idPelCine.+' + theater_codes[i]
))
for p in tmp:
movie_name = p.parent.find('a', class_='peliculaCartelera').string
movie_showtimes = [t.string for t in p.parent.parent.find_all(
'span', class_=re.compile('^horariosCartelera*')
)]
movie_showtimes = self.grab_horarios(" ".join(movie_showtimes))
movie = Movie(
name = self.purify_movie_name(movie_name),
showtimes = movie_showtimes,
isSubtitled = self.is_movie_subtitled(movie_name),
isTranslated = self.is_movie_translated(movie_name),
isHD = self.is_movie_HD(movie_name),
is3D = self.is_movie_3D(movie_name),
isDbox = self.is_movie_dbox(movie_name),
)
movies.append(movie)
# movies.sort(key=lambda x: x.name)
theater.movies = movies
result.append(theater)
else:
return []
else:
return []
return result
urls = [
r"http://www.cinepolis.com.pe/_CARTELERA/cartelera.aspx?ic=100",
r"http://www.cinepolis.com.pe/_CARTELERA/cartelera.aspx?ic=143",
]
theaters = []
for u in urls:
theaters = theaters + get_movies(u)
return theaters
#
# UVK
#
class MovieCrawlerUVK(MovieCrawler):
"""UVK Multicines"""
def __init__(self):
super(MovieCrawlerUVK, self).__init__(cadena=u"UVK Multicines",tag="UVK")
self.url = r"""http://www.uvkmulticines.com"""
self.encoding = 'utf-8'
# indicadores de subtitulos
# (indicador también del desorden cerebral de los encargados del website de UVK)
self.suffix_subtitles['doblada'] = [
u'(doblada Estreno)', u'(Doblada - Estreno)',
u'(Estreno Doblada)', u'(Doblada Estreno)', u'(HD Doblada)', u'(Digital doblada)', u'(Doblada)',
u'( Doblada)',
]
self.suffix_subtitles['subtitulada'] = [
u'(subtitulada Estreno )', u'(Estreno Subtitulada)', u'(Subtitulada Estreno)', u'(Subtitulada - Estreno)', u'(HD Subtitulada)', u'(Digital subtitulada)',
u'(subtitulada)', u'(Subtitulada)', u'( Subtitulada)',
]
# indicadores de resolución
self.suffix_resolutions['HD'] = [ u'(HD Doblada)', u'(Digital subtitulada)', u'(Digital doblada)', u'HD']
self.suffix_resolutions['3D'] = [ u'3D', ]
self.suffix_discard = [ '(Estreno)', ]
self.conn = urllib3.connection_from_url(self.url, timeout=self.timeout)
def get_cines_cadena(self):
# TODO: sacar esta lista de la página web
result = [
(u'Platino Basadre', 0, r"http://www.uvkmulticines.com/multicines/cine/UVK-PLATiNO-BASADRE-"),
(u'Larcomar', 0, r"http://www.uvkmulticines.com/multicines/cine/UVK-LARCOMAR" ),
(u'Caminos del Inca',0, r"http://www.uvkmulticines.com/multicines/cine/UVK-CAMiNOS-DEL-iNCA"),
(u'San Martín Centro', 0, r"http://www.uvkmulticines.com/multicines/cine/UVK-SAN-MARTIN-CENTRO"),
(u'Ica', 0, r"http://www.uvkmulticines.com/multicines/cine/UVK-iCA" ),
(u'Asia', 0, r"http://www.uvkmulticines.com/multicines/cine/UVK-ASiA"),
(u'Huacho', 0, r"http://www.uvkmulticines.com/multicines/cine/UVK-HUACHO" ),
(u'El Agustino', 0, r"http://www.uvkmulticines.com/multicines/cine/UVK-EL-AGUSTINO" ),
(u'Piura', 0, r"http://www.uvkmulticines.com/multicines/cine/UVK-PiURA"),
]
return result
def get_programacion_cine(self, idCine=0, url=None):
# UVK usa urls para identificar sus cines, no ids
# TODO: handle errors
retries = 3
while retries > 0:
try:
r = self.conn.request(
'GET',
url,
headers = urllib3.make_headers(user_agent=wanderer())
)
break
except TimeoutError:
retries = retries - 1
if retries > 0:
if r.status == 200:
# Page readed ok
html = r.data.decode(self.encoding, errors='replace')
# html = r.text.encode(r.encoding, errors='replace')
soup = BeautifulSoup(html, 'html5lib')
p1 = soup.find('div', class_='highslide-body')
if p1:
peliculas = p1.find_all('td', class_='bg_infotabla1')
else:
return []
result = []
for i in range(0, len(peliculas)-1):
# Identificamos el comienzo del listado de películas porque son los
# <td> que tienen class bg_infotabla1 y un elemento <a href=...>
if peliculas[i].a is not None:
# El primer <td> tiene el nombre de la película.
# El segundo <td> tiene el horario
movie = Movie(
name = self.purify_movie_name(peliculas[i].string),
showtimes = self.grab_horarios(peliculas[i+1].string),
isSubtitled = self.is_movie_subtitled(peliculas[i].string),
isTranslated = self.is_movie_translated(peliculas[i].string),
isHD = self.is_movie_HD(peliculas[i].string),
is3D =self.is_movie_3D(peliculas[i].string),
isDbox = self.is_movie_dbox(peliculas[i].string),
)
result.append(movie)
i = i +2
else:
i = i+1
# Sort movies
result.sort(key=lambda x: x.name)
return result
else:
return []
else:
return []
class MovieCrawlerCMP(MovieCrawler):
"""docstring for CineCMP"""
def __init__(self):
super(MovieCrawlerCMP, self).__init__(cadena=u"Cinemark", tag="CMP")
self.url = r"""http://www.cinemark-peru.com"""
# indicadores de subtitulos
self.suffix_subtitles['doblada'] = [ u'(DOB)', u'(DOB', ]
self.suffix_subtitles['subtitluada'] = [ u'(SUB)', u'(SUB',]
# indicadores de resolución
self.suffix_resolutions['HD'] = [ u'XD', u'XD 3D' ]
self.suffix_resolutions['3D'] = [ u'3D', ]
# único de Cinemark...
self.prefix_resolutions['HD'] = [ u'2D Digital', u'2D DIG']
self.suffix_discard = [ '2D', ]
self.suffix_dbox = [ 'd-box', 'D-Box', 'D-box', 'D-BOX', 'DBOX', 'dbox', 'Dbox', ]
# aunque su página diga 'utf-8', el servidor envía iso-8859-15
# self.encoding = 'iso-8859-15'
self.encoding = 'utf-8'
# self.suffix_resolutions = self.replace_strings_for_regex_in_dict_with_list(self.suffix_resolutions)
# self.prefix_resolutions = self.replace_strings_for_regex_in_dict_with_list(self.prefix_resolutions)
# self.suffix_subtitles = self.replace_strings_for_regex_in_dict_with_list(self.suffix_subtitles)
# self.prefix_subtitles = self.replace_strings_for_regex_in_dict_with_list(self.prefix_subtitles)
self.conn = urllib3.connection_from_url(self.url, timeout=self.timeout)
def get_cines_cadena(self):
# cines = [
# (u'Jockey Plaza', 740),
# (u'San Miguel', 742),
# (u'Mega Plaza', 743),
# (u'Plaza Lima Sur', 741),
# (u'Aventura Plaza Trujillo', 744),
# (u'Aventura Plaza Bella Vista', 754),
# (u'Open Plaza Angamos', 600),
# (u'Parque Lambramani', 659),
# (u'Open Plaza Piura', 660),
# (u'Aventura Plaza Arequipa', 778),
# ]
cines = [
(u'Jockey Plaza', '/cines/jockey-plaza'),
(u'San Miguel', '/cines/san-miguel'),
(u'Mega Plaza', '/cines/mega-plaza'),
(u'Plaza Lima Sur', '/cines/plaza-lima-sur'),
(u'Aventura Plaza Trujillo', '/cines/mall-aventura-plaza-trujillo'),
(u'Aventura Plaza Bella Vista', '/cines/mall-aventura-plaza-bellavista'),
(u'Open Plaza Angamos', '/cines/open-plaza-angamos'),
(u'Parque Lambramani', '/cines/parque-lambramani'),
(u'Open Plaza Piura', '/cines/open-plaza-piura'),
(u'Aventura Plaza Arequipa', '/cines/mall-aventura-plaza-arequipa'),
]
url = """%s%s"""
result = [ [c, 0, url % ( self.url, u)] for c, u in cines ]
return result
def get_programacion_cine(self, idCine=0, url=None):
def only_today(l):
months = [ 'Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun',
'Jul', 'Ago', 'Set', 'Oct', 'Nov', 'Dic']
# build string for today in CMP format (i.e., '07 Feb')
dt = datetime.datetime.utcnow()
dt = dt.replace(tzinfo = pytz.utc)
today_tag = "%02d %s" % (
datetime.datetime.astimezone(dt, pytz.timezone('America/Lima')).day,
months[dt.month-1].upper()
)
result = []
for elm in l:
if elm.text.upper() == today_tag:
result.append(elm)
return result
retries = 3
while retries > 0:
try:
r = self.conn.request('GET', url)
break
except TimeoutError:
retries = retries - 1
if retries > 0:
if r.status == 200:
# En su página dice 'charset=utf-8' pero usan en realidad iso-8859-15
html = r.data.decode(self.encoding, errors='replace')
soup = BeautifulSoup(html)
base = soup.find_all('h4')
# keep only today movies and showtimes
base = only_today(base)
result = []
for elm in base:
z = elm.next_sibling.next_sibling
tmpPeliculas = z.find_all('h5')
tmpHorarios = z.find_all('ul')
tmp_list = zip(tmpPeliculas, tmpHorarios)
for p, h in tmp_list:
tPelicula = p.text.strip()
tHorarios = h.text.strip()
movie = Movie(
name = self.purify_movie_name(tPelicula),
showtimes = self.grab_horarios(tHorarios),
isSubtitled = self.is_movie_subtitled(tPelicula),
isTranslated = self.is_movie_translated(tPelicula),
isHD = self.is_movie_HD(tPelicula),
is3D = self.is_movie_3D(tPelicula),
isDbox = self.is_movie_dbox(tPelicula),
)
result.append(movie)
# Sort movies
result.sort(key=lambda x: x.name)
return result
else:
return []
else:
return []
def wanderer():
return "The Crawler"
class MovieCrawlerCP(MovieCrawler):
"""Cine implementation for CinePlanet"""
def __init__(self):
super(MovieCrawlerCP, self).__init__(cadena=u"Cineplanet", tag="CP")
# indicadores de subtitulos
self.suffix_subtitles['doblada'] = [
u'2D Doblada',
u'3D Doblada',
u'Doblada',
]
self.suffix_subtitles['subtitluada'] = [
u'Subtitulada', u'2D Subtitulada',
u'3D Subtitulada',
]
# indicadores de resolución
self.suffix_resolutions['HD'] = [ u'Digital', u'Digital Hd', u'HD', u'Hd', ]
self.suffix_resolutions['3D'] = [ u'3D', ]
self.suffix_discard = [ ]
self.url = r"""https://cineplanet.com.pe"""
self.encoding = 'utf-8'
urllib3.make_headers(user_agent=wanderer())
self.conn = urllib3.connectionpool.connection_from_url(
self.url,
timeout=self.timeout,
headers=wanderer()
)
def get_cines_cadena(self):
"""
Devuelve una lista de los cines y sus urls.
Este método fue modificado el 2014-09-26 porque Cineplanet cambió
su página web (cambió para mejor: más ordenada y gráficamente más
amigable).
"""
result = []
tipo_salas = [ 'cines-lima', 'cines-provincias']
for sala in tipo_salas:
retries = 5
while retries > 0:
try:
r = self.conn.request(
'GET',
os.path.join(self.url, sala),
headers ={ 'user-agent': wanderer() }
)
break
except TimeoutError:
retries = retries - 1
if retries > 0:
if r.status == 200:
html = r.data.decode(self.encoding, errors='replace')
soup = BeautifulSoup(html)
# Get all cines for this kind of sala
t_cines = soup.find_all('div', { 'class': 'WEB_cineListadoNombre' })
for c in t_cines:
tmp = []
tmp.append(c.a.text)
tmp.append(0)
tmp.append(os.path.join(self.url, c.a['href']))
result.append(tmp)
# Salas prime
# Cineplanet uses Javascript to generate the data for its prime theaters.
# So we choose the easiest way (perhaps not the best...): hardcode the urls.
result.append([u'Salaverry', 0, os.path.join('/cine/salaverry')])
result.append([u'San Borja (La Rambla)', 0, os.path.join('/cine/san-borja')])
result.append([u'Plaza San Miguel', 0, os.path.join('/cine/san-miguel')])
return result
def get_programacion_cine(self, idCine=0, url=None):