[allocine] handles showtimelist in allocine

This commit is contained in:
Bezleputh 2014-12-26 21:18:28 +01:00 committed by Florent
commit 0ec91edc37
3 changed files with 135 additions and 11 deletions

View file

@ -17,6 +17,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.calendar import BaseCalendarEvent, TRANSP, STATUS, CATEGORIES
from weboob.capabilities.collection import Collection from weboob.capabilities.collection import Collection
from weboob.capabilities.video import BaseVideo from weboob.capabilities.video import BaseVideo
from weboob.capabilities.image import BaseImage from weboob.capabilities.image import BaseImage
@ -30,7 +31,6 @@ from datetime import datetime, date, timedelta
import time import time
import urllib import urllib
__all__ = ['AllocineBrowser'] __all__ = ['AllocineBrowser']
@ -492,8 +492,9 @@ class AllocineBrowser(Browser):
video.title = unicode(movie['trailer']['name']) video.title = unicode(movie['trailer']['name'])
video._video_code = unicode(movie['trailer']['code']) video._video_code = unicode(movie['trailer']['code'])
video.ext = u'mp4' video.ext = u'mp4'
video.thumbnail = BaseImage(movie['poster']['href']) if 'poster' in movie:
video.thumbnail.url = unicode(movie['poster']['href']) video.thumbnail = BaseImage(movie['poster']['href'])
video.thumbnail.url = unicode(movie['poster']['href'])
tdate = movie['release']['releaseDate'].split('-') tdate = movie['release']['releaseDate'].split('-')
day = 1 day = 1
month = 1 month = 1
@ -507,7 +508,7 @@ class AllocineBrowser(Browser):
if 'userRating' in movie['statistics']: if 'userRating' in movie['statistics']:
video.rating = movie['statistics']['userRating'] video.rating = movie['statistics']['userRating']
elif 'pressRating' in movie['statistics']: elif 'pressRating' in movie['statistics']:
video.rating = movie['statistics']['pressRating']*2 video.rating = movie['statistics']['pressRating'] * 2
video.rating_max = 5 video.rating_max = 5
if 'synopsis' in movie: if 'synopsis' in movie:
video.description = unicode(movie['synopsis'].replace('<p>', '').replace('</p>', '')) video.description = unicode(movie['synopsis'].replace('<p>', '').replace('</p>', ''))
@ -561,3 +562,86 @@ class AllocineBrowser(Browser):
result = json.loads(res) result = json.loads(res)
for emission in result['feed']['term']: for emission in result['feed']['term']:
yield Collection([basename, unicode(emission['nameShort'])], unicode(emission['$'])) yield Collection([basename, unicode(emission['nameShort'])], unicode(emission['$']))
def search_events(self, query):
params = [('partner', self.PARTNER_KEY),
('format', 'json'),
('zip', query.city)
]
if query.summary:
movie = self.iter_movies(query.summary).next()
params.append(('movie', movie.id))
res = self.__do_request('showtimelist', params)
if res is None:
return
result = json.loads(res)
for event in self.create_event(result):
if (not query.end_date or event.start_date <= query.end_date)\
and event.start_date >= query.start_date:
yield event
def get_event(self, _id):
split_id = _id.split('#')
params = [('partner', self.PARTNER_KEY),
('format', 'json'),
('theaters', split_id[0]),
('zip', split_id[1]),
('movie', split_id[2]),
]
res = self.__do_request('showtimelist', params)
if res is None:
return
result = json.loads(res)
for event in self.create_event(result):
if event.id.split('#')[-1] == split_id[-1]:
event.description = self.get_movie(split_id[2]).pitch
return event
def create_event(self, data):
sequence = 1
transp = TRANSP.TRANSPARENT
status = STATUS.CONFIRMED
category = CATEGORIES.CINE
if 'theaterShowtimes' not in data['feed']:
return
for items in data['feed']['theaterShowtimes']:
cinema = items['place']['theater']
city = unicode(cinema['city'])
location = u'%s, %s' % (cinema['name'], cinema['address'])
postalCode = cinema['postalCode']
cinemaCode = cinema['code']
for show in items['movieShowtimes']:
summary = unicode(show['onShow']['movie']['title'])
movieCode = show['onShow']['movie']['code']
for jour in show['scr']:
tdate = jour['d'].split('-')
year = int(tdate[0])
month = int(tdate[1])
day = int(tdate[2])
for seance in jour['t']:
ttime = seance['$'].split(':')
heure = int(ttime[0])
minute = int(ttime[1])
start_date = datetime(year=year, month=month, day=day,
hour=heure, minute=minute)
seanceCode = seance['code']
_id = u'%s#%s#%s#%s' % (cinemaCode, postalCode, movieCode, seanceCode)
event = BaseCalendarEvent()
event.id = _id
event.sequence = sequence
event.transp = transp
event.status = status
event.category = category
event.city = city
event.location = location
event.start_date = start_date
event.summary = summary
yield event

View file

@ -16,7 +16,10 @@
# #
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
import re
from weboob.capabilities.base import UserError
from weboob.capabilities.calendar import CapCalendarEvent, CATEGORIES, BaseCalendarEvent
from weboob.capabilities.video import CapVideo, BaseVideo from weboob.capabilities.video import CapVideo, BaseVideo
from weboob.capabilities.collection import CapCollection, CollectionNotFound, Collection from weboob.capabilities.collection import CapCollection, CollectionNotFound, Collection
from weboob.capabilities.cinema import CapCinema, Person, Movie from weboob.capabilities.cinema import CapCinema, Person, Movie
@ -27,7 +30,7 @@ from .browser import AllocineBrowser
__all__ = ['AllocineModule'] __all__ = ['AllocineModule']
class AllocineModule(Module, CapCinema, CapVideo, CapCollection): class AllocineModule(Module, CapCinema, CapVideo, CapCalendarEvent, CapCollection):
NAME = 'allocine' NAME = 'allocine'
MAINTAINER = u'Julien Veyssier' MAINTAINER = u'Julien Veyssier'
EMAIL = 'julien.veyssier@aiur.fr' EMAIL = 'julien.veyssier@aiur.fr'
@ -35,6 +38,7 @@ class AllocineModule(Module, CapCinema, CapVideo, CapCollection):
DESCRIPTION = u'AlloCiné French cinema database service' DESCRIPTION = u'AlloCiné French cinema database service'
LICENSE = 'AGPLv3+' LICENSE = 'AGPLv3+'
BROWSER = AllocineBrowser BROWSER = AllocineBrowser
ASSOCIATED_CATEGORIES = [CATEGORIES.CINE]
def get_movie(self, id): def get_movie(self, id):
return self.browser.get_movie(id) return self.browser.get_movie(id)
@ -117,7 +121,7 @@ class AllocineModule(Module, CapCinema, CapVideo, CapCollection):
video = self.get_video(self, video.id) video = self.get_video(self, video.id)
if hasattr(video, '_video_code'): if hasattr(video, '_video_code'):
video.url = self.browser.get_video_url(video._video_code) video.url = unicode(self.browser.get_video_url(video._video_code))
if 'thumbnail' in fields and video and video.thumbnail: if 'thumbnail' in fields and video and video.thumbnail:
with self.browser: with self.browser:
@ -174,8 +178,29 @@ class AllocineModule(Module, CapCinema, CapVideo, CapCollection):
raise CollectionNotFound(collection.split_path) raise CollectionNotFound(collection.split_path)
def search_events(self, query):
with self.browser:
if CATEGORIES.CINE in query.categories:
if query.city and re.match('\d{5}', query.city):
events = list(self.browser.search_events(query))
events.sort(key=lambda x: x.start_date, reverse=False)
return events
else:
raise UserError('You must enter a zip code in city field')
def get_event(self, id):
return self.browser.get_event(id)
def fill_event(self, event, fields):
if 'description' in fields:
movieCode = event.id.split('#')[2]
movie = self.get_movie(movieCode)
event.description = movie.pitch
return event
OBJECTS = { OBJECTS = {
Person: fill_person, Person: fill_person,
Movie: fill_movie, Movie: fill_movie,
BaseVideo: fill_video BaseVideo: fill_video,
BaseCalendarEvent: fill_event
} }

View file

@ -19,6 +19,8 @@
from weboob.tools.test import BackendTest from weboob.tools.test import BackendTest
from weboob.capabilities.video import BaseVideo from weboob.capabilities.video import BaseVideo
from weboob.capabilities.calendar import Query, CATEGORIES
from datetime import datetime
import re import re
@ -93,26 +95,39 @@ class AllocineTest(BackendTest):
l1 = list(self.backend.iter_resources([BaseVideo], l[0].split_path)) l1 = list(self.backend.iter_resources([BaseVideo], l[0].split_path))
assert len(l1) assert len(l1)
v = l1[0] v = l1[0]
self.backend.fillobj(v, ('url',)) self.backend.fillobj(v, 'url')
self.assertTrue(v.url, 'URL for video "%s" not found' % (v.id)) self.assertTrue(v.url, 'URL for video "%s" not found' % (v.id))
def test_interview(self): def test_interview(self):
l = list(self.backend.iter_resources([BaseVideo], [u'interview'])) l = list(self.backend.iter_resources([BaseVideo], [u'interview']))
assert len(l) assert len(l)
v = l[0] v = l[0]
self.backend.fillobj(v, ('url',)) self.backend.fillobj(v, 'url')
self.assertTrue(v.url, 'URL for video "%s" not found' % (v.id)) self.assertTrue(v.url, 'URL for video "%s" not found' % (v.id))
def test_comingsoon(self): def test_comingsoon(self):
l = list(self.backend.iter_resources([BaseVideo], [u'comingsoon'])) l = list(self.backend.iter_resources([BaseVideo], [u'comingsoon']))
assert len(l) assert len(l)
v = l[0] v = l[0]
self.backend.fillobj(v, ('url',)) self.backend.fillobj(v, 'url')
self.assertTrue(v.url, 'URL for video "%s" not found' % (v.id)) self.assertTrue(v.url, 'URL for video "%s" not found' % (v.id))
def test_nowshowing(self): def test_nowshowing(self):
l = list(self.backend.iter_resources([BaseVideo], [u'nowshowing'])) l = list(self.backend.iter_resources([BaseVideo], [u'nowshowing']))
assert len(l) assert len(l)
v = l[0] v = l[0]
self.backend.fillobj(v, ('url',)) self.backend.fillobj(v, 'url')
self.assertTrue(v.url, 'URL for video "%s" not found' % (v.id)) self.assertTrue(v.url, 'URL for video "%s" not found' % (v.id))
def test_showtimelist(self):
query = Query()
query.city = u'59000'
query.categories = [CATEGORIES.CINE]
query.start_date = datetime.now()
l = self.backend.search_events(query)
assert len(l)
e = l[0]
self.backend.fillobj(e, 'description')
self.assertTrue(e.description, 'Description of "%s" not found' % (e.id))
e = self.backend.get_event(e.id)
self.assertTrue(e.description, 'Description of "%s" not found' % (e.id))