X-Git-Url: http://dolda2000.com/gitweb/?p=utils.git;a=blobdiff_plain;f=ann.py;h=4d2c5d15ed7b8eccc2f0ac062fa76edc52f6ff19;hp=ee3d4726188dcad0683cbbb123c53dd264c0f9d9;hb=f8775f9c59566ff6c5d90f04f471f0bcbf2316d7;hpb=307f4e9308c8c11069f48e3bd88fbedddf4f6de3 diff --git a/ann.py b/ann.py index ee3d472..4d2c5d1 100644 --- a/ann.py +++ b/ann.py @@ -3,6 +3,9 @@ from urllib.parse import urljoin, urlencode import bs4 soup = lambda cont: bs4.BeautifulSoup(cont, "html.parser") +__all__ = ["anime", "getlist", + "error", "incompatible"] + base = "http://www.animenewsnetwork.com/encyclopedia/" class error(Exception): @@ -63,6 +66,8 @@ def cstr(soup): for el in soup: ret += cstr(el) return ret + elif isinstance(soup, str): + return soup elif soup is None: return None else: @@ -145,6 +150,18 @@ class anime(object): return ret return int(ret) + @cproperty + def vintage(self): + return cstr(self._info("vintage")).strip() + + @cproperty + def genres(self): + return [cstr(el) for x in (self._info("genres") or []) if isinstance(x, bs4.Tag) for el in x.findAll("a")] + + @cproperty + def themes(self): + return [cstr(el) for x in (self._info("themes") or []) if isinstance(x, bs4.Tag) for el in x.findAll("a")] + def __repr__(self): return "" % (self.name, self.id) @@ -157,7 +174,7 @@ class anime(object): linkpat = re.compile("^/encyclopedia/anime\\.php\\?id=(\d+)$") def getlist(name): - name = s(name, "^the\s+", "") + name = s(name, "^(the|a)\s+", "") if len(name) < 1: raise error("list() needs a prefix of at least one character") fc = name[0] @@ -169,15 +186,19 @@ def getlist(name): ret = [] ldiv = afind(afind(d, "div", id="maincontent"), "div", "lst") for link in ldiv("a", "HOVERLINE"): - mn = "" + rawname = "" for el in link.font: if isinstance(el, str): - mn += el.strip() + rawname += el.strip() + mn = rawname.lower() + mn = s(mn, "^a\s+", "") + mn = mn.replace("\u014d", "ou") + mn = mn.replace("\u016b", "uu") if mn.lower().startswith(name.lower()): m = linkpat.match(link["href"]) if not m: raise incompatible() found = anime.byid(int(m.groups()[0])) - found.rawname = mn + found.rawname = rawname ret.append(found) return ret