ann.py: Various improvements.
authorFredrik Tolf <fredrik@dolda2000.com>
Fri, 20 Dec 2019 02:20:13 +0000 (03:20 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Fri, 20 Dec 2019 02:20:13 +0000 (03:20 +0100)
ann.py

diff --git a/ann.py b/ann.py
index 2f3970a..ee3d472 100644 (file)
--- a/ann.py
+++ b/ann.py
@@ -63,6 +63,8 @@ def cstr(soup):
         for el in soup:
             ret += cstr(el)
         return ret
+    elif soup is None:
+        return None
     else:
         return soup.string
 
@@ -101,17 +103,14 @@ class anime(object):
     def _main(self):
         return afind(self._page, "div", id="maincontent")
 
-    @cproperty
-    def _info(self):
-        ret = {}
+    def _info(self, nm):
         for t in afind(self._main, "div", id="content-zone")("div", "encyc-info-type"):
-            if t.strong:
-                ret[t.strong.text.lower().strip()[:-1]] = t.contents[t.contents.index(t.strong) + 1:]
-        return ret
+            if t.strong and t.strong.text.lower().strip()[:-1] == nm:
+                return t.contents[t.contents.index(t.strong) + 1:]
 
     @cproperty
     def rawname(self):
-        afind(self._main, "h1", id="page_header").text
+        return afind(self._main, "h1", id="page_header").text
     _nre = re.compile(r"^(.*\S)\s+\(([^\)]+)\)$")
     @cproperty
     def _sname(self):
@@ -125,8 +124,26 @@ class anime(object):
     def type(self): return self._sname[1]
 
     @cproperty
+    def names(self):
+        ret = []
+        for el in self._info("alternative title"):
+            if isinstance(el, bs4.Tag) and el.name == "div" and "tab" in el.get("class", []):
+                m = self._nre.search(el.text)
+                if m:
+                    ret.append((m.groups()[0], m.groups()[1]))
+                else:
+                    ret.append((el.text, None))
+        if (self.name, None) in ret:
+            ret.remove((self.name, None))
+        ret.insert(0, (self.name, None))
+        return ret
+
+    @cproperty
     def eps(self):
-        return int(cstr(self._info["number of episodes"]))
+        ret = cstr(self._info("number of episodes"))
+        if ret is None:
+            return ret
+        return int(ret)
 
     def __repr__(self):
         return "<ann.anime: %r (%i)>" % (self.name, self.id)
@@ -134,6 +151,10 @@ class anime(object):
     def __str__(self):
         return self.name
 
+    @classmethod
+    def byid(cls, id):
+        return cls(id)
+
 linkpat = re.compile("^/encyclopedia/anime\\.php\\?id=(\d+)$")
 def getlist(name):
     name = s(name, "^the\s+", "")
@@ -156,7 +177,7 @@ def getlist(name):
             m = linkpat.match(link["href"])
             if not m:
                 raise incompatible()
-            found = anime(int(m.groups()[0]))
+            found = anime.byid(int(m.groups()[0]))
             found.rawname = mn
             ret.append(found)
     return ret