trackma 0.4__py2.7.egg
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- EGG-INFO/PKG-INFO +19 -0
- EGG-INFO/SOURCES.txt +35 -0
- EGG-INFO/dependency_links.txt +1 -0
- EGG-INFO/entry_points.txt +8 -0
- EGG-INFO/not-zip-safe +1 -0
- EGG-INFO/requires.txt +10 -0
- EGG-INFO/top_level.txt +1 -0
- trackma/__init__.py +0 -0
- trackma/__init__.pyc +0 -0
- trackma/accounts.py +110 -0
- trackma/accounts.pyc +0 -0
- trackma/data/anilist.jpg +0 -0
- trackma/data/hb.jpg +0 -0
- trackma/data/icon.png +0 -0
- trackma/data/mal.jpg +0 -0
- trackma/data/melative.jpg +0 -0
- trackma/data/vndb.jpg +0 -0
- trackma/data.py +585 -0
- trackma/data.pyc +0 -0
- trackma/engine.py +742 -0
- trackma/engine.pyc +0 -0
- trackma/extras/AnimeInfoExtractor.py +308 -0
- trackma/extras/AnimeInfoExtractor.pyc +0 -0
- trackma/extras/__init__.py +0 -0
- trackma/extras/__init__.pyc +0 -0
- trackma/extras/plex.py +100 -0
- trackma/extras/plex.pyc +0 -0
- trackma/lib/__init__.py +0 -0
- trackma/lib/__init__.pyc +0 -0
- trackma/lib/lib.py +162 -0
- trackma/lib/lib.pyc +0 -0
- trackma/lib/libanilist.py +355 -0
- trackma/lib/libanilist.pyc +0 -0
- trackma/lib/libhb.py +248 -0
- trackma/lib/libhb.pyc +0 -0
- trackma/lib/libmal.py +705 -0
- trackma/lib/libmal.pyc +0 -0
- trackma/lib/libmelative.py +213 -0
- trackma/lib/libmelative.pyc +0 -0
- trackma/lib/libvndb.py +373 -0
- trackma/lib/libvndb.pyc +0 -0
- trackma/messenger.py +42 -0
- trackma/messenger.pyc +0 -0
- trackma/tracker.py +301 -0
- trackma/tracker.pyc +0 -0
- trackma/ui/__init__.py +0 -0
- trackma/ui/__init__.pyc +0 -0
- trackma/ui/cli.py +712 -0
- trackma/ui/cli.pyc +0 -0
- trackma/ui/curses.py +1049 -0
- trackma/ui/curses.pyc +0 -0
- trackma/ui/gtkui.py +2360 -0
- trackma/ui/gtkui.pyc +0 -0
- trackma/ui/qtui.py +2120 -0
- trackma/ui/qtui.pyc +0 -0
- trackma/utils.py +299 -0
- trackma/utils.pyc +0 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
# This file is part of Trackma.
|
|
2
|
+
#
|
|
3
|
+
# This program is free software: you can redistribute it and/or modify
|
|
4
|
+
# it under the terms of the GNU General Public License as published by
|
|
5
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
6
|
+
# (at your option) any later version.
|
|
7
|
+
#
|
|
8
|
+
# This program is distributed in the hope that it will be useful,
|
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11
|
+
# GNU General Public License for more details.
|
|
12
|
+
#
|
|
13
|
+
# You should have received a copy of the GNU General Public License
|
|
14
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
15
|
+
#
|
|
16
|
+
|
|
17
|
+
from trackma.lib.lib import lib
|
|
18
|
+
import trackma.utils as utils
|
|
19
|
+
|
|
20
|
+
import json
|
|
21
|
+
import urllib, urllib2, socket
|
|
22
|
+
import time
|
|
23
|
+
|
|
24
|
+
class libanilist(lib):
|
|
25
|
+
"""
|
|
26
|
+
API class to communicate with Anilist
|
|
27
|
+
|
|
28
|
+
Website: http://anilist.co
|
|
29
|
+
|
|
30
|
+
messenger: Messenger object to send useful messages to
|
|
31
|
+
"""
|
|
32
|
+
name = 'libanilist'
|
|
33
|
+
msg = None
|
|
34
|
+
logged_in = False
|
|
35
|
+
|
|
36
|
+
api_info = { 'name': 'Anilist', 'version': '1', 'merge': False }
|
|
37
|
+
mediatypes = dict()
|
|
38
|
+
mediatypes['anime'] = {
|
|
39
|
+
'has_progress': True,
|
|
40
|
+
'can_add': True,
|
|
41
|
+
'can_delete': True,
|
|
42
|
+
'can_score': True,
|
|
43
|
+
'can_status': True,
|
|
44
|
+
'can_update': True,
|
|
45
|
+
'can_play': True,
|
|
46
|
+
'status_start': 'watching',
|
|
47
|
+
'status_finish': 'completed',
|
|
48
|
+
'statuses': ['watching', 'completed', 'on-hold', 'dropped', 'plan to watch'],
|
|
49
|
+
'statuses_dict': {
|
|
50
|
+
'watching': 'Watching',
|
|
51
|
+
'completed': 'Completed',
|
|
52
|
+
'on-hold': 'On Hold',
|
|
53
|
+
'dropped': 'Dropped',
|
|
54
|
+
'plan to watch': 'Plan to Watch'
|
|
55
|
+
},
|
|
56
|
+
'score_max': 100,
|
|
57
|
+
'score_step': 1,
|
|
58
|
+
}
|
|
59
|
+
mediatypes['manga'] = {
|
|
60
|
+
'has_progress': True,
|
|
61
|
+
'can_add': True,
|
|
62
|
+
'can_delete': True,
|
|
63
|
+
'can_score': True,
|
|
64
|
+
'can_status': True,
|
|
65
|
+
'can_update': True,
|
|
66
|
+
'can_play': False,
|
|
67
|
+
'status_start': 'reading',
|
|
68
|
+
'status_finish': 'completed',
|
|
69
|
+
'statuses': ['reading', 'completed', 'on-hold', 'dropped', 'plan to read'],
|
|
70
|
+
'statuses_dict': {
|
|
71
|
+
'reading': 'Reading',
|
|
72
|
+
'completed': 'Completed',
|
|
73
|
+
'on-hold': 'On Hold',
|
|
74
|
+
'dropped': 'Dropped',
|
|
75
|
+
'plan to read': 'Plan to Read'
|
|
76
|
+
},
|
|
77
|
+
'score_max': 100,
|
|
78
|
+
'score_step': 1,
|
|
79
|
+
}
|
|
80
|
+
default_mediatype = 'anime'
|
|
81
|
+
|
|
82
|
+
# Supported signals for the data handler
|
|
83
|
+
signals = { 'show_info_changed': None, }
|
|
84
|
+
|
|
85
|
+
url = "http://anilist.co/api/"
|
|
86
|
+
client_id = "z411-gdjc3"
|
|
87
|
+
_client_secret = "MyzwuYoMqPPglXwCTcexG1i"
|
|
88
|
+
|
|
89
|
+
def __init__(self, messenger, account, userconfig):
|
|
90
|
+
"""Initializes the API"""
|
|
91
|
+
super(libanilist, self).__init__(messenger, account, userconfig)
|
|
92
|
+
|
|
93
|
+
self.pin = account['password'].strip()
|
|
94
|
+
self.userid = userconfig['userid']
|
|
95
|
+
|
|
96
|
+
if len(self.pin) != 40:
|
|
97
|
+
raise utils.APIFatal("Invalid PIN.")
|
|
98
|
+
|
|
99
|
+
if self.mediatype == 'manga':
|
|
100
|
+
self.total_str = "total_chapters"
|
|
101
|
+
self.watched_str = "chapters_read"
|
|
102
|
+
self.airing_str = "publishing_status"
|
|
103
|
+
self.status_translate = {
|
|
104
|
+
'publishing': utils.STATUS_AIRING,
|
|
105
|
+
'finished': utils.STATUS_FINISHED,
|
|
106
|
+
'not yet published': utils.STATUS_NOTYET,
|
|
107
|
+
'cancelled': utils.STATUS_CANCELLED,
|
|
108
|
+
}
|
|
109
|
+
else:
|
|
110
|
+
self.total_str = "total_episodes"
|
|
111
|
+
self.watched_str = "episodes_watched"
|
|
112
|
+
self.airing_str = "airing_status"
|
|
113
|
+
self.status_translate = {
|
|
114
|
+
'currently airing': utils.STATUS_AIRING,
|
|
115
|
+
'finished airing': utils.STATUS_FINISHED,
|
|
116
|
+
'not yet aired': utils.STATUS_NOTYET,
|
|
117
|
+
'cancelled': utils.STATUS_CANCELLED,
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
#handler=urllib2.HTTPHandler(debuglevel=1)
|
|
121
|
+
#self.opener = urllib2.build_opener(handler)
|
|
122
|
+
self.opener = urllib2.build_opener()
|
|
123
|
+
self.opener.addheaders = [('User-agent', 'Trackma/0.1')]
|
|
124
|
+
|
|
125
|
+
def _request(self, method, url, get=None, post=None, auth=False):
|
|
126
|
+
if get:
|
|
127
|
+
url = "{}?{}".format(url, urllib.urlencode(get))
|
|
128
|
+
if post:
|
|
129
|
+
post = urllib.urlencode(post)
|
|
130
|
+
|
|
131
|
+
request = urllib2.Request(self.url + url, post)
|
|
132
|
+
request.get_method = lambda: method
|
|
133
|
+
|
|
134
|
+
if auth:
|
|
135
|
+
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
|
|
136
|
+
request.add_header('Authorization', '{0} {1}'.format(
|
|
137
|
+
self._get_userconfig('token_type').capitalize(),
|
|
138
|
+
self._get_userconfig('access_token'),
|
|
139
|
+
))
|
|
140
|
+
|
|
141
|
+
try:
|
|
142
|
+
response = self.opener.open(request, timeout = 10)
|
|
143
|
+
return json.load(response)
|
|
144
|
+
except urllib2.HTTPError, e:
|
|
145
|
+
if e.code == 400:
|
|
146
|
+
raise utils.APIError("Invalid PIN. It is either probably expired or meant for another application.")
|
|
147
|
+
else:
|
|
148
|
+
raise utils.APIError("Connection error: %s" % e)
|
|
149
|
+
except socket.timeout:
|
|
150
|
+
raise utils.APIError("Connection timed out.")
|
|
151
|
+
|
|
152
|
+
def _request_access_token(self):
|
|
153
|
+
self.msg.info(self.name, 'Requesting access token...')
|
|
154
|
+
param = {
|
|
155
|
+
'grant_type': 'authorization_pin',
|
|
156
|
+
'client_id': self.client_id,
|
|
157
|
+
'client_secret': self._client_secret,
|
|
158
|
+
'code': self.pin,
|
|
159
|
+
}
|
|
160
|
+
data = self._request("POST", "auth/access_token", get=param)
|
|
161
|
+
|
|
162
|
+
self._set_userconfig('access_token', data['access_token'])
|
|
163
|
+
self._set_userconfig('token_type', data['token_type'])
|
|
164
|
+
self._set_userconfig('expires', data['expires'])
|
|
165
|
+
self._set_userconfig('refresh_token', data['refresh_token'])
|
|
166
|
+
|
|
167
|
+
self.logged_in = True
|
|
168
|
+
self._refresh_user_info()
|
|
169
|
+
self._emit_signal('userconfig_changed')
|
|
170
|
+
|
|
171
|
+
def _refresh_access_token(self):
|
|
172
|
+
self.msg.info(self.name, 'Refreshing access token...')
|
|
173
|
+
param = {
|
|
174
|
+
'grant_type': 'refresh_token',
|
|
175
|
+
'client_id': self.client_id,
|
|
176
|
+
'client_secret': self._client_secret,
|
|
177
|
+
'refresh_token': self._get_userconfig('refresh_token'),
|
|
178
|
+
}
|
|
179
|
+
data = self._request("POST", "auth/access_token", get=param)
|
|
180
|
+
|
|
181
|
+
self._set_userconfig('access_token', data['access_token'])
|
|
182
|
+
self._set_userconfig('token_type', data['token_type'])
|
|
183
|
+
self._set_userconfig('expires', data['expires'])
|
|
184
|
+
|
|
185
|
+
self.logged_in = True
|
|
186
|
+
self._refresh_user_info()
|
|
187
|
+
self._emit_signal('userconfig_changed')
|
|
188
|
+
|
|
189
|
+
def _refresh_user_info(self):
|
|
190
|
+
self.msg.info(self.name, 'Refreshing user details...')
|
|
191
|
+
param = {'access_token': self._get_userconfig('access_token')}
|
|
192
|
+
|
|
193
|
+
data = self._request("GET", "user", get=param)
|
|
194
|
+
|
|
195
|
+
self._set_userconfig('userid', data['id'])
|
|
196
|
+
self._set_userconfig('username', data['display_name'])
|
|
197
|
+
|
|
198
|
+
self.userid = data['id']
|
|
199
|
+
|
|
200
|
+
def check_credentials(self):
|
|
201
|
+
"""
|
|
202
|
+
Log into Anilist. Since it uses OAuth, we either request an access token
|
|
203
|
+
or refresh the current one. If neither is necessary, just continue.
|
|
204
|
+
"""
|
|
205
|
+
timestamp = int(time.time())
|
|
206
|
+
|
|
207
|
+
if not self._get_userconfig('access_token'):
|
|
208
|
+
self._request_access_token()
|
|
209
|
+
elif (timestamp+60) > self._get_userconfig('expires'):
|
|
210
|
+
self._refresh_access_token()
|
|
211
|
+
else:
|
|
212
|
+
self.logged_in = True
|
|
213
|
+
return True
|
|
214
|
+
|
|
215
|
+
def fetch_list(self):
|
|
216
|
+
self.check_credentials()
|
|
217
|
+
self.msg.info(self.name, 'Downloading list...')
|
|
218
|
+
|
|
219
|
+
param = {'access_token': self._get_userconfig('access_token')}
|
|
220
|
+
data = self._request("GET", "user/{0}/{1}list".format(self.userid, self.mediatype), get=param)
|
|
221
|
+
|
|
222
|
+
showlist = {}
|
|
223
|
+
|
|
224
|
+
#with open('list', 'w') as f:
|
|
225
|
+
# json.dump(data, f, indent=2)
|
|
226
|
+
|
|
227
|
+
if not data["lists"]:
|
|
228
|
+
# No lists returned so no need to continue
|
|
229
|
+
return showlist
|
|
230
|
+
|
|
231
|
+
for remotelist in data["lists"].itervalues():
|
|
232
|
+
for item in remotelist:
|
|
233
|
+
if item['list_status'] not in self.media_info()['statuses']:
|
|
234
|
+
continue
|
|
235
|
+
|
|
236
|
+
show = utils.show()
|
|
237
|
+
showid = item[self.mediatype]['id']
|
|
238
|
+
show.update({
|
|
239
|
+
'id': showid,
|
|
240
|
+
'title': item[self.mediatype]['title_romaji'],
|
|
241
|
+
'aliases': [item[self.mediatype]['title_english']],
|
|
242
|
+
'type': item[self.mediatype]['type'],
|
|
243
|
+
'status': self.status_translate[item[self.mediatype][self.airing_str]],
|
|
244
|
+
'my_progress': self._c(item[self.watched_str]),
|
|
245
|
+
'my_status': item['list_status'],
|
|
246
|
+
'my_score': self._c(item['score']),
|
|
247
|
+
'total': self._c(item[self.mediatype][self.total_str]),
|
|
248
|
+
'image': item[self.mediatype]['image_url_lge'],
|
|
249
|
+
'image_thumb': item[self.mediatype]['image_url_med'],
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
showlist[showid] = show
|
|
253
|
+
|
|
254
|
+
return showlist
|
|
255
|
+
|
|
256
|
+
def add_show(self, item):
|
|
257
|
+
self.check_credentials()
|
|
258
|
+
self.msg.info(self.name, "Adding item %s..." % item['title'])
|
|
259
|
+
return self._update_entry(item, "POST")
|
|
260
|
+
|
|
261
|
+
def update_show(self, item):
|
|
262
|
+
self.check_credentials()
|
|
263
|
+
self.msg.info(self.name, "Updating item %s..." % item['title'])
|
|
264
|
+
return self._update_entry(item, "PUT")
|
|
265
|
+
|
|
266
|
+
def delete_show(self, item):
|
|
267
|
+
self.check_credentials()
|
|
268
|
+
self.msg.info(self.name, "Deleting item %s..." % item['title'])
|
|
269
|
+
|
|
270
|
+
data = self._request("DELETE", "{}list/{}".format(self.mediatype, item['id']), auth=True)
|
|
271
|
+
return True
|
|
272
|
+
|
|
273
|
+
def search(self, criteria):
|
|
274
|
+
self.check_credentials()
|
|
275
|
+
|
|
276
|
+
self.msg.info(self.name, "Searching for {}...".format(criteria))
|
|
277
|
+
param = {'access_token': self._get_userconfig('access_token')}
|
|
278
|
+
try:
|
|
279
|
+
data = self._request("GET", "{0}/search/{1}".format(self.mediatype, criteria), get=param)
|
|
280
|
+
except ValueError:
|
|
281
|
+
# An empty document, without any JSON, is returned
|
|
282
|
+
# when there are no results.
|
|
283
|
+
return []
|
|
284
|
+
|
|
285
|
+
showlist = []
|
|
286
|
+
|
|
287
|
+
for item in data:
|
|
288
|
+
show = utils.show()
|
|
289
|
+
showid = item['id']
|
|
290
|
+
show.update({
|
|
291
|
+
'id': showid,
|
|
292
|
+
'title': item['title_romaji'],
|
|
293
|
+
'aliases': [item['title_english']],
|
|
294
|
+
'type': item['type'],
|
|
295
|
+
'status': item[self.airing_str],
|
|
296
|
+
'my_status': self.media_info()['status_start'],
|
|
297
|
+
'total': item[self.total_str],
|
|
298
|
+
'image': item['image_url_lge'],
|
|
299
|
+
'image_thumb': item['image_url_med'],
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
showlist.append( show )
|
|
303
|
+
|
|
304
|
+
return showlist
|
|
305
|
+
|
|
306
|
+
def request_info(self, itemlist):
|
|
307
|
+
self.check_credentials()
|
|
308
|
+
param = {'access_token': self._get_userconfig('access_token')}
|
|
309
|
+
infolist = []
|
|
310
|
+
|
|
311
|
+
for show in itemlist:
|
|
312
|
+
data = self._request("GET", "{0}/{1}".format(self.mediatype, show['id']), get=param)
|
|
313
|
+
infolist.append( self._parse_info(data) )
|
|
314
|
+
|
|
315
|
+
self._emit_signal('show_info_changed', infolist)
|
|
316
|
+
return infolist
|
|
317
|
+
|
|
318
|
+
def media_info(self):
|
|
319
|
+
"""Return information about the currently selected mediatype."""
|
|
320
|
+
return self.mediatypes[self.mediatype]
|
|
321
|
+
|
|
322
|
+
def _update_entry(self, item, method):
|
|
323
|
+
values = { 'id': item['id'] }
|
|
324
|
+
if 'my_progress' in item.keys():
|
|
325
|
+
values[self.watched_str] = item['my_progress']
|
|
326
|
+
if 'my_status' in item.keys():
|
|
327
|
+
values['list_status'] = item['my_status']
|
|
328
|
+
if 'my_score' in item.keys():
|
|
329
|
+
values['score'] = item['my_score']
|
|
330
|
+
|
|
331
|
+
data = self._request(method, "{}list".format(self.mediatype), post=values, auth=True)
|
|
332
|
+
return True
|
|
333
|
+
|
|
334
|
+
def _parse_info(self, item):
|
|
335
|
+
info = utils.show()
|
|
336
|
+
info.update({
|
|
337
|
+
'id': item['id'],
|
|
338
|
+
'title': item['title_romaji'],
|
|
339
|
+
'status': self.status_translate[item[self.airing_str]],
|
|
340
|
+
'image': item['image_url_lge'],
|
|
341
|
+
'extra': [
|
|
342
|
+
('Description', item.get('description')),
|
|
343
|
+
('Genres', item.get('genres')),
|
|
344
|
+
('Classification', item.get('classification')),
|
|
345
|
+
('Status', item.get(self.airing_str)),
|
|
346
|
+
('Average score', item.get('average_score')),
|
|
347
|
+
('Japanese title', item.get('title_japanese')),
|
|
348
|
+
('English title', item.get('title_english')),
|
|
349
|
+
]
|
|
350
|
+
})
|
|
351
|
+
return info
|
|
352
|
+
|
|
353
|
+
def _c(self, s):
|
|
354
|
+
return 0 if s is None else s
|
|
355
|
+
|
|
Binary file
|
trackma/lib/libhb.py
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# This file is part of Trackma.
|
|
3
|
+
#
|
|
4
|
+
# This program is free software: you can redistribute it and/or modify
|
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
|
6
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
# (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
# GNU General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU General Public License
|
|
15
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
+
#
|
|
17
|
+
|
|
18
|
+
from trackma.lib.lib import lib
|
|
19
|
+
import trackma.utils as utils
|
|
20
|
+
import datetime
|
|
21
|
+
|
|
22
|
+
import urllib, urllib2
|
|
23
|
+
import json
|
|
24
|
+
|
|
25
|
+
class libhb(lib):
|
|
26
|
+
"""
|
|
27
|
+
API class to communicate with Hummingbird
|
|
28
|
+
Should inherit a base library interface.
|
|
29
|
+
|
|
30
|
+
Website: http://hummingbird.me/
|
|
31
|
+
API documentation:
|
|
32
|
+
Designed by:
|
|
33
|
+
|
|
34
|
+
"""
|
|
35
|
+
name = 'libhb'
|
|
36
|
+
|
|
37
|
+
username = '' # TODO Must be filled by check_credentials
|
|
38
|
+
auth = ''
|
|
39
|
+
logged_in = False
|
|
40
|
+
|
|
41
|
+
api_info = { 'name': 'Hummingbird', 'version': 'v0.2', 'merge': False }
|
|
42
|
+
|
|
43
|
+
default_mediatype = 'anime'
|
|
44
|
+
mediatypes = dict()
|
|
45
|
+
mediatypes['anime'] = {
|
|
46
|
+
'has_progress': True,
|
|
47
|
+
'can_add': True,
|
|
48
|
+
'can_delete': True,
|
|
49
|
+
'can_score': True,
|
|
50
|
+
'can_status': True,
|
|
51
|
+
'can_update': True,
|
|
52
|
+
'can_play': True,
|
|
53
|
+
'status_start': 'currently-watching',
|
|
54
|
+
'status_finish': 'completed',
|
|
55
|
+
'statuses': ['currently-watching', 'completed', 'on-hold', 'dropped', 'plan-to-watch'],
|
|
56
|
+
'statuses_dict': { 'currently-watching': 'Watching', 'completed': 'Completed', 'on-hold': 'On Hold', 'dropped': 'Dropped', 'plan-to-watch': 'Plan to Watch' },
|
|
57
|
+
'score_max': 5,
|
|
58
|
+
'score_step': 0.5,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
url = "http://hummingbird.me/api/v1"
|
|
62
|
+
|
|
63
|
+
status_translate = {'Currently Airing': utils.STATUS_AIRING,
|
|
64
|
+
'Finished Airing': utils.STATUS_FINISHED,
|
|
65
|
+
'Not Yet Aired': utils.STATUS_NOTYET}
|
|
66
|
+
|
|
67
|
+
def __init__(self, messenger, account, userconfig):
|
|
68
|
+
"""Initializes the useragent through credentials."""
|
|
69
|
+
super(libhb, self).__init__(messenger, account, userconfig)
|
|
70
|
+
|
|
71
|
+
self.username = account['username']
|
|
72
|
+
self.password = account['password']
|
|
73
|
+
|
|
74
|
+
# Build opener with the mashape API key
|
|
75
|
+
self.opener = urllib2.build_opener()
|
|
76
|
+
|
|
77
|
+
def _request(self, url, get=None, post=None):
|
|
78
|
+
if get:
|
|
79
|
+
url += "?%s" % urllib.urlencode(get)
|
|
80
|
+
if post:
|
|
81
|
+
post = urllib.urlencode(post)
|
|
82
|
+
|
|
83
|
+
try:
|
|
84
|
+
return self.opener.open(self.url + url, post, 10)
|
|
85
|
+
except urllib2.HTTPError, e:
|
|
86
|
+
if e.code == 401:
|
|
87
|
+
raise utils.APIError("Incorrect credentials.")
|
|
88
|
+
else:
|
|
89
|
+
raise utils.APIError("Connection error: %s" % e)
|
|
90
|
+
|
|
91
|
+
def check_credentials(self):
|
|
92
|
+
"""Checks if credentials are correct; returns True or False."""
|
|
93
|
+
if self.logged_in:
|
|
94
|
+
return True # Already logged in
|
|
95
|
+
|
|
96
|
+
self.msg.info(self.name, 'Logging in...')
|
|
97
|
+
|
|
98
|
+
response = self._request( "/users/authenticate", post={'username': self.username, 'password': self.password} ).read()
|
|
99
|
+
self.auth = response.strip('"')
|
|
100
|
+
self._set_userconfig('username', self.username)
|
|
101
|
+
self.logged_in = True
|
|
102
|
+
return True
|
|
103
|
+
|
|
104
|
+
def fetch_list(self):
|
|
105
|
+
"""Queries the full list from the remote server.
|
|
106
|
+
Returns the list if successful, False otherwise."""
|
|
107
|
+
self.check_credentials()
|
|
108
|
+
self.msg.info(self.name, 'Downloading list...')
|
|
109
|
+
|
|
110
|
+
try:
|
|
111
|
+
data = self._request( "/users/%s/library" % self.username, get={'auth_token': self.auth} )
|
|
112
|
+
shows = json.load(data)
|
|
113
|
+
|
|
114
|
+
showlist = dict()
|
|
115
|
+
infolist = list()
|
|
116
|
+
|
|
117
|
+
for show in shows:
|
|
118
|
+
showid = show['anime']['id']
|
|
119
|
+
status = show['anime']['status']
|
|
120
|
+
rating = show['rating']['value']
|
|
121
|
+
epCount = show['anime']['episode_count']
|
|
122
|
+
alt_titles = []
|
|
123
|
+
|
|
124
|
+
if show['anime']['alternate_title'] is not None:
|
|
125
|
+
alt_titles.append(show['anime']['alternate_title'])
|
|
126
|
+
showlist[showid] = utils.show()
|
|
127
|
+
showlist[showid].update({
|
|
128
|
+
'id': showid,
|
|
129
|
+
'title': show['anime']['title'] or show['anime']['alternate_title'] or "",
|
|
130
|
+
'status': self.status_translate[status],
|
|
131
|
+
'start_date': self._str2date( show['anime']['started_airing'] ),
|
|
132
|
+
'end_date': self._str2date( show['anime']['finished_airing'] ),
|
|
133
|
+
'my_progress': show['episodes_watched'],
|
|
134
|
+
'my_score': float(rating) if rating is not None else 0.0,
|
|
135
|
+
'aliases': alt_titles,
|
|
136
|
+
'my_status': show['status'],
|
|
137
|
+
'total': int(epCount) if epCount is not None else 0,
|
|
138
|
+
'image': show['anime']['cover_image'],
|
|
139
|
+
})
|
|
140
|
+
info = self._parse_info(show['anime'])
|
|
141
|
+
infolist.append(info)
|
|
142
|
+
|
|
143
|
+
self._emit_signal('show_info_changed', infolist)
|
|
144
|
+
return showlist
|
|
145
|
+
except urllib2.HTTPError, e:
|
|
146
|
+
raise utils.APIError("Error getting list.")
|
|
147
|
+
|
|
148
|
+
def add_show(self, item):
|
|
149
|
+
"""Adds a new show in the server"""
|
|
150
|
+
self.update_show(item)
|
|
151
|
+
|
|
152
|
+
def update_show(self, item):
|
|
153
|
+
"""Sends a show update to the server"""
|
|
154
|
+
self.check_credentials()
|
|
155
|
+
self.msg.info(self.name, "Updating show %s..." % item['title'])
|
|
156
|
+
|
|
157
|
+
# Send the POST data to the Hummingbird API
|
|
158
|
+
values = {'auth_token': self.auth}
|
|
159
|
+
|
|
160
|
+
# Update necessary keys
|
|
161
|
+
if 'my_progress' in item.keys():
|
|
162
|
+
values['episodes_watched'] = item['my_progress']
|
|
163
|
+
if 'my_status' in item.keys():
|
|
164
|
+
values['status'] = item['my_status']
|
|
165
|
+
if 'my_score' in item.keys():
|
|
166
|
+
values['sane_rating_update'] = item['my_score']
|
|
167
|
+
|
|
168
|
+
try:
|
|
169
|
+
response = self._request("/libraries/%s" % item['id'], post=values)
|
|
170
|
+
return True
|
|
171
|
+
except urllib2.HTTPError, e:
|
|
172
|
+
raise utils.APIError('Error updating: ' + str(e.code))
|
|
173
|
+
|
|
174
|
+
def delete_show(self, item):
|
|
175
|
+
"""Sends a show delete to the server"""
|
|
176
|
+
self.check_credentials()
|
|
177
|
+
self.msg.info(self.name, "Deleting show %s..." % item['title'])
|
|
178
|
+
|
|
179
|
+
values = {'auth_token': self.auth}
|
|
180
|
+
try:
|
|
181
|
+
response = self._request("/libraries/%s/remove" % item['id'], post=values)
|
|
182
|
+
return True
|
|
183
|
+
except urllib2.HTTPError, e:
|
|
184
|
+
raise utils.APIError('Error deleting: ' + str(e.code))
|
|
185
|
+
|
|
186
|
+
def search(self, query):
|
|
187
|
+
self.msg.info(self.name, "Searching for %s..." % query)
|
|
188
|
+
|
|
189
|
+
values = {'query': query}
|
|
190
|
+
try:
|
|
191
|
+
data = self._request("/search/anime", get=values)
|
|
192
|
+
shows = json.load(data)
|
|
193
|
+
|
|
194
|
+
infolist = []
|
|
195
|
+
for show in shows:
|
|
196
|
+
info = self._parse_info(show)
|
|
197
|
+
info['my_status'] = 'currently-watching' # TODO : Default to watching; this should be changeable
|
|
198
|
+
infolist.append(info)
|
|
199
|
+
|
|
200
|
+
self._emit_signal('show_info_changed', infolist)
|
|
201
|
+
|
|
202
|
+
if not infolist:
|
|
203
|
+
raise utils.APIError('No results.')
|
|
204
|
+
|
|
205
|
+
return infolist
|
|
206
|
+
except urllib2.HTTPError, e:
|
|
207
|
+
raise utils.APIError('Error searching: ' + str(e.code))
|
|
208
|
+
|
|
209
|
+
def _str2date(self, string):
|
|
210
|
+
if string != '0000-00-00':
|
|
211
|
+
try:
|
|
212
|
+
return datetime.datetime.strptime(string, "%Y-%m-%d")
|
|
213
|
+
except:
|
|
214
|
+
return None # Ignore date if it's invalid
|
|
215
|
+
else:
|
|
216
|
+
return None
|
|
217
|
+
|
|
218
|
+
def _parse_info(self, show):
|
|
219
|
+
info = utils.show()
|
|
220
|
+
alt_titles = []
|
|
221
|
+
if show['alternate_title'] is not None:
|
|
222
|
+
alt_titles.append(show['alternate_title'])
|
|
223
|
+
info.update({
|
|
224
|
+
'id': show['id'],
|
|
225
|
+
'title': show['title'] or show['alternate_title'] or "",
|
|
226
|
+
'status': self.status_translate[show['status']],
|
|
227
|
+
'image': show['cover_image'],
|
|
228
|
+
'url': show['url'],
|
|
229
|
+
'aliases': alt_titles,
|
|
230
|
+
'extra': [
|
|
231
|
+
('Alternate title', show['alternate_title']),
|
|
232
|
+
('Show type', show['show_type']),
|
|
233
|
+
('Synopsis', show['synopsis']),
|
|
234
|
+
('Status', show['status']),
|
|
235
|
+
]
|
|
236
|
+
})
|
|
237
|
+
return info
|
|
238
|
+
|
|
239
|
+
def _urlencode(self, in_dict):
|
|
240
|
+
"""Helper function to urlencode dicts in unicode. urllib doesn't like them."""
|
|
241
|
+
out_dict = {}
|
|
242
|
+
for k, v in in_dict.iteritems():
|
|
243
|
+
out_dict[k] = v
|
|
244
|
+
if isinstance(v, unicode):
|
|
245
|
+
out_dict[k] = v.encode('utf8')
|
|
246
|
+
elif isinstance(v, str):
|
|
247
|
+
out_dict[k] = v.decode('utf8')
|
|
248
|
+
return urllib.urlencode(out_dict)
|
trackma/lib/libhb.pyc
ADDED
|
Binary file
|