def __init__(self, aniDB, name=None, aid=None, tvdbid=None, paramsA=None, autoCorrectName=False, load=False): self.maper = AniDBMaper() self.tvDBMap = TvDBMap() self.allAnimeXML = None self.name = name self.aid = aid self.tvdb_id = tvdbid if self.tvdb_id and not self.aid: self.aid = self.tvDBMap.get_anidb_for_tvdb(self.tvdb_id) if not (self.name or self.aid): raise AniDBIncorrectParameterError("No aid or name available") if not self.aid: self.aid = self._get_aid_from_xml(self.name) if not self.name or autoCorrectName: self.name = self._get_name_from_xml(self.aid) if not (self.name or self.aid): raise ValueError if not self.tvdb_id: self.tvdb_id = self.tvDBMap.get_tvdb_for_anidb(self.aid) if not paramsA: self.bitCode = "b2f0e0fc000000" self.params = self.maper.getAnimeCodesA(self.bitCode) else: self.paramsA = paramsA self.bitCode = self.maper.getAnimeBitsA(self.paramsA) super(Anime, self).__init__(aniDB, load)
class Anime(aniDBabstractObject): def __init__(self, aniDB, name=None, aid=None, tvdbid=None, paramsA=None, autoCorrectName=False, load=False): self.maper = AniDBMaper() self.tvDBMap = TvDBMap() self.allAnimeXML = None self.name = name self.aid = aid self.tvdb_id = tvdbid if self.tvdb_id and not self.aid: self.aid = self.tvDBMap.get_anidb_for_tvdb(self.tvdb_id) if not (self.name or self.aid): raise AniDBIncorrectParameterError("No aid or name available") if not self.aid: self.aid = self._get_aid_from_xml(self.name) if not self.name or autoCorrectName: self.name = self._get_name_from_xml(self.aid) if not (self.name or self.aid): raise ValueError if not self.tvdb_id: self.tvdb_id = self.tvDBMap.get_tvdb_for_anidb(self.aid) if not paramsA: self.bitCode = "b2f0e0fc000000" self.params = self.maper.getAnimeCodesA(self.bitCode) else: self.paramsA = paramsA self.bitCode = self.maper.getAnimeBitsA(self.paramsA) super(Anime, self).__init__(aniDB, load) def load_data(self): """load the data from anidb""" if not (self.name or self.aid): raise ValueError self.rawData = self.aniDB.anime(aid=self.aid, aname=self.name, amask=self.bitCode) if self.rawData.datalines: self._fill(self.rawData.datalines[0]) self._builPreSequal() self.laoded = True def get_groups(self): if not self.aid: return [] self.rawData = self.aniDB.groupstatus(aid=self.aid) self.release_groups = [] for line in self.rawData.datalines: self.release_groups.append({"name":unicode(line["name"], "utf-8"), "rating":line["rating"], "range":line["episode_range"] }) return self.release_groups #TODO: refactor and use the new functions in anidbFileinfo def _get_aid_from_xml(self, name): if not self.allAnimeXML: self.allAnimeXML = self._read_animetitels_xml() regex = re.compile('( \(\d{4}\))|[%s]' % re.escape(string.punctuation)) # remove any punctuation and e.g. ' (2011)' #regex = re.compile('[%s]' % re.escape(string.punctuation)) # remove any punctuation and e.g. ' (2011)' name = regex.sub('', name.lower()) lastAid = 0 for element in self.allAnimeXML.getiterator(): if element.get("aid", False): lastAid = int(element.get("aid")) if element.text: testname = regex.sub('', element.text.lower()) if testname == name: return lastAid return 0 #TODO: refactor and use the new functions in anidbFileinfo def _get_name_from_xml(self, aid, onlyMain=True): if not self.allAnimeXML: self.allAnimeXML = self._read_animetitels_xml() for anime in self.allAnimeXML.findall("anime"): if int(anime.get("aid", False)) == aid: for title in anime.getiterator(): currentLang = title.get("{http://www.w3.org/XML/1998/namespace}lang", False) currentType = title.get("type", False) if (currentLang == "en" and not onlyMain) or currentType == "main": return title.text return "" def _read_animetitels_xml(self, path=None): if not path: path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "animetitles.xml") f = open(path, "r") allAnimeXML = etree.ElementTree(file=f) return allAnimeXML def _builPreSequal(self): if self.related_aid_list and self.related_aid_type: try: for i in range(len(self.related_aid_list)): if self.related_aid_type[i] == 2: self.__dict__["prequal"] = self.related_aid_list[i] elif self.related_aid_type[i] == 1: self.__dict__["sequal"] = self.related_aid_list[i] except: if self.related_aid_type == 2: self.__dict__["prequal"] = self.related_aid_list elif self.str_related_aid_type == 1: self.__dict__["sequal"] = self.related_aid_list
class Anime(aniDBabstractObject): def __init__(self, aniDB, name=None, aid=None, tvdbid=None, paramsA=None, autoCorrectName=False, load=False): self.maper = AniDBMaper() self.tvDBMap = TvDBMap() self.allAnimeXML = None self.name = name self.aid = aid self.tvdb_id = tvdbid if self.tvdb_id and not self.aid: self.aid = self.tvDBMap.get_anidb_for_tvdb(self.tvdb_id) if not (self.name or self.aid): raise AniDBIncorrectParameterError("No aid or name available") if not self.aid: self.aid = self._get_aid_from_xml(self.name) if not self.name or autoCorrectName: self.name = self._get_name_from_xml(self.aid) if not (self.name or self.aid): raise ValueError if not self.tvdb_id: self.tvdb_id = self.tvDBMap.get_tvdb_for_anidb(self.aid) if not paramsA: self.bitCode = "b2f0e0fc000000" self.params = self.maper.getAnimeCodesA(self.bitCode) else: self.paramsA = paramsA self.bitCode = self.maper.getAnimeBitsA(self.paramsA) super(Anime, self).__init__(aniDB, load) def load_data(self): """load the data from anidb""" if not (self.name or self.aid): raise ValueError self.rawData = self.aniDB.anime(aid=self.aid, aname=self.name, amask=self.bitCode) if self.rawData.datalines: self._fill(self.rawData.datalines[0]) self._builPreSequal() self.laoded = True def get_groups(self): if not self.aid: return [] self.rawData = self.aniDB.groupstatus(aid=self.aid) self.release_groups = [] for line in self.rawData.datalines: self.release_groups.append({"name": unicode(line["name"], "utf-8"), "rating": line["rating"], "range": line["episode_range"] }) return self.release_groups # TODO: refactor and use the new functions in anidbFileinfo def _get_aid_from_xml(self, name): if not self.allAnimeXML: self.allAnimeXML = read_anidb_xml() regex = re.compile( '( \(\d{4}\))|[%s]' % re.escape(string.punctuation)) # remove any punctuation and e.g. ' (2011)' #regex = re.compile('[%s]' % re.escape(string.punctuation)) # remove any punctuation and e.g. ' (2011)' name = regex.sub('', name.lower()) lastAid = 0 for element in self.allAnimeXML.iter(): if element.get("aid", False): lastAid = int(element.get("aid")) if element.text: testname = regex.sub('', element.text.lower()) if testname == name: return lastAid return 0 #TODO: refactor and use the new functions in anidbFileinfo def _get_name_from_xml(self, aid, onlyMain=True): if not self.allAnimeXML: self.allAnimeXML = read_anidb_xml() for anime in self.allAnimeXML.findall("anime"): if int(anime.get("aid", False)) == aid: for title in anime.iter(): currentLang = title.get("{http://www.w3.org/XML/1998/namespace}lang", False) currentType = title.get("type", False) if (currentLang == "en" and not onlyMain) or currentType == "main": return title.text return "" def _builPreSequal(self): if self.related_aid_list and self.related_aid_type: try: for i in range(len(self.related_aid_list)): if self.related_aid_type[i] == 2: self.__dict__["prequal"] = self.related_aid_list[i] elif self.related_aid_type[i] == 1: self.__dict__["sequal"] = self.related_aid_list[i] except: if self.related_aid_type == 2: self.__dict__["prequal"] = self.related_aid_list elif self.str_related_aid_type == 1: self.__dict__["sequal"] = self.related_aid_list