Bohdan Rybak avatar Bohdan Rybak committed b9ec5b1

MAL entry parse function added

Comments (0)

Files changed (4)

+# (c) Bohdan Rybak, 2012, see LICENSE for details
+
+import urllib2
+from xml.etree import ElementTree
+
+import mal_utils
+ 
+class MyAnimeList(object):
+    _url = 'http://myanimelist.net/malappinfo.php?status=all&u=%s&type=anime'
+
+    def __init__(self, username):
+        self._apiurl = self._url % username
+
+    def get_full_xml(self):
+        u = urllib2.urlopen(self._apiurl)
+        return ElementTree.parse(u)
+
+    def get_anime_list(self, status='all'):
+        anime_list = self.get_full_xml().findall('anime')
+        return mal_utils.filter_anime_list(anime_list, status)
+# (c) Bohdan Rybak, 2012, see LICENSE for details
+
+import datetime
+
+anime_type = {1: 'TV', 2: 'OVA', 3: 'Movie', 4: 'Special', 5: 'ONA', 6: 'Music'}
+
+anime_status = {1: 'airing', 2: 'finished', 3: 'not_aired'}
+    
+anime_user_status = {1: 'watching', 2: 'completed', 3: 'on_hold',
+                     4: 'dropped', 6: 'plan_to_watch'}
+
+def filter_anime_list(anime_list, status):
+    if status == 'all':
+        return anime_list
+    else:
+        return [a for a in anime_list
+                if int(a.find('my_status').text) == anime_user_status[status]]
+
+
+def parse_anime_entry(entry):
+    def get(attrib):
+        return entry.find(attrib).text
+
+    def get_date(attrib):
+        text = get(attrib)
+        if text == '0000-00-00':
+            return None
+        else:
+            return datetime.datetime.strptime(text, "%Y-%m-%d").date()
+
+    def split_text(text, delim):
+        if text is None or text == '':
+            return []
+            
+        splitted = text.split(delim)
+        return splitted
+        
+    result = {
+        'mal_db_id': int(get('series_animedb_id')),
+        'title': get('series_title'),
+        'title_synonyms': split_text(get('series_synonyms'), '; '),
+        'type': anime_type[int(get('series_type'))],
+        'episodes': int(get('series_episodes')),
+        'status': anime_status[int(get('series_status'))],
+        'start': get_date('series_start'),
+        'end': get_date('series_end'),
+        'image': get('series_image'),
+        'my_id': int(get('my_id')),
+        'my_watched_episodes': int(get('my_watched_episodes')),
+        'my_start_date': get_date('my_start_date'),
+        'my_finish_date': get_date('my_finish_date'),
+        'my_score': int(get('my_score')),
+        'my_status': anime_user_status[int(get('my_status'))],
+        'my_rewatching': int(get('my_rewatching')),
+        'my_rewatching_ep': int(get('my_rewatching_ep')),
+        'my_last_updated': int(get('my_last_updated')),
+        'my_tags': split_text(get('my_tags'),',')
+    }
+
+    return result

myanimelist.py

-# (c) Bohdan Rybak, 2012, see LICENSE for details
-
-import urllib2
-from xml.etree import ElementTree
-
-class AnimeStatus(object):
-    All, Watching, Completed, OnHold, Dropped = range(5)
-    PlanToWatch = 6  # Don't ask me why -_-
-
-def filter_anime_list(anime_list, status):
-    if status == AnimeStatus.All:
-        return anime_list
-    else:
-        return [a for a in anime_list if int(a.find('my_status').text) == status]
-
-class MyAnimeList(object):
-    _url = 'http://myanimelist.net/malappinfo.php?status=all&u=%s&type=anime'
-
-    def __init__(self, username):
-        self._apiurl = self._url % username
-
-    def get_full_xml(self):
-        u = urllib2.urlopen(self._apiurl)
-        return ElementTree.parse(u)
-
-    def get_anime_list(self, status=AnimeStatus.All):
-        anime_list = self.get_full_xml().findall('anime')
-        return filter_anime_list(anime_list, status)

tests/mal_utils_test.py

+# (c) Bohdan Rybak, 2012, see LICENSE for details
+
+import unittest
+
+import datetime
+from xml.etree import ElementTree
+
+import mal_utils
+
+
+class MALFunctionsTestCase(unittest.TestCase):
+    def test_parse_anime_entry(self):
+        xml = "<anime><series_animedb_id>12189</series_animedb_id><series_title>Hyouka</series_title><series_synonyms>Hyou-ka; Hyouka: You can't escape; Hyou-ka: You can't escape; Hyoka</series_synonyms><series_type>1</series_type><series_episodes>21</series_episodes><series_status>1</series_status><series_start>2012-04-14</series_start><series_end>0000-00-00</series_end><series_image>http://cdn.myanimelist.net/images/anime/5/38273.jpg</series_image><my_id>0</my_id><my_watched_episodes>16</my_watched_episodes><my_start_date>0000-00-00</my_start_date><my_finish_date>0000-00-00</my_finish_date><my_score>8</my_score><my_status>1</my_status><my_rewatching>0</my_rewatching><my_rewatching_ep>0</my_rewatching_ep><my_last_updated>1344543860</my_last_updated><my_tags /></anime>"
+
+        expected = {
+            'mal_db_id': 12189,
+            'title': 'Hyouka',
+            'title_synonyms': [
+                "Hyou-ka",
+                "Hyouka: You can't escape",
+                "Hyou-ka: You can't escape",
+                "Hyoka"
+            ],
+            'type': 'TV',
+            'episodes': 21,
+            'status': 'airing',
+            'start': datetime.date(2012, 04, 14),
+            'end': None,
+            'image': 'http://cdn.myanimelist.net/images/anime/5/38273.jpg',
+            'my_id': 0,
+            'my_watched_episodes': 16,
+            'my_start_date': None,
+            'my_finish_date': None,
+            'my_score': 8,
+            'my_status': 'watching',
+            'my_rewatching': 0,
+            'my_rewatching_ep': 0,
+            'my_last_updated': 1344543860, # ?
+            'my_tags': []
+        }
+
+        self.assertEqual(mal_utils.parse_anime_entry(ElementTree.fromstring(xml)),
+                         expected)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.