trackma 0.6__py3.5.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 +37 -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/__pycache__/__init__.cpython-35.pyc +0 -0
- trackma/__pycache__/accounts.cpython-35.pyc +0 -0
- trackma/__pycache__/data.cpython-35.pyc +0 -0
- trackma/__pycache__/engine.cpython-35.pyc +0 -0
- trackma/__pycache__/messenger.cpython-35.pyc +0 -0
- trackma/__pycache__/tracker.cpython-35.pyc +0 -0
- trackma/__pycache__/utils.cpython-35.pyc +0 -0
- trackma/accounts.py +145 -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/shikimori.jpg +0 -0
- trackma/data/vndb.jpg +0 -0
- trackma/data.py +597 -0
- trackma/engine.py +818 -0
- trackma/extras/AnimeInfoExtractor.py +308 -0
- trackma/extras/__init__.py +1 -0
- trackma/extras/__pycache__/AnimeInfoExtractor.cpython-35.pyc +0 -0
- trackma/extras/__pycache__/__init__.cpython-35.pyc +0 -0
- trackma/extras/__pycache__/plex.cpython-35.pyc +0 -0
- trackma/extras/plex.py +101 -0
- trackma/lib/__init__.py +0 -0
- trackma/lib/__pycache__/__init__.cpython-35.pyc +0 -0
- trackma/lib/__pycache__/lib.cpython-35.pyc +0 -0
- trackma/lib/__pycache__/libanilist.cpython-35.pyc +0 -0
- trackma/lib/__pycache__/libhb.cpython-35.pyc +0 -0
- trackma/lib/__pycache__/libmal.cpython-35.pyc +0 -0
- trackma/lib/__pycache__/libmelative.cpython-35.pyc +0 -0
- trackma/lib/__pycache__/libshikimori.cpython-35.pyc +0 -0
- trackma/lib/__pycache__/libvndb.cpython-35.pyc +0 -0
- trackma/lib/lib.py +162 -0
- trackma/lib/libanilist.py +406 -0
- trackma/lib/libhb.py +237 -0
- trackma/lib/libmal.py +707 -0
- trackma/lib/libmelative.py +204 -0
- trackma/lib/libshikimori.py +340 -0
- trackma/lib/libvndb.py +375 -0
- trackma/messenger.py +42 -0
- trackma/tracker.py +390 -0
- trackma/ui/__init__.py +0 -0
- trackma/ui/__pycache__/__init__.cpython-35.pyc +0 -0
- trackma/ui/__pycache__/cli.cpython-35.pyc +0 -0
- trackma/ui/__pycache__/curses.cpython-35.pyc +0 -0
- trackma/ui/__pycache__/gtkui.cpython-35.pyc +0 -0
- trackma/ui/__pycache__/qtui.cpython-35.pyc +0 -0
- trackma/ui/cli.py +835 -0
- trackma/ui/curses.py +1061 -0
- trackma/ui/gtkui.py +2647 -0
- trackma/ui/qtui.py +3046 -0
- trackma/utils.py +379 -0
trackma/lib/libmal.py
ADDED
|
@@ -0,0 +1,707 @@
|
|
|
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
|
+
import re
|
|
19
|
+
import urllib.parse
|
|
20
|
+
import urllib.request
|
|
21
|
+
import datetime
|
|
22
|
+
import base64
|
|
23
|
+
import gzip
|
|
24
|
+
import xml.etree.ElementTree as ET
|
|
25
|
+
|
|
26
|
+
from trackma.lib.lib import lib
|
|
27
|
+
from trackma import utils
|
|
28
|
+
|
|
29
|
+
class libmal(lib):
|
|
30
|
+
"""
|
|
31
|
+
API class to communicate with MyAnimeList
|
|
32
|
+
Should inherit a base library interface.
|
|
33
|
+
|
|
34
|
+
Website: http://www.myanimelist.net
|
|
35
|
+
API documentation: http://myanimelist.net/modules.php?go=api
|
|
36
|
+
Designed by: Garrett Gyssler (http://myanimelist.net/profile/Xinil)
|
|
37
|
+
|
|
38
|
+
"""
|
|
39
|
+
name = 'libmal'
|
|
40
|
+
|
|
41
|
+
username = '' # TODO Must be filled by check_credentials
|
|
42
|
+
logged_in = False
|
|
43
|
+
opener = None
|
|
44
|
+
|
|
45
|
+
api_info = { 'name': 'MyAnimeList', 'shortname': 'mal', 'version': 'v0.3', 'merge': False }
|
|
46
|
+
|
|
47
|
+
default_mediatype = 'anime'
|
|
48
|
+
mediatypes = dict()
|
|
49
|
+
mediatypes['anime'] = {
|
|
50
|
+
'has_progress': True,
|
|
51
|
+
'can_add': True,
|
|
52
|
+
'can_delete': True,
|
|
53
|
+
'can_score': True,
|
|
54
|
+
'can_status': True,
|
|
55
|
+
'can_tag': True,
|
|
56
|
+
'can_update': True,
|
|
57
|
+
'can_play': True,
|
|
58
|
+
'can_date': True,
|
|
59
|
+
'status_start': 1,
|
|
60
|
+
'status_finish': 2,
|
|
61
|
+
'statuses': [1, 2, 3, 4, 6],
|
|
62
|
+
'statuses_dict': { 1: 'Watching', 2: 'Completed', 3: 'On Hold', 4: 'Dropped', 6: 'Plan to Watch' },
|
|
63
|
+
'score_max': 10,
|
|
64
|
+
'score_step': 1,
|
|
65
|
+
}
|
|
66
|
+
mediatypes['manga'] = {
|
|
67
|
+
'has_progress': True,
|
|
68
|
+
'can_add': True,
|
|
69
|
+
'can_delete': True,
|
|
70
|
+
'can_score': True,
|
|
71
|
+
'can_status': True,
|
|
72
|
+
'can_tag': True,
|
|
73
|
+
'can_update': True,
|
|
74
|
+
'can_play': False,
|
|
75
|
+
'can_date': True,
|
|
76
|
+
'status_start': 1,
|
|
77
|
+
'status_finish': 2,
|
|
78
|
+
'statuses': [1, 2, 3, 4, 6],
|
|
79
|
+
'statuses_dict': { 1: 'Reading', 2: 'Completed', 3: 'On Hold', 4: 'Dropped', 6: 'Plan to Read' },
|
|
80
|
+
'score_max': 10,
|
|
81
|
+
'score_step': 1,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
# Authorized User-Agent for Trackma
|
|
85
|
+
url = 'http://myanimelist.net/api/'
|
|
86
|
+
useragent = 'api-team-f894427cc1c571f79da49605ef8b112f'
|
|
87
|
+
|
|
88
|
+
def __init__(self, messenger, account, userconfig):
|
|
89
|
+
"""Initializes the useragent through credentials."""
|
|
90
|
+
# Since MyAnimeList uses a cookie we just create a HTTP Auth handler
|
|
91
|
+
# together with the urllib opener.
|
|
92
|
+
super(libmal, self).__init__(messenger, account, userconfig)
|
|
93
|
+
|
|
94
|
+
token = '%s:%s' % (account['username'], account['password'])
|
|
95
|
+
auth_string = 'Basic ' + base64.b64encode(token.encode('utf-8')).decode('ascii').replace('\n', '')
|
|
96
|
+
|
|
97
|
+
self.username = self._get_userconfig('username')
|
|
98
|
+
self.opener = urllib.request.build_opener()
|
|
99
|
+
self.opener.addheaders = [
|
|
100
|
+
('User-Agent', self.useragent),
|
|
101
|
+
('Authorization', auth_string),
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
def _request(self, url):
|
|
105
|
+
"""
|
|
106
|
+
Requests the page as gzip and uncompresses it
|
|
107
|
+
|
|
108
|
+
Returns a stream object
|
|
109
|
+
|
|
110
|
+
"""
|
|
111
|
+
try:
|
|
112
|
+
request = urllib.request.Request(url)
|
|
113
|
+
request.add_header('Accept-Encoding', 'gzip')
|
|
114
|
+
response = self.opener.open(request, timeout = 10)
|
|
115
|
+
except urllib.request.HTTPError as e:
|
|
116
|
+
if e.code == 401:
|
|
117
|
+
raise utils.APIError(
|
|
118
|
+
"Unauthorized. Please check if your username and password are correct."
|
|
119
|
+
"\n\nPlease note that you might also be getting this error if you have "
|
|
120
|
+
"non-alphanumeric characters in your password due to an upstream "
|
|
121
|
+
"MAL bug (#138).")
|
|
122
|
+
else:
|
|
123
|
+
raise utils.APIError("HTTP error %d: %s" % (e.code, e.reason))
|
|
124
|
+
except urllib.request.URLError as e:
|
|
125
|
+
raise utils.APIError("Connection error: %s" % e)
|
|
126
|
+
|
|
127
|
+
if response.info().get('content-encoding') == 'gzip':
|
|
128
|
+
ret = gzip.decompress(response.read())
|
|
129
|
+
else:
|
|
130
|
+
# If the content is not gzipped return it as-is
|
|
131
|
+
ret = response.read()
|
|
132
|
+
if isinstance(ret, bytes):
|
|
133
|
+
return ret.decode('utf-8')
|
|
134
|
+
return ret
|
|
135
|
+
|
|
136
|
+
def check_credentials(self):
|
|
137
|
+
"""Checks if credentials are correct; returns True or False."""
|
|
138
|
+
if self.logged_in:
|
|
139
|
+
return True # Already logged in
|
|
140
|
+
|
|
141
|
+
self.msg.info(self.name, 'Logging in...')
|
|
142
|
+
|
|
143
|
+
response = self._request(self.url + "account/verify_credentials.xml")
|
|
144
|
+
root = self._parse_xml(response)
|
|
145
|
+
(userid, username) = self._parse_credentials(root)
|
|
146
|
+
self.username = username
|
|
147
|
+
|
|
148
|
+
self._set_userconfig('userid', userid)
|
|
149
|
+
self._set_userconfig('username', username)
|
|
150
|
+
self._emit_signal('userconfig_changed')
|
|
151
|
+
|
|
152
|
+
self.logged_in = True
|
|
153
|
+
return True
|
|
154
|
+
|
|
155
|
+
def fetch_list(self):
|
|
156
|
+
"""Queries the full list from the remote server.
|
|
157
|
+
Returns the list if successful, False otherwise."""
|
|
158
|
+
self.check_credentials()
|
|
159
|
+
self.msg.info(self.name, 'Downloading list...')
|
|
160
|
+
|
|
161
|
+
try:
|
|
162
|
+
# Get an XML list from MyAnimeList API
|
|
163
|
+
data = self._request("http://myanimelist.net/malappinfo.php?u="+self.username+"&status=all&type="+self.mediatype)
|
|
164
|
+
|
|
165
|
+
# Parse the XML data and load it into a dictionary
|
|
166
|
+
# using the proper function (anime or manga)
|
|
167
|
+
root = self._parse_xml(data)
|
|
168
|
+
|
|
169
|
+
if self.mediatype == 'anime':
|
|
170
|
+
self.msg.info(self.name, 'Parsing anime list...')
|
|
171
|
+
return self._parse_anime(root)
|
|
172
|
+
elif self.mediatype == 'manga':
|
|
173
|
+
self.msg.info(self.name, 'Parsing manga list...')
|
|
174
|
+
return self._parse_manga(root)
|
|
175
|
+
else:
|
|
176
|
+
raise utils.APIFatal('Attempted to parse unsupported media type.')
|
|
177
|
+
except urllib.request.HTTPError as e:
|
|
178
|
+
raise utils.APIError("Error getting list.")
|
|
179
|
+
except IOError as e:
|
|
180
|
+
raise utils.APIError("Error reading list: %s" % e)
|
|
181
|
+
|
|
182
|
+
def add_show(self, item):
|
|
183
|
+
"""Adds a new show in the server"""
|
|
184
|
+
self.check_credentials()
|
|
185
|
+
self.msg.info(self.name, "Adding show %s..." % item['title'])
|
|
186
|
+
|
|
187
|
+
xml = self._build_xml(item)
|
|
188
|
+
|
|
189
|
+
# Send the XML as POST data to the MyAnimeList API
|
|
190
|
+
values = {'data': xml}
|
|
191
|
+
data = urllib.parse.urlencode(values)
|
|
192
|
+
try:
|
|
193
|
+
self.opener.open(self.url + self.mediatype + "list/add/" + str(item['id']) + ".xml", data.encode('utf-8'))
|
|
194
|
+
except urllib.request.HTTPError as e:
|
|
195
|
+
raise utils.APIError('Error adding: ' + str(e.code))
|
|
196
|
+
|
|
197
|
+
def update_show(self, item):
|
|
198
|
+
"""Sends a show update to the server"""
|
|
199
|
+
self.check_credentials()
|
|
200
|
+
self.msg.info(self.name, "Updating show %s..." % item['title'])
|
|
201
|
+
|
|
202
|
+
xml = self._build_xml(item)
|
|
203
|
+
|
|
204
|
+
# Send the XML as POST data to the MyAnimeList API
|
|
205
|
+
values = {'data': xml}
|
|
206
|
+
data = urllib.parse.urlencode(values)
|
|
207
|
+
try:
|
|
208
|
+
self.opener.open(self.url + self.mediatype + "list/update/" + str(item['id']) + ".xml", data.encode('utf-8'))
|
|
209
|
+
except urllib.request.HTTPError as e:
|
|
210
|
+
raise utils.APIError('Error updating: ' + str(e.code))
|
|
211
|
+
|
|
212
|
+
def delete_show(self, item):
|
|
213
|
+
"""Sends a show delete to the server"""
|
|
214
|
+
self.check_credentials()
|
|
215
|
+
self.msg.info(self.name, "Deleting show %s..." % item['title'])
|
|
216
|
+
|
|
217
|
+
try:
|
|
218
|
+
self.opener.open(self.url + self.mediatype + "list/delete/" + str(item['id']) + ".xml")
|
|
219
|
+
except urllib.request.HTTPError as e:
|
|
220
|
+
raise utils.APIError('Error deleting: ' + str(e.code))
|
|
221
|
+
|
|
222
|
+
def search(self, criteria):
|
|
223
|
+
"""Searches MyAnimeList database for the queried show"""
|
|
224
|
+
self.msg.info(self.name, "Searching for %s..." % criteria)
|
|
225
|
+
|
|
226
|
+
# Send the urlencoded query to the search API
|
|
227
|
+
query = urllib.parse.urlencode({'q': criteria})
|
|
228
|
+
data = self._request(self.url + self.mediatype + "/search.xml?" + query)
|
|
229
|
+
|
|
230
|
+
# Load the results into XML
|
|
231
|
+
try:
|
|
232
|
+
root = self._parse_xml(data)
|
|
233
|
+
except ET.ParseError as e:
|
|
234
|
+
if e.code == 3:
|
|
235
|
+
# Empty document; no results
|
|
236
|
+
return []
|
|
237
|
+
else:
|
|
238
|
+
raise utils.APIError("Parser error: %r" % e)
|
|
239
|
+
except IOError:
|
|
240
|
+
raise utils.APIError("IO error: %r" % e)
|
|
241
|
+
|
|
242
|
+
# Use the correct tag name for episodes
|
|
243
|
+
if self.mediatype == 'manga':
|
|
244
|
+
episodes_str = 'chapters'
|
|
245
|
+
else:
|
|
246
|
+
episodes_str = 'episodes'
|
|
247
|
+
|
|
248
|
+
# Since the MAL API returns the status as a string, and
|
|
249
|
+
# we handle statuses as integers, we need to convert them
|
|
250
|
+
if self.mediatype == 'anime':
|
|
251
|
+
status_translate = {'Currently Airing': utils.STATUS_AIRING,
|
|
252
|
+
'Finished Airing': utils.STATUS_FINISHED,
|
|
253
|
+
'Not yet aired': utils.STATUS_NOTYET}
|
|
254
|
+
elif self.mediatype == 'manga':
|
|
255
|
+
status_translate = {'Publishing': utils.STATUS_AIRING,
|
|
256
|
+
'Finished': utils.STATUS_AIRING}
|
|
257
|
+
|
|
258
|
+
entries = list()
|
|
259
|
+
for child in root.iter('entry'):
|
|
260
|
+
show = utils.show()
|
|
261
|
+
showid = int(child.find('id').text)
|
|
262
|
+
show.update({
|
|
263
|
+
'id': showid,
|
|
264
|
+
'title': child.find('title').text,
|
|
265
|
+
'type': child.find('type').text,
|
|
266
|
+
'status': status_translate[child.find('status').text], # TODO : This should return an int!
|
|
267
|
+
'total': int(child.find(episodes_str).text),
|
|
268
|
+
'image': child.find('image').text,
|
|
269
|
+
'url': "http://myanimelist.net/anime/%d" % showid,
|
|
270
|
+
'start_date': self._str2date( child.find('start_date').text ),
|
|
271
|
+
'end_date': self._str2date( child.find('end_date').text ),
|
|
272
|
+
'extra': [
|
|
273
|
+
('English', child.find('english').text),
|
|
274
|
+
('Synonyms', child.find('synonyms').text),
|
|
275
|
+
('Synopsis', self._translate_synopsis(child.find('synopsis').text)),
|
|
276
|
+
(episodes_str.title(), child.find(episodes_str).text),
|
|
277
|
+
('Type', child.find('type').text),
|
|
278
|
+
('Score', child.find('score').text),
|
|
279
|
+
('Status', child.find('status').text),
|
|
280
|
+
('Start date', child.find('start_date').text),
|
|
281
|
+
('End date', child.find('end_date').text),
|
|
282
|
+
]
|
|
283
|
+
})
|
|
284
|
+
entries.append(show)
|
|
285
|
+
|
|
286
|
+
self._emit_signal('show_info_changed', entries)
|
|
287
|
+
return entries
|
|
288
|
+
|
|
289
|
+
def _translate_synopsis(self, string):
|
|
290
|
+
if string is None:
|
|
291
|
+
return None
|
|
292
|
+
else:
|
|
293
|
+
return string.replace('<br />', '')
|
|
294
|
+
|
|
295
|
+
def request_info(self, itemlist):
|
|
296
|
+
resultdict = dict()
|
|
297
|
+
for item in itemlist:
|
|
298
|
+
# Search for it only if it hasn't been found earlier
|
|
299
|
+
if item['id'] not in resultdict:
|
|
300
|
+
infos = self.search(item['title'])
|
|
301
|
+
for info in infos:
|
|
302
|
+
showid = info['id']
|
|
303
|
+
resultdict[showid] = info
|
|
304
|
+
|
|
305
|
+
itemids = [ show['id'] for show in itemlist ]
|
|
306
|
+
|
|
307
|
+
try:
|
|
308
|
+
reslist = [ resultdict[itemid] for itemid in itemids ]
|
|
309
|
+
except KeyError:
|
|
310
|
+
raise utils.APIError('There was a problem getting the show details.')
|
|
311
|
+
|
|
312
|
+
return reslist
|
|
313
|
+
|
|
314
|
+
def _parse_credentials(self, root):
|
|
315
|
+
if root is not None:
|
|
316
|
+
userid = int(root.find('id').text)
|
|
317
|
+
username = root.find('username').text
|
|
318
|
+
return (userid, username)
|
|
319
|
+
|
|
320
|
+
def _parse_anime(self, root):
|
|
321
|
+
"""Converts an XML anime list to a dictionary"""
|
|
322
|
+
showlist = dict()
|
|
323
|
+
for child in root.iter('anime'):
|
|
324
|
+
show_id = int(child.find('series_animedb_id').text)
|
|
325
|
+
if child.find('series_synonyms').text:
|
|
326
|
+
aliases = child.find('series_synonyms').text.lstrip('; ').split('; ')
|
|
327
|
+
else:
|
|
328
|
+
aliases = []
|
|
329
|
+
|
|
330
|
+
show = utils.show()
|
|
331
|
+
show.update({
|
|
332
|
+
'id': show_id,
|
|
333
|
+
'title': child.find('series_title').text,
|
|
334
|
+
'aliases': aliases,
|
|
335
|
+
'my_progress': int(child.find('my_watched_episodes').text),
|
|
336
|
+
'my_status': int(child.find('my_status').text),
|
|
337
|
+
'my_score': int(child.find('my_score').text),
|
|
338
|
+
'my_start_date': self._str2date( child.find('my_start_date').text ),
|
|
339
|
+
'my_finish_date': self._str2date( child.find('my_finish_date').text ),
|
|
340
|
+
'my_tags': child.find('my_tags').text,
|
|
341
|
+
'total': int(child.find('series_episodes').text),
|
|
342
|
+
'status': int(child.find('series_status').text),
|
|
343
|
+
'start_date': self._str2date( child.find('series_start').text ),
|
|
344
|
+
'end_date': self._str2date( child.find('series_end').text ),
|
|
345
|
+
'image': child.find('series_image').text,
|
|
346
|
+
'url': "http://myanimelist.net/anime/%d" % show_id,
|
|
347
|
+
})
|
|
348
|
+
showlist[show_id] = show
|
|
349
|
+
return showlist
|
|
350
|
+
|
|
351
|
+
def _parse_manga(self, root):
|
|
352
|
+
"""Converts an XML manga list to a dictionary"""
|
|
353
|
+
mangalist = dict()
|
|
354
|
+
for child in root.iter('manga'):
|
|
355
|
+
manga_id = int(child.find('series_mangadb_id').text)
|
|
356
|
+
if child.find('series_synonyms').text:
|
|
357
|
+
aliases = child.find('series_synonyms').text.lstrip('; ').split('; ')
|
|
358
|
+
else:
|
|
359
|
+
aliases = []
|
|
360
|
+
|
|
361
|
+
show = utils.show()
|
|
362
|
+
show.update({
|
|
363
|
+
'id': manga_id,
|
|
364
|
+
'title': child.find('series_title').text,
|
|
365
|
+
'aliases': aliases,
|
|
366
|
+
'my_progress': int(child.find('my_read_chapters').text),
|
|
367
|
+
'my_status': int(child.find('my_status').text),
|
|
368
|
+
'my_score': int(child.find('my_score').text),
|
|
369
|
+
'my_start_date': self._str2date( child.find('my_start_date').text ),
|
|
370
|
+
'my_finish_date': self._str2date( child.find('my_finish_date').text ),
|
|
371
|
+
'total': int(child.find('series_chapters').text),
|
|
372
|
+
'status': int(child.find('series_status').text),
|
|
373
|
+
'start_date': self._str2date( child.find('series_start').text ),
|
|
374
|
+
'end_date': self._str2date( child.find('series_end').text ),
|
|
375
|
+
'image': child.find('series_image').text,
|
|
376
|
+
'url': "http://myanimelist.net/manga/%d" % manga_id,
|
|
377
|
+
})
|
|
378
|
+
mangalist[manga_id] = show
|
|
379
|
+
return mangalist
|
|
380
|
+
|
|
381
|
+
def _build_xml(self, item):
|
|
382
|
+
"""
|
|
383
|
+
Creates an "anime|manga data" XML to be used in the
|
|
384
|
+
add, update and delete methods.
|
|
385
|
+
|
|
386
|
+
More information:
|
|
387
|
+
http://myanimelist.net/modules.php?go=api#animevalues
|
|
388
|
+
http://myanimelist.net/modules.php?go=api#mangavalues
|
|
389
|
+
|
|
390
|
+
"""
|
|
391
|
+
|
|
392
|
+
# Start building XML
|
|
393
|
+
root = ET.Element("entry")
|
|
394
|
+
|
|
395
|
+
# Use the correct name depending on mediatype
|
|
396
|
+
if self.mediatype == 'anime':
|
|
397
|
+
progressname = 'episode'
|
|
398
|
+
else:
|
|
399
|
+
progressname = 'chapter'
|
|
400
|
+
|
|
401
|
+
# Update necessary keys
|
|
402
|
+
if 'my_progress' in item.keys():
|
|
403
|
+
episode = ET.SubElement(root, progressname)
|
|
404
|
+
episode.text = str(item['my_progress'])
|
|
405
|
+
if 'my_status' in item.keys():
|
|
406
|
+
status = ET.SubElement(root, "status")
|
|
407
|
+
status.text = str(item['my_status'])
|
|
408
|
+
if 'my_score' in item.keys():
|
|
409
|
+
score = ET.SubElement(root, "score")
|
|
410
|
+
score.text = str(item['my_score'])
|
|
411
|
+
if 'my_start_date' in item.keys():
|
|
412
|
+
start_date = ET.SubElement(root, "date_start")
|
|
413
|
+
start_date.text = self._date2str(item['my_start_date'])
|
|
414
|
+
if 'my_finish_date' in item.keys():
|
|
415
|
+
finish_date = ET.SubElement(root, "date_finish")
|
|
416
|
+
finish_date.text = self._date2str(item['my_finish_date'])
|
|
417
|
+
if 'my_tags' in item.keys():
|
|
418
|
+
tags = ET.SubElement(root, "tags")
|
|
419
|
+
tags.text = str(item['my_tags'])
|
|
420
|
+
|
|
421
|
+
return ET.tostring(root)
|
|
422
|
+
|
|
423
|
+
def _date2str(self, date):
|
|
424
|
+
if date:
|
|
425
|
+
return date.strftime("%m%d%Y")
|
|
426
|
+
else:
|
|
427
|
+
return '0000-00-00'
|
|
428
|
+
|
|
429
|
+
def _str2date(self, string):
|
|
430
|
+
if string != '0000-00-00':
|
|
431
|
+
try:
|
|
432
|
+
return datetime.datetime.strptime(string, "%Y-%m-%d")
|
|
433
|
+
except ValueError:
|
|
434
|
+
return None # Ignore date if it's invalid
|
|
435
|
+
else:
|
|
436
|
+
return None
|
|
437
|
+
|
|
438
|
+
def _parse_xml(self, data):
|
|
439
|
+
# For some reason MAL returns an XML file with HTML exclusive
|
|
440
|
+
# entities like á, so we have to create a custom XMLParser
|
|
441
|
+
# to convert these entities correctly.
|
|
442
|
+
|
|
443
|
+
ENTITIES = {
|
|
444
|
+
"nbsp": u'\u00A0',
|
|
445
|
+
"iexcl": u'\u00A1',
|
|
446
|
+
"cent": u'\u00A2',
|
|
447
|
+
"pound": u'\u00A3',
|
|
448
|
+
"curren": u'\u00A4',
|
|
449
|
+
"yen": u'\u00A5',
|
|
450
|
+
"brvbar": u'\u00A6',
|
|
451
|
+
"sect": u'\u00A7',
|
|
452
|
+
"uml": u'\u00A8',
|
|
453
|
+
"copy": u'\u00A9',
|
|
454
|
+
"ordf": u'\u00AA',
|
|
455
|
+
"laquo": u'\u00AB',
|
|
456
|
+
"not": u'\u00AC',
|
|
457
|
+
"shy": u'\u00AD',
|
|
458
|
+
"reg": u'\u00AE',
|
|
459
|
+
"macr": u'\u00AF',
|
|
460
|
+
"deg": u'\u00B0',
|
|
461
|
+
"plusmn": u'\u00B1',
|
|
462
|
+
"sup2": u'\u00B2',
|
|
463
|
+
"sup3": u'\u00B3',
|
|
464
|
+
"acute": u'\u00B4',
|
|
465
|
+
"micro": u'\u00B5',
|
|
466
|
+
"para": u'\u00B6',
|
|
467
|
+
"middot": u'\u00B7',
|
|
468
|
+
"cedil": u'\u00B8',
|
|
469
|
+
"sup1": u'\u00B9',
|
|
470
|
+
"ordm": u'\u00BA',
|
|
471
|
+
"raquo": u'\u00BB',
|
|
472
|
+
"frac14": u'\u00BC',
|
|
473
|
+
"frac12": u'\u00BD',
|
|
474
|
+
"frac34": u'\u00BE',
|
|
475
|
+
"iquest": u'\u00BF',
|
|
476
|
+
"Agrave": u'\u00C0',
|
|
477
|
+
"Aacute": u'\u00C1',
|
|
478
|
+
"Acirc": u'\u00C2',
|
|
479
|
+
"Atilde": u'\u00C3',
|
|
480
|
+
"Auml": u'\u00C4',
|
|
481
|
+
"Aring": u'\u00C5',
|
|
482
|
+
"AElig": u'\u00C6',
|
|
483
|
+
"Ccedil": u'\u00C7',
|
|
484
|
+
"Egrave": u'\u00C8',
|
|
485
|
+
"Eacute": u'\u00C9',
|
|
486
|
+
"Ecirc": u'\u00CA',
|
|
487
|
+
"Euml": u'\u00CB',
|
|
488
|
+
"Igrave": u'\u00CC',
|
|
489
|
+
"Iacute": u'\u00CD',
|
|
490
|
+
"Icirc": u'\u00CE',
|
|
491
|
+
"Iuml": u'\u00CF',
|
|
492
|
+
"ETH": u'\u00D0',
|
|
493
|
+
"Ntilde": u'\u00D1',
|
|
494
|
+
"Ograve": u'\u00D2',
|
|
495
|
+
"Oacute": u'\u00D3',
|
|
496
|
+
"Ocirc": u'\u00D4',
|
|
497
|
+
"Otilde": u'\u00D5',
|
|
498
|
+
"Ouml": u'\u00D6',
|
|
499
|
+
"times": u'\u00D7',
|
|
500
|
+
"Oslash": u'\u00D8',
|
|
501
|
+
"Ugrave": u'\u00D9',
|
|
502
|
+
"Uacute": u'\u00DA',
|
|
503
|
+
"Ucirc": u'\u00DB',
|
|
504
|
+
"Uuml": u'\u00DC',
|
|
505
|
+
"Yacute": u'\u00DD',
|
|
506
|
+
"THORN": u'\u00DE',
|
|
507
|
+
"szlig": u'\u00DF',
|
|
508
|
+
"agrave": u'\u00E0',
|
|
509
|
+
"aacute": u'\u00E1',
|
|
510
|
+
"acirc": u'\u00E2',
|
|
511
|
+
"atilde": u'\u00E3',
|
|
512
|
+
"auml": u'\u00E4',
|
|
513
|
+
"aring": u'\u00E5',
|
|
514
|
+
"aelig": u'\u00E6',
|
|
515
|
+
"ccedil": u'\u00E7',
|
|
516
|
+
"egrave": u'\u00E8',
|
|
517
|
+
"eacute": u'\u00E9',
|
|
518
|
+
"ecirc": u'\u00EA',
|
|
519
|
+
"euml": u'\u00EB',
|
|
520
|
+
"igrave": u'\u00EC',
|
|
521
|
+
"iacute": u'\u00ED',
|
|
522
|
+
"icirc": u'\u00EE',
|
|
523
|
+
"iuml": u'\u00EF',
|
|
524
|
+
"eth": u'\u00F0',
|
|
525
|
+
"ntilde": u'\u00F1',
|
|
526
|
+
"ograve": u'\u00F2',
|
|
527
|
+
"oacute": u'\u00F3',
|
|
528
|
+
"ocirc": u'\u00F4',
|
|
529
|
+
"otilde": u'\u00F5',
|
|
530
|
+
"ouml": u'\u00F6',
|
|
531
|
+
"divide": u'\u00F7',
|
|
532
|
+
"oslash": u'\u00F8',
|
|
533
|
+
"ugrave": u'\u00F9',
|
|
534
|
+
"uacute": u'\u00FA',
|
|
535
|
+
"ucirc": u'\u00FB',
|
|
536
|
+
"uuml": u'\u00FC',
|
|
537
|
+
"yacute": u'\u00FD',
|
|
538
|
+
"thorn": u'\u00FE',
|
|
539
|
+
"yuml": u'\u00FF',
|
|
540
|
+
"fnof": u'\u0192',
|
|
541
|
+
"Alpha": u'\u0391',
|
|
542
|
+
"Beta": u'\u0392',
|
|
543
|
+
"Gamma": u'\u0393',
|
|
544
|
+
"Delta": u'\u0394',
|
|
545
|
+
"Epsilon": u'\u0395',
|
|
546
|
+
"Zeta": u'\u0396',
|
|
547
|
+
"Eta": u'\u0397',
|
|
548
|
+
"Theta": u'\u0398',
|
|
549
|
+
"Iota": u'\u0399',
|
|
550
|
+
"Kappa": u'\u039A',
|
|
551
|
+
"Lambda": u'\u039B',
|
|
552
|
+
"Mu": u'\u039C',
|
|
553
|
+
"Nu": u'\u039D',
|
|
554
|
+
"Xi": u'\u039E',
|
|
555
|
+
"Omicron": u'\u039F',
|
|
556
|
+
"Pi": u'\u03A0',
|
|
557
|
+
"Rho": u'\u03A1',
|
|
558
|
+
"Sigma": u'\u03A3',
|
|
559
|
+
"Tau": u'\u03A4',
|
|
560
|
+
"Upsilon": u'\u03A5',
|
|
561
|
+
"Phi": u'\u03A6',
|
|
562
|
+
"Chi": u'\u03A7',
|
|
563
|
+
"Psi": u'\u03A8',
|
|
564
|
+
"Omega": u'\u03A9',
|
|
565
|
+
"alpha": u'\u03B1',
|
|
566
|
+
"beta": u'\u03B2',
|
|
567
|
+
"gamma": u'\u03B3',
|
|
568
|
+
"delta": u'\u03B4',
|
|
569
|
+
"epsilon": u'\u03B5',
|
|
570
|
+
"zeta": u'\u03B6',
|
|
571
|
+
"eta": u'\u03B7',
|
|
572
|
+
"theta": u'\u03B8',
|
|
573
|
+
"iota": u'\u03B9',
|
|
574
|
+
"kappa": u'\u03BA',
|
|
575
|
+
"lambda": u'\u03BB',
|
|
576
|
+
"mu": u'\u03BC',
|
|
577
|
+
"nu": u'\u03BD',
|
|
578
|
+
"xi": u'\u03BE',
|
|
579
|
+
"omicron": u'\u03BF',
|
|
580
|
+
"pi": u'\u03C0',
|
|
581
|
+
"rho": u'\u03C1',
|
|
582
|
+
"sigmaf": u'\u03C2',
|
|
583
|
+
"sigma": u'\u03C3',
|
|
584
|
+
"tau": u'\u03C4',
|
|
585
|
+
"upsilon": u'\u03C5',
|
|
586
|
+
"phi": u'\u03C6',
|
|
587
|
+
"chi": u'\u03C7',
|
|
588
|
+
"psi": u'\u03C8',
|
|
589
|
+
"omega": u'\u03C9',
|
|
590
|
+
"thetasym": u'\u03D1',
|
|
591
|
+
"upsih": u'\u03D2',
|
|
592
|
+
"piv": u'\u03D6',
|
|
593
|
+
"bull": u'\u2022',
|
|
594
|
+
"hellip": u'\u2026',
|
|
595
|
+
"prime": u'\u2032',
|
|
596
|
+
"Prime": u'\u2033',
|
|
597
|
+
"oline": u'\u203E',
|
|
598
|
+
"frasl": u'\u2044',
|
|
599
|
+
"weierp": u'\u2118',
|
|
600
|
+
"image": u'\u2111',
|
|
601
|
+
"real": u'\u211C',
|
|
602
|
+
"trade": u'\u2122',
|
|
603
|
+
"alefsym": u'\u2135',
|
|
604
|
+
"larr": u'\u2190',
|
|
605
|
+
"uarr": u'\u2191',
|
|
606
|
+
"rarr": u'\u2192',
|
|
607
|
+
"darr": u'\u2193',
|
|
608
|
+
"harr": u'\u2194',
|
|
609
|
+
"crarr": u'\u21B5',
|
|
610
|
+
"lArr": u'\u21D0',
|
|
611
|
+
"uArr": u'\u21D1',
|
|
612
|
+
"rArr": u'\u21D2',
|
|
613
|
+
"dArr": u'\u21D3',
|
|
614
|
+
"hArr": u'\u21D4',
|
|
615
|
+
"forall": u'\u2200',
|
|
616
|
+
"part": u'\u2202',
|
|
617
|
+
"exist": u'\u2203',
|
|
618
|
+
"empty": u'\u2205',
|
|
619
|
+
"nabla": u'\u2207',
|
|
620
|
+
"isin": u'\u2208',
|
|
621
|
+
"notin": u'\u2209',
|
|
622
|
+
"ni": u'\u220B',
|
|
623
|
+
"prod": u'\u220F',
|
|
624
|
+
"sum": u'\u2211',
|
|
625
|
+
"minus": u'\u2212',
|
|
626
|
+
"lowast": u'\u2217',
|
|
627
|
+
"radic": u'\u221A',
|
|
628
|
+
"prop": u'\u221D',
|
|
629
|
+
"infin": u'\u221E',
|
|
630
|
+
"ang": u'\u2220',
|
|
631
|
+
"and": u'\u2227',
|
|
632
|
+
"or": u'\u2228',
|
|
633
|
+
"cap": u'\u2229',
|
|
634
|
+
"cup": u'\u222A',
|
|
635
|
+
"int": u'\u222B',
|
|
636
|
+
"there4": u'\u2234',
|
|
637
|
+
"sim": u'\u223C',
|
|
638
|
+
"cong": u'\u2245',
|
|
639
|
+
"asymp": u'\u2248',
|
|
640
|
+
"ne": u'\u2260',
|
|
641
|
+
"equiv": u'\u2261',
|
|
642
|
+
"le": u'\u2264',
|
|
643
|
+
"ge": u'\u2265',
|
|
644
|
+
"sub": u'\u2282',
|
|
645
|
+
"sup": u'\u2283',
|
|
646
|
+
"nsub": u'\u2284',
|
|
647
|
+
"sube": u'\u2286',
|
|
648
|
+
"supe": u'\u2287',
|
|
649
|
+
"oplus": u'\u2295',
|
|
650
|
+
"otimes": u'\u2297',
|
|
651
|
+
"perp": u'\u22A5',
|
|
652
|
+
"sdot": u'\u22C5',
|
|
653
|
+
"lceil": u'\u2308',
|
|
654
|
+
"rceil": u'\u2309',
|
|
655
|
+
"lfloor": u'\u230A',
|
|
656
|
+
"rfloor": u'\u230B',
|
|
657
|
+
"lang": u'\u2329',
|
|
658
|
+
"rang": u'\u232A',
|
|
659
|
+
"loz": u'\u25CA',
|
|
660
|
+
"spades": u'\u2660',
|
|
661
|
+
"clubs": u'\u2663',
|
|
662
|
+
"hearts": u'\u2665',
|
|
663
|
+
"diams": u'\u2666',
|
|
664
|
+
"quot": u'\"' ,
|
|
665
|
+
"amp": u'&' ,
|
|
666
|
+
"lt": u'<' ,
|
|
667
|
+
"gt": u'>' ,
|
|
668
|
+
"OElig": u'\u0152',
|
|
669
|
+
"oelig": u'\u0153',
|
|
670
|
+
"Scaron": u'\u0160',
|
|
671
|
+
"scaron": u'\u0161',
|
|
672
|
+
"Yuml": u'\u0178',
|
|
673
|
+
"circ": u'\u02C6',
|
|
674
|
+
"tilde": u'\u02DC',
|
|
675
|
+
"ensp": u'\u2002',
|
|
676
|
+
"emsp": u'\u2003',
|
|
677
|
+
"thinsp": u'\u2009',
|
|
678
|
+
"zwnj": u'\u200C',
|
|
679
|
+
"zwj": u'\u200D',
|
|
680
|
+
"lrm": u'\u200E',
|
|
681
|
+
"rlm": u'\u200F',
|
|
682
|
+
"ndash": u'\u2013',
|
|
683
|
+
"mdash": u'\u2014',
|
|
684
|
+
"lsquo": u'\u2018',
|
|
685
|
+
"rsquo": u'\u2019',
|
|
686
|
+
"sbquo": u'\u201A',
|
|
687
|
+
"ldquo": u'\u201C',
|
|
688
|
+
"rdquo": u'\u201D',
|
|
689
|
+
"bdquo": u'\u201E',
|
|
690
|
+
"dagger": u'\u2020',
|
|
691
|
+
"Dagger": u'\u2021',
|
|
692
|
+
"permil": u'\u2030',
|
|
693
|
+
"lsaquo": u'\u2039',
|
|
694
|
+
"rsaquo": u'\u203A',
|
|
695
|
+
"euro": u'\u20AC',
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
# http://stackoverflow.com/a/35591479/2016221
|
|
699
|
+
magic = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
700
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [\n'''
|
|
701
|
+
magic += ''.join("<!ENTITY %s '&#%d;'>\n" % (key, ord(value)) for key, value in ENTITIES.items())
|
|
702
|
+
magic += '\n]>'
|
|
703
|
+
|
|
704
|
+
# strip xml declaration since we're concatenating something before it
|
|
705
|
+
data = re.sub('<\?.*?\?>', '', data)
|
|
706
|
+
|
|
707
|
+
return ET.fromstring(magic + data)
|