[FIX] MyAnimeList - Use JSON instead of XML#2183
Conversation
Fixed MyAnimeList input plugin by switching from PHP\XML to JSON parsing. ANIME_TYPE item 'series' updated to 'tv' in order to better reflect how the site refers to these.
| if not isinstance(selected_types, list): | ||
| selected_types = [selected_types] | ||
|
|
||
| for i in range(len(selected_status)): |
There was a problem hiding this comment.
Looks like you can use enumerate here
There was a problem hiding this comment.
Fixed that and the other comments below. Should open a new PR or can a commit be added to this one?
|
|
||
| try: | ||
| list_response = task.requests.get('https://myanimelist.net/malappinfo.php', params=parameters) | ||
| list.response = task.requests.get('https://myanimelist.net/animelist/' + config['username'] + '/load.json') |
There was a problem hiding this comment.
The dot is probably a typo, right?
| if entry.isvalid(): | ||
| entries.append(entry) | ||
|
|
||
| js = json.loads(list.response.text.encode('utf-8')) |
There was a problem hiding this comment.
No need for this, just call .json() on the response
There was a problem hiding this comment.
That would also save the import json line at the top
| raise plugin.PluginError('Error reading JSON') | ||
|
|
||
| for anime in js: | ||
| if (anime["status"] in selected_status |
There was a problem hiding this comment.
Broke it down to comply with styling recommendation (up to 120 characters line) and make the conditions easier to read
There was a problem hiding this comment.
In that case, something like this is probably better:
has_selected_status = anime["status"] in selected_status or config['status'] == 'all'
has_selected_type = anime["anime_media_type_string"].lower() in selected_types or config['type'] == 'all'
if has_selected_status and has_selected_type:Lines are still <120 characters, and it's significantly easier to understand.
Fixed MyAnimeList input plugin by switching from PHP\XML to JSON parsing. ANIME_TYPE item 'series' updated to 'tv' in order to better reflect how the site refers to these.
| entries.append(entry) | ||
|
|
||
| js = list_response.json() | ||
| except: |
There was a problem hiding this comment.
This is too broad. I imagine you want to catch requests.HTTPError here
There was a problem hiding this comment.
The HTTP request is done earlier, this just decodes the response.
What exception is raised on decode errors seems to depend on the underlying JSON library used by requests, and possibles exceptions include at least simplejson.errors.JSONDecodeError and json.decoder.JSONDecodeError.
At the very least, the statement should be updated to except Exception.
| if not isinstance(selected_types, list): | ||
| selected_types = [selected_types] | ||
|
|
||
| for i,j in enumerate(selected_status): |
There was a problem hiding this comment.
Missing whitespace after comma. Also, if possible, please provide meaningful variable names.
| 'on_hold' : 3, | ||
| 'dropped' : 4, | ||
| 'plan_to_watch' : 6, | ||
| 'all' : 7 |
There was a problem hiding this comment.
Dictionary keys and : should not be separated by whitespace.
| selected_status = config.get('status', list(STATUS.values())) | ||
| selected_types = config.get('type', list(ANIME_TYPE.values())) | ||
| selected_status = config.get('status', list(STATUS.keys())) | ||
| selected_types = config.get('type', list(ANIME_TYPE)) |
There was a problem hiding this comment.
Assuming the defaults set in the schema ensure these have values, the second parameter to .get will never be used, and these could be replaced by config['status']/config['type'].
| selected_types = [selected_types] | ||
|
|
||
| for i,j in enumerate(selected_status): | ||
| selected_status[i] = STATUS[j] |
There was a problem hiding this comment.
Replacing these two lines with selected_status = [STATUS[s] for s in selected_status] would be more idiomatic.
| raise plugin.PluginError('Error reading JSON') | ||
|
|
||
| for anime in js: | ||
| if (anime["status"] in selected_status |
There was a problem hiding this comment.
In that case, something like this is probably better:
has_selected_status = anime["status"] in selected_status or config['status'] == 'all'
has_selected_type = anime["anime_media_type_string"].lower() in selected_types or config['type'] == 'all'
if has_selected_status and has_selected_type:Lines are still <120 characters, and it's significantly easier to understand.
| url = "https://myanimelist.net" + anime["anime_url"], | ||
| mal_name = anime["anime_title"], | ||
| mal_poster = anime["anime_image_path"], | ||
| mal_type = anime["anime_media_type_string"] |
There was a problem hiding this comment.
Equals sign (=) between parameter names and values should not be surrounded by white space.
|
|
||
| try: | ||
| list_response = task.requests.get('https://myanimelist.net/malappinfo.php', params=parameters) | ||
| list_response = task.requests.get('https://myanimelist.net/animelist/' + config['username'] + '/load.json') |
There was a problem hiding this comment.
config['username'] may require URL encoding (depending on what characters are allowed in usernames).
| mal_poster = anime["anime_image_path"], | ||
| mal_type = anime["anime_media_type_string"] | ||
| ) | ||
| entries.append(entry) |
There was a problem hiding this comment.
The Entry could be constructed directly when appended. No need to assign it to an intermediate variable first, i.e:
entries.append(Entry(
title=anime["anime_title"],
...
))There was a problem hiding this comment.
Nothing wrong with a little brevity there, assigning it first is fine
| selected_types = [selected_types] | ||
|
|
||
| for index, value in enumerate(selected_status): | ||
| selected_status[index] = STATUS[value] |
There was a problem hiding this comment.
selected_status = [STATUS[s] for s in selected_status] would still be more idiomatic than these two lines.
Other than that: LGTM.
Fixed MyAnimeList input plugin by switching from PHP\XML to JSON parsing.
ANIME_TYPE item 'series' updated to 'tv' in order to better reflect how the site refers to these.
Motivation for changes: Plugin stopped working after site maintenance and backend changes
Detailed changes:
Addressed issues:
Implemented feature requests:
Config usage if relevant (new plugin or updated schema):
type: seriesshould be updated totype: tvTo Do: