X-Git-Url: http://dolda2000.com/gitweb/?p=utils.git;a=blobdiff_plain;f=ann.py;h=935e0a9cbe50ca078c8e11b9a3ee0aaba68b78c4;hp=2f3970a2a4409a94b5d51789f7b08b8ad44bb6ec;hb=f7bd9b51138dbccb7cc2fd27f07ef66d5d2d79cf;hpb=5b7914ac4a6aa32ac7b8417b286e6ba584fce636 diff --git a/ann.py b/ann.py index 2f3970a..935e0a9 100644 --- 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 "" % (self.name, self.id) @@ -134,9 +151,13 @@ 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+", "") + name = s(name, "^(the|a)\s+", "") if len(name) < 1: raise error("list() needs a prefix of at least one character") fc = name[0] @@ -152,11 +173,12 @@ def getlist(name): for el in link.font: if isinstance(el, str): mn += el.strip() + mn = s(mn, "^a\s+", "") if mn.lower().startswith(name.lower()): 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