-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocine.py
208 lines (176 loc) · 6.69 KB
/
allocine.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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
"""
A module to use Allocine API V3 in Python
Repository: https://github.com/xbgmsharp/allocine
Base on work from: https://github.com/gromez/allocine-api
License: LGPLv2 http://www.gnu.org/licenses/lgpl.html
Sample code:
from allocine import allocine
api = allocine()
api.configure('100043982026','29d185d98c984a359e6e6f26a0474269')
movie = api.movie(27405)
search = api.search("Oblivion")
"""
# Debug
#from pprint import pprint
# standard module
from datetime import date
import urllib2, urllib
import hashlib, base64
import json as simplejson
__version__ = "0.2"
__author__ = "Francois Lacroix"
__license__ = "GPL"
__description__ = "A module to use Allocine API V3 in Python"
class allocine(object):
"""An interface to the Allocine API"""
def __init__(self, partner_key=None, secret_key=None):
"""Init values"""
self._api_url = 'http://api.allocine.fr/rest/v3'
self._partner_key = 'aXBob25lLXYy'
self._secret_key = secret_key
self._user_agent = 'AlloCine/2.9.5 CFNetwork/548.1.4 Darwin/11.0.0'
def configure(self, partner_key=None, secret_key=None):
"""Set the keys"""
self._partner_key = 'aXBob25lLXYy'
self._secret_key = secret_key
def _do_request(self, method=None, params=None):
"""Generate and send the request"""
# build the URL
query_url = self._api_url+'/'+method;
# new algo to build the query
today = date.today()
sed = today.strftime('%Y%m%d')
#print sed
sha1 = hashlib.sha1(self._secret_key+urllib.urlencode(params)+'&sed='+sed).digest()
#print sha1
b64 = base64.b64encode(sha1)
#print b64
sig = urllib2.quote(b64)
#query_url += '?'+urllib.urlencode(params)+'&sed='+sed+'&sig='+sig
query_url += '?'+urllib.urlencode(params, True)
#print query_url;
# do the request
req = urllib2.Request(query_url)
req.add_header('User-agent', self._user_agent)
response = simplejson.load(urllib2.urlopen(req, timeout = 10))
return response;
def search(self, query, filter="movie"):
"""Search for a term
Param:
query -- Term to search for
filter -- Filter by resut type (movie, theater, person, news, tvseries)
"""
# build the params
params = {}
params['format'] = 'json'
params['partner'] = self._partner_key
params['q'] = query
params['filter'] = filter
params['profile'] = "large"
# do the request
response = self._do_request('search', params);
return response;
def movie(self, id, profile="large", mediafmt="mp4-lc:m"):
"""Get the movie details by ID
Param:
id -- Unique ID of the movie your search for
profile -- Level of details to return (small, medium, large)
mediafmt -- The media format (flv, mp4-lc, mp4-hip, mp4-archive, mpeg2-theater, mpeg2)
"""
# build the params
params = {}
params['format'] = 'json'
params['partner'] = self._partner_key
params['mediafmt'] = mediafmt
params['profile'] = profile
params['code'] = id
params['striptags'] = 'synopsis,synopsisshort'
# do the request
response = self._do_request('movie', params);
return response;
def tvseries(self, id, profile="large", mediafmt="mp4-lc:m"):
"""Get the TVshow details by ID
Param:
id -- Unique ID of the tvseries your search for
profile -- Level of details to return (small, medium, large)
mediafmt -- The media format (flv, mp4-lc, mp4-hip, mp4-archive, mpeg2-theater, mpeg2)
"""
# build the params
params = {}
params['format'] = 'json'
params['partner'] = self._partner_key
params['mediafmt'] = mediafmt
params['profile'] = profile
params['code'] = id
params['striptags'] = 'synopsis,synopsisshort'
# do the request
response = self._do_request('tvseries', params);
return response;
def season(self, id, profile="large"):
"""Get the season details by ID
Param:
id -- Unique ID of the season your search for
profile -- Level of details to return (small, medium, large)
"""
# build the params
params = {}
params['format'] = 'json'
params['partner'] = self._partner_key
params['profile'] = profile
params['code'] = id
params['striptags'] = 'synopsis,synopsisshort'
# do the request
response = self._do_request('season', params);
return response;
def episode(self, id, profile="large"):
"""Get the episode details by ID
Param:
id -- Unique ID of the episode your search for
profile -- Level of details to return (small, medium, large)
"""
# build the params
params = {}
params['format'] = 'json'
params['partner'] = self._partner_key
params['profile'] = profile
params['code'] = id
params['striptags'] = 'synopsis,synopsisshort'
# do the request
response = self._do_request('episode', params);
return response;
def trailer(self, id, profile="large", mediafmt="mp4-lc:m"):
"""Get the movie details by ID
Param:
id -- Unique ID of the movie your search for
profile -- Level of details to return (small, medium, large)
mediafmt -- The media format (flv, mp4-lc, mp4-hip, mp4-archive, mpeg2-theater, mpeg2)
"""
# build the params
params = {}
params['format'] = 'json'
params['partner'] = self._partner_key
params['profile'] = profile
params['code'] = id
# do the request
response = self._do_request('media',params);
return response;
def movielist(self, typemovie, profile="large", mediafmt="mp4-lc:m"):
"""Get the movie details by ID
Param:
id -- Unique ID of the movie your search for
profile -- Level of details to return (small, medium, large)
mediafmt -- The media format (flv, mp4-lc, mp4-hip, mp4-archive, mpeg2-theater, mpeg2)
"""
# build the params
params = {}
params['format'] = 'json'
params['partner'] = self._partner_key
params['profile'] = profile
params['filter'] = typemovie
params['order'] = 'toprank'
params['count'] = 100
# do the request
response = self._do_request('movielist',params);
return response;