Implementation of the new API mechanism + tests
With the new Allocine API needs the query to be signed. So I added a new method to build the query with the signature algorithm. Also adding some tests to improve coverage.
This commit is contained in:
parent
665871096a
commit
002f284d31
2 changed files with 138 additions and 19 deletions
|
|
@ -18,12 +18,16 @@
|
||||||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
from weboob.tools.browser import BaseBrowser
|
|
||||||
from weboob.capabilities.base import NotAvailable, NotLoaded
|
from weboob.capabilities.base import NotAvailable, NotLoaded
|
||||||
from weboob.capabilities.cinema import Movie, Person
|
from weboob.capabilities.cinema import Movie, Person
|
||||||
|
from weboob.tools.browser import BaseBrowser
|
||||||
from weboob.tools.json import json
|
from weboob.tools.json import json
|
||||||
|
import base64
|
||||||
|
import hashlib
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import time
|
||||||
|
import urllib
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['AllocineBrowser']
|
__all__ = ['AllocineBrowser']
|
||||||
|
|
||||||
|
|
@ -32,10 +36,28 @@ class AllocineBrowser(BaseBrowser):
|
||||||
DOMAIN = 'api.allocine.fr'
|
DOMAIN = 'api.allocine.fr'
|
||||||
PROTOCOL = 'http'
|
PROTOCOL = 'http'
|
||||||
ENCODING = 'utf-8'
|
ENCODING = 'utf-8'
|
||||||
USER_AGENT = BaseBrowser.USER_AGENTS['android']
|
USER_AGENT = 'Dalvik/1.6.0 (Linux; U; Android 4.2.2; Nexus 4 Build/JDQ39E)'
|
||||||
|
|
||||||
|
PARTNER_KEY = '100043982026'
|
||||||
|
SECRET_KEY = '29d185d98c984a359e6e6f26a0474269'
|
||||||
|
|
||||||
|
def __do_request(self, method, params):
|
||||||
|
params_encode = urllib.urlencode(params)
|
||||||
|
|
||||||
|
sed = time.strftime('%Y%m%d', time.localtime())
|
||||||
|
sig = base64.b64encode(hashlib.sha1(self.SECRET_KEY + params_encode + '&sed=' + sed).digest())
|
||||||
|
|
||||||
|
query_url = 'http://api.allocine.fr/rest/v3/' + method + '?' + params_encode + '&sed=' + sed + '&sig=' + sig
|
||||||
|
|
||||||
|
return self.readurl(query_url)
|
||||||
|
|
||||||
def iter_movies(self, pattern):
|
def iter_movies(self, pattern):
|
||||||
res = self.readurl('http://api.allocine.fr/rest/v3/search?partner=YW5kcm9pZC12M3M&filter=movie&q=%s&format=json' % pattern.encode('utf-8'))
|
params = [('partner', self.PARTNER_KEY),
|
||||||
|
('q', pattern.encode('utf-8')),
|
||||||
|
('format', 'json'),
|
||||||
|
('filter', 'movie')]
|
||||||
|
|
||||||
|
res = self.__do_request('search', params)
|
||||||
jres = json.loads(res)
|
jres = json.loads(res)
|
||||||
if 'movie' not in jres['feed']:
|
if 'movie' not in jres['feed']:
|
||||||
return
|
return
|
||||||
|
|
@ -67,7 +89,12 @@ class AllocineBrowser(BaseBrowser):
|
||||||
yield movie
|
yield movie
|
||||||
|
|
||||||
def iter_persons(self, pattern):
|
def iter_persons(self, pattern):
|
||||||
res = self.readurl('http://api.allocine.fr/rest/v3/search?partner=YW5kcm9pZC12M3M&filter=person&q=%s&format=json' % pattern.encode('utf-8'))
|
params = [('partner', self.PARTNER_KEY),
|
||||||
|
('q', pattern.encode('utf-8')),
|
||||||
|
('format', 'json'),
|
||||||
|
('filter', 'person')]
|
||||||
|
|
||||||
|
res = self.__do_request('search', params)
|
||||||
jres = json.loads(res)
|
jres = json.loads(res)
|
||||||
if 'person' not in jres['feed']:
|
if 'person' not in jres['feed']:
|
||||||
return
|
return
|
||||||
|
|
@ -95,8 +122,15 @@ class AllocineBrowser(BaseBrowser):
|
||||||
yield person
|
yield person
|
||||||
|
|
||||||
def get_movie(self, id):
|
def get_movie(self, id):
|
||||||
res = self.readurl(
|
params = [('partner', self.PARTNER_KEY),
|
||||||
'http://api.allocine.fr/rest/v3/movie?partner=YW5kcm9pZC12M3M&code=%s&profile=large&mediafmt=mp4-lc&format=json&filter=movie&striptags=synopsis,synopsisshort' % id)
|
('code', id),
|
||||||
|
('profile', 'large'),
|
||||||
|
('mediafmt', 'mp4-lc'),
|
||||||
|
('filter', 'movie'),
|
||||||
|
('striptags', 'synopsis,synopsisshort'),
|
||||||
|
('format', 'json')]
|
||||||
|
|
||||||
|
res = self.__do_request('movie', params)
|
||||||
if res is not None:
|
if res is not None:
|
||||||
jres = json.loads(res)
|
jres = json.loads(res)
|
||||||
if 'movie' in jres:
|
if 'movie' in jres:
|
||||||
|
|
@ -169,8 +203,15 @@ class AllocineBrowser(BaseBrowser):
|
||||||
return movie
|
return movie
|
||||||
|
|
||||||
def get_person(self, id):
|
def get_person(self, id):
|
||||||
res = self.readurl(
|
params = [('partner', self.PARTNER_KEY),
|
||||||
'http://api.allocine.fr/rest/v3/person?partner=YW5kcm9pZC12M3M&profile=large&code=%s&mediafmt=mp4-lc&filter=movie&format=json&striptags=biography' % id)
|
('code', id),
|
||||||
|
('profile', 'large'),
|
||||||
|
('mediafmt', 'mp4-lc'),
|
||||||
|
('filter', 'movie'),
|
||||||
|
('striptags', 'biography,biographyshort'),
|
||||||
|
('format', 'json')]
|
||||||
|
|
||||||
|
res = self.__do_request('person', params)
|
||||||
if res is not None:
|
if res is not None:
|
||||||
jres = json.loads(res)
|
jres = json.loads(res)
|
||||||
if 'person' in jres:
|
if 'person' in jres:
|
||||||
|
|
@ -250,8 +291,15 @@ class AllocineBrowser(BaseBrowser):
|
||||||
return person
|
return person
|
||||||
|
|
||||||
def iter_movie_persons(self, movie_id, role_filter):
|
def iter_movie_persons(self, movie_id, role_filter):
|
||||||
res = self.readurl(
|
params = [('partner', self.PARTNER_KEY),
|
||||||
'http://api.allocine.fr/rest/v3/movie?partner=YW5kcm9pZC12M3M&code=%s&profile=large&mediafmt=mp4-lc&format=json&filter=movie&striptags=synopsis,synopsisshort' % movie_id)
|
('code', movie_id),
|
||||||
|
('profile', 'large'),
|
||||||
|
('mediafmt', 'mp4-lc'),
|
||||||
|
('filter', 'movie'),
|
||||||
|
('striptags', 'synopsis,synopsisshort'),
|
||||||
|
('format', 'json')]
|
||||||
|
|
||||||
|
res = self.__do_request('movie', params)
|
||||||
if res is not None:
|
if res is not None:
|
||||||
jres = json.loads(res)
|
jres = json.loads(res)
|
||||||
if 'movie' in jres:
|
if 'movie' in jres:
|
||||||
|
|
@ -285,8 +333,13 @@ class AllocineBrowser(BaseBrowser):
|
||||||
yield person
|
yield person
|
||||||
|
|
||||||
def iter_person_movies(self, person_id, role_filter):
|
def iter_person_movies(self, person_id, role_filter):
|
||||||
res = self.readurl(
|
params = [('partner', self.PARTNER_KEY),
|
||||||
'http://api.allocine.fr/rest/v3/filmography?partner=YW5kcm9pZC12M3M&profile=medium&code=%s&filter=movie&format=json' % person_id)
|
('code', person_id),
|
||||||
|
('profile', 'medium'),
|
||||||
|
('filter', 'movie'),
|
||||||
|
('format', 'json')]
|
||||||
|
|
||||||
|
res = self.__do_request('filmography', params)
|
||||||
if res is not None:
|
if res is not None:
|
||||||
jres = json.loads(res)
|
jres = json.loads(res)
|
||||||
if 'person' in jres:
|
if 'person' in jres:
|
||||||
|
|
@ -317,8 +370,13 @@ class AllocineBrowser(BaseBrowser):
|
||||||
yield movie
|
yield movie
|
||||||
|
|
||||||
def iter_person_movies_ids(self, person_id):
|
def iter_person_movies_ids(self, person_id):
|
||||||
res = self.readurl(
|
params = [('partner', self.PARTNER_KEY),
|
||||||
'http://api.allocine.fr/rest/v3/filmography?partner=YW5kcm9pZC12M3M&profile=medium&code=%s&filter=movie&format=json' % person_id)
|
('code', person_id),
|
||||||
|
('profile', 'medium'),
|
||||||
|
('filter', 'movie'),
|
||||||
|
('format', 'json')]
|
||||||
|
|
||||||
|
res = self.__do_request('filmography', params)
|
||||||
if res is not None:
|
if res is not None:
|
||||||
jres = json.loads(res)
|
jres = json.loads(res)
|
||||||
if 'person' in jres:
|
if 'person' in jres:
|
||||||
|
|
@ -331,8 +389,15 @@ class AllocineBrowser(BaseBrowser):
|
||||||
yield unicode(m['movie']['code'])
|
yield unicode(m['movie']['code'])
|
||||||
|
|
||||||
def iter_movie_persons_ids(self, movie_id):
|
def iter_movie_persons_ids(self, movie_id):
|
||||||
res = self.readurl(
|
params = [('partner', self.PARTNER_KEY),
|
||||||
'http://api.allocine.fr/rest/v3/movie?partner=YW5kcm9pZC12M3M&code=%s&profile=large&mediafmt=mp4-lc&format=json&filter=movie&striptags=synopsis,synopsisshort' % movie_id)
|
('code', movie_id),
|
||||||
|
('profile', 'large'),
|
||||||
|
('mediafmt', 'mp4-lc'),
|
||||||
|
('filter', 'movie'),
|
||||||
|
('striptags', 'synopsis,synopsisshort'),
|
||||||
|
('format', 'json')]
|
||||||
|
|
||||||
|
res = self.__do_request('movie', params)
|
||||||
if res is not None:
|
if res is not None:
|
||||||
jres = json.loads(res)
|
jres = json.loads(res)
|
||||||
if 'movie' in jres:
|
if 'movie' in jres:
|
||||||
|
|
@ -347,3 +412,28 @@ class AllocineBrowser(BaseBrowser):
|
||||||
|
|
||||||
def get_movie_releases(self, id, country):
|
def get_movie_releases(self, id, country):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def get_person_biography(self, id):
|
||||||
|
params = [('partner', self.PARTNER_KEY),
|
||||||
|
('code', id),
|
||||||
|
('profile', 'large'),
|
||||||
|
('mediafmt', 'mp4-lc'),
|
||||||
|
('filter', 'movie'),
|
||||||
|
('striptags', 'biography,biographyshort'),
|
||||||
|
('format', 'json')]
|
||||||
|
|
||||||
|
res = self.__do_request('person', params)
|
||||||
|
if res is not None:
|
||||||
|
jres = json.loads(res)
|
||||||
|
if 'person' in jres:
|
||||||
|
jres = jres['person']
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
biography = NotAvailable
|
||||||
|
if 'biography' in jres:
|
||||||
|
biography = unicode(jres['biography'])
|
||||||
|
|
||||||
|
return biography
|
||||||
|
|
|
||||||
|
|
@ -18,40 +18,69 @@
|
||||||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from weboob.tools.test import BackendTest
|
from weboob.tools.test import BackendTest
|
||||||
|
import re
|
||||||
|
|
||||||
class AllocineTest(BackendTest):
|
class AllocineTest(BackendTest):
|
||||||
BACKEND = 'allocine'
|
BACKEND = 'allocine'
|
||||||
|
|
||||||
def test_search_movie(self):
|
def test_search_movie(self):
|
||||||
movies = list(self.backend.iter_movies('spiderman'))
|
movies = list(self.backend.iter_movies('spiderman'))
|
||||||
|
assert len(movies) > 0
|
||||||
for movie in movies:
|
for movie in movies:
|
||||||
assert movie.id
|
assert movie.id
|
||||||
|
|
||||||
def test_get_movie(self):
|
def test_get_movie(self):
|
||||||
movie = self.backend.get_movie('5032')
|
movie = self.backend.get_movie('5032')
|
||||||
|
assert movie
|
||||||
assert movie.id
|
assert movie.id
|
||||||
assert movie.original_title
|
assert movie.original_title
|
||||||
|
|
||||||
def test_search_person(self):
|
def test_search_person(self):
|
||||||
persons = list(self.backend.iter_persons('dewaere'))
|
persons = list(self.backend.iter_persons('dewaere'))
|
||||||
|
assert len(persons) > 0
|
||||||
for person in persons:
|
for person in persons:
|
||||||
assert person.id
|
assert person.id
|
||||||
|
|
||||||
def test_get_person(self):
|
def test_get_person(self):
|
||||||
person = self.backend.get_person('1116')
|
person = self.backend.get_person('1115')
|
||||||
|
assert person
|
||||||
assert person.id
|
assert person.id
|
||||||
assert person.name
|
assert person.name
|
||||||
assert person.birth_date
|
assert person.birth_date
|
||||||
|
|
||||||
def test_movie_persons(self):
|
def test_movie_persons(self):
|
||||||
persons = list(self.backend.iter_movie_persons('5032'))
|
persons = list(self.backend.iter_movie_persons('5032'))
|
||||||
|
assert len(persons) > 0
|
||||||
for person in persons:
|
for person in persons:
|
||||||
assert person.id
|
assert person.id
|
||||||
assert person.name
|
assert person.name
|
||||||
|
|
||||||
def test_person_movies(self):
|
def test_person_movies(self):
|
||||||
movies = list(self.backend.iter_person_movies('1115'))
|
movies = list(self.backend.iter_person_movies('1115'))
|
||||||
|
assert len(movies) > 0
|
||||||
for movie in movies:
|
for movie in movies:
|
||||||
assert movie.id
|
assert movie.id
|
||||||
assert movie.original_title
|
assert movie.original_title
|
||||||
|
|
||||||
|
def test_get_person_biography(self):
|
||||||
|
bio = self.backend.get_person_biography('1115')
|
||||||
|
assert bio != ''
|
||||||
|
assert bio is not None
|
||||||
|
assert re.match(r'^Patrick Dewaere, fils.*', bio)
|
||||||
|
|
||||||
|
def test_get_movie_releases(self):
|
||||||
|
rel = self.backend.get_movie_releases('5032', 'fr')
|
||||||
|
assert rel != ''
|
||||||
|
assert rel is not None
|
||||||
|
|
||||||
|
def test_iter_person_movies_ids(self):
|
||||||
|
movies_ids = list(self.backend.iter_person_movies_ids('1115'))
|
||||||
|
assert len(movies_ids) > 0
|
||||||
|
for movie_id in movies_ids:
|
||||||
|
assert movie_id
|
||||||
|
|
||||||
|
def test_iter_movie_persons_ids(self):
|
||||||
|
persons_ids = list(self.backend.iter_movie_persons_ids('5032'))
|
||||||
|
assert len(persons_ids) > 0
|
||||||
|
for person_id in persons_ids:
|
||||||
|
assert person_id
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue