def __init__(self, tvdb_url, language, is_movie, TVDb): tvdb_url = tvdb_url.strip() if not is_movie and tvdb_url.startswith( (TVDb.series_url, TVDb.alt_series_url, TVDb.series_id_url)): self.media_type = "Series" elif is_movie and tvdb_url.startswith( (TVDb.movies_url, TVDb.alt_movies_url, TVDb.movie_id_url)): self.media_type = "Movie" else: raise Failed("TVDb Error: {} must begin with {}".format( tvdb_url, TVDb.movies_url if is_movie else TVDb.series_url)) response = TVDb.send_request(tvdb_url, language) results = response.xpath( "//*[text()='TheTVDB.com {} ID']/parent::node()/span/text()". format(self.media_type)) if len(results) > 0: self.id = int(results[0]) else: raise Failed( "TVDb Error: Could not find a TVDb {} ID at the URL {}".format( self.media_type, tvdb_url)) results = response.xpath( "//div[@class='change_translation_text' and @data-language='eng']/@data-title" ) if len(results) > 0 and len(results[0]) > 0: self.title = results[0] else: raise Failed("TVDb Error: Name not found from TVDb URL: {}".format( tvdb_url)) results = response.xpath( "//div[@class='row hidden-xs hidden-sm']/div/img/@src") self.poster_path = results[0] if len(results) > 0 and len( results[0]) > 0 else None tmdb_id = None if is_movie: results = response.xpath("//*[text()='TheMovieDB.com']/@href") if len(results) > 0: try: tmdb_id = util.regex_first_int(results[0], "TMDb ID") except Failed as e: logger.error(e) if not tmdb_id: results = response.xpath("//*[text()='IMDB']/@href") if len(results) > 0: try: tmdb_id = TVDb.convert_from_imdb( util.get_id_from_imdb_url(results[0]), language) except Failed as e: logger.error(e) self.tmdb_id = tmdb_id self.tvdb_url = tvdb_url self.language = language self.is_movie = is_movie self.TVDb = TVDb
def __init__(self, tvdb_url, language, is_movie, TVDb): tvdb_url = tvdb_url.strip() if not is_movie and tvdb_url.startswith( (TVDb.series_url, TVDb.alt_series_url, TVDb.series_id_url)): self.media_type = "Series" elif is_movie and tvdb_url.startswith( (TVDb.movies_url, TVDb.alt_movies_url, TVDb.movie_id_url)): self.media_type = "Movie" else: raise Failed( f"TVDb Error: {tvdb_url} must begin with {TVDb.movies_url if is_movie else TVDb.series_url}" ) response = TVDb.send_request(tvdb_url, language) results = response.xpath( f"//*[text()='TheTVDB.com {self.media_type} ID']/parent::node()/span/text()" ) if len(results) > 0: self.id = int(results[0]) elif tvdb_url.startswith(TVDb.movie_id_url): raise Failed( f"TVDb Error: Could not find a TVDb Movie using TVDb Movie ID: {tvdb_url[len(TVDb.movie_id_url):]}" ) elif tvdb_url.startswith(TVDb.series_id_url): raise Failed( f"TVDb Error: Could not find a TVDb Series using TVDb Series ID: {tvdb_url[len(TVDb.series_id_url):]}" ) else: raise Failed( f"TVDb Error: Could not find a TVDb {self.media_type} ID at the URL {tvdb_url}" ) results = response.xpath( "//div[@class='change_translation_text' and @data-language='eng']/@data-title" ) if len(results) > 0 and len(results[0]) > 0: self.title = results[0] else: raise Failed( f"TVDb Error: Name not found from TVDb URL: {tvdb_url}") results = response.xpath( "//div[@class='row hidden-xs hidden-sm']/div/img/@src") self.poster_path = results[0] if len(results) > 0 and len( results[0]) > 0 else None results = response.xpath( "(//h2[@class='mt-4' and text()='Backgrounds']/following::div/a/@href)[1]" ) self.background_path = results[0] if len(results) > 0 and len( results[0]) > 0 else None results = response.xpath( "//div[@class='block']/div[not(@style='display:none')]/p/text()") self.summary = results[0] if len(results) > 0 and len( results[0]) > 0 else None tmdb_id = None if is_movie: results = response.xpath("//*[text()='TheMovieDB.com']/@href") if len(results) > 0: try: tmdb_id = util.regex_first_int(results[0], "TMDb ID") except Failed as e: logger.error(e) if not tmdb_id: results = response.xpath("//*[text()='IMDB']/@href") if len(results) > 0: try: tmdb_id, _ = TVDb.config.Arms.imdb_to_ids( util.get_id_from_imdb_url(results[0]), language) except Failed as e: logger.error(e) self.tmdb_id = tmdb_id self.tvdb_url = tvdb_url self.language = language self.is_movie = is_movie self.TVDb = TVDb
def __init__(self, tvdb_url, language, is_movie, config): self.tvdb_url = tvdb_url.strip() self.language = language self.is_movie = is_movie self.config = config if not self.is_movie and self.tvdb_url.startswith( (urls["series"], urls["alt_series"], urls["series_id"])): self.media_type = "Series" elif self.is_movie and self.tvdb_url.startswith( (urls["movies"], urls["alt_movies"], urls["movie_id"])): self.media_type = "Movie" else: raise Failed( f"TVDb Error: {self.tvdb_url} must begin with {urls['movies'] if self.is_movie else urls['series']}" ) if self.config.trace_mode: logger.debug(f"URL: {tvdb_url}") response = self.config.get_html(self.tvdb_url, headers=util.header(self.language)) results = response.xpath( f"//*[text()='TheTVDB.com {self.media_type} ID']/parent::node()/span/text()" ) if len(results) > 0: self.id = int(results[0]) elif self.tvdb_url.startswith(urls["movie_id"]): raise Failed( f"TVDb Error: Could not find a TVDb Movie using TVDb Movie ID: {self.tvdb_url[len(urls['movie_id']):]}" ) elif self.tvdb_url.startswith(urls["series_id"]): raise Failed( f"TVDb Error: Could not find a TVDb Series using TVDb Series ID: {self.tvdb_url[len(urls['series_id']):]}" ) else: raise Failed( f"TVDb Error: Could not find a TVDb {self.media_type} ID at the URL {self.tvdb_url}" ) def parse_page(xpath): parse_results = response.xpath(xpath) if len(parse_results) > 0: parse_results = [ r.strip() for r in parse_results if len(r) > 0 ] return parse_results[0] if len(parse_results) > 0 else None def parse_title_summary(lang=None): place = "//div[@class='change_translation_text' and " place += f"@data-language='{lang}']" if lang else "not(@style='display:none')]" return parse_page(f"{place}/@data-title"), parse_page( f"{place}/p/text()[normalize-space()]") self.title, self.summary = parse_title_summary(lang=self.language) if not self.title and self.language in language_translation: self.title, self.summary = parse_title_summary( lang=language_translation[self.language]) if not self.title: self.title, self.summary = parse_title_summary() if not self.title: raise Failed( f"TVDb Error: Name not found from TVDb URL: {self.tvdb_url}") self.poster_path = parse_page( "//div[@class='row hidden-xs hidden-sm']/div/img/@src") self.background_path = parse_page( "(//h2[@class='mt-4' and text()='Backgrounds']/following::div/a/@href)[1]" ) if self.is_movie: self.directors = parse_page( "//strong[text()='Directors']/parent::li/span/a/text()[normalize-space()]" ) self.writers = parse_page( "//strong[text()='Writers']/parent::li/span/a/text()[normalize-space()]" ) self.studios = parse_page( "//strong[text()='Studio']/parent::li/span/a/text()[normalize-space()]" ) else: self.networks = parse_page( "//strong[text()='Networks']/parent::li/span/a/text()[normalize-space()]" ) self.genres = parse_page( "//strong[text()='Genres']/parent::li/span/a/text()[normalize-space()]" ) tmdb_id = None imdb_id = None if self.is_movie: results = response.xpath("//*[text()='TheMovieDB.com']/@href") if len(results) > 0: try: tmdb_id = util.regex_first_int(results[0], "TMDb ID") except Failed: pass results = response.xpath("//*[text()='IMDB']/@href") if len(results) > 0: try: imdb_id = util.get_id_from_imdb_url(results[0]) except Failed: pass if tmdb_id is None and imdb_id is None: raise Failed( f"TVDB Error: No TMDb ID or IMDb ID found for {self.title}" ) self.tmdb_id = tmdb_id self.imdb_id = imdb_id