def main(): print("! Getting a list of TV shows from eztv...") showlist_page = lxml.html.fromstring(requests.get(EZTV_URL).content) shows = [l.text for l in showlist_page.xpath('//a[@class="thread_link"]')] print("") imdb = Imdb() episode_records = [] for show_name in shows[:10]: print("* Processing `{}`...".format(show_name)) episodes = None for show in imdb.search_for_title(show_name): try: episodes = imdb.get_episodes(show['imdb_id']) break except (RuntimeError, TypeError): # RuntimeError: This is thrown when a show is not recognized a series # TypeError: Bug where seasons is None. continue if episodes is None: print(" ! Couldn't find an IMDB entry for `{}`. Ignoring.".format( show_name)) continue episode_records += [e.__dict__ for e in episodes] df = pd.DataFrame(episode_records) df.to_csv(DATA_PATH, index=False)
class imdb_api: def __init__(self, anonymize=False): self.imdb = Imdb(anonymize=anonymize) def search(self, title, imdb_id=""): if not imdb_id: print("Searching") results = self.imdb.search_for_title(title) chosen = self.search_select(results) title_id = results[chosen]["imdb_id"] else: title_id = imdb_id print("Looking up series") title = self.imdb.get_title_by_id(title_id) if title.type == "tv_series": print("Detected TV series, downloading episode list") episodes = self.imdb.get_episodes(title_id) print(episodes) return [title, episodes] else: return [title] def search_select(self, results): def get_input(allow_0=True): print("Which show do you want? type 0 to see more") out = 0 while out == 0: inp = input("> ") if inp == "0" and allow_0: return out else: try: out = int(inp) except ValueError: print("Not a number") return out if len(results) > 1: chosen = 0 for index, show in enumerate(results): print("[{0}] ({3})\t{1}\t{2}".format(str(index + 1), show["title"], show["year"], show["imdb_id"])) if ((index + 1) % 10) == 0: chosen = get_input() if chosen != 0: break if chosen == 0: print("No more") chosen = get_input(allow_0=False) return chosen - 1 #imdb = imdb_api() #x = imdb.search("Person of Interest") #print(x)
def test_get_episodes(self): assert self.imdb.get_title_by_id('tt0303461') is not None imdb = Imdb() episodes = imdb.get_episodes('tt0303461') assert episodes is not None assert len(episodes) == 14 episode_1 = episodes[0] assert episode_1.imdb_id == "tt0579539" assert episode_1.type == "tv_episode" assert episode_1.title == u'The Train Job' assert episode_1.series_name == 'Firefly' assert episode_1.release_date == "2002-09-20" assert episode_1.year == 2002
def seasonBuilder(title): # gets the information of the show in general # Also gets the seasons and episdoes in a dict to use in the other file # iniatilize imdb object imdb = Imdb() imdb = Imdb(anonymize=True) title_json = imdb.search_for_title(title) if title_json == []: print('No Results Found') else: # get imdb id to get more information title_id = title_json[0]['imdb_id'] result = imdb.get_title_by_id(title_id) show_title = result.title year = result.year image_url = result.cover_url description = result.plot_outline temp = imdb.get_episodes(title_id) # build season dict to send back to main file seasons = {} episodes = {} season_counter = 1 for e in temp: # new dict entry for the next season, the number season of the show is the entry key if e.season > season_counter: # the current season is done, time to start building the next episiode dict seasons[season_counter] = episodes episodes = {} season_counter += 1 episodes[e.episode] = [e.title, e.release_date, e.imdb_id] return show_title, year, image_url, description, seasons
def receive_episodes(ep_no): try: global to_download imdb = Imdb() z = imdb.get_episodes(ep_no) for item in z: if item.release_date == yesterday: if item.season < 10: season = "0" + str(item.season) else: season = str(item.season) if item.episode < 10: ep = "0" + str(item.episode) else: ep = str(item.episode) new_item = item.series_name + ' s' + season + 'e' + ep + '\n' to_download = to_download + new_item except Exception as err5: err_msg5 = (str(err5), 'Error Origin: Receive Episodes') print(err_msg5) del err_msg5
class Watcher: def __init__(self): self.imdb = Imdb(anonymize=True) self.tracked_shows = self.get_shows() self.static_dir = os.path.join(os.path.dirname(__file__), '../static/images') def get_shows(self): """ gets all current popular shows from imdb """ shows = self.imdb.popular_shows() tracked_shows = [] for show in shows: tracked_shows_d = {} tracked_shows_d['id'] = show['tconst'] tracked_shows_d['title'] = show['title'] tracked_shows_d['poster'] = show['image']['url'] tracked_shows.append(tracked_shows_d) return tracked_shows def get_show_id(self, show_title): """ Gets show title id args: show_title: name of show to be queried returns: show_id: id of show """ for show in self.tracked_shows: if show_title == show['title']: return show['id'] def get_episodes(self, show_id): """ Gets all episodes of a given show args: show_id: tconst id from imdb returns: ist of episodes """ return self.imdb.get_episodes(show_id) def get_all_episodes(self): """ Gets all episodes args: None returns: list of episodes for all shows""" programs = {} for show in self.tracked_shows: programs[show['title']] = self.get_episodes(show['id']) return programs def get_poster(self, show_title): """ gets the img url for the poster of a show args: show_title: title of show returns: dictionary with {show_title: poster_url} """ #print(self.tracked_shows[show_id]['poster']) for show in self.tracked_shows: if show['title'] == show_title: return {show_title: show['poster']} def save_posters(self, urls, title): title = self.sanitize_title(title) dest = '{}/{}.jpg'.format(self.static_dir, title) urllib.request.urlretrieve(url, dest) def sanitize_title(self, title): forbidden = ('<', '>', ':', '"', '/', '\\', '|', '?', '*') for char in forbidden: title = title.replace(char, '') return title def get_show_titles(self): """ Gets show titles args: None returns: list of show titles """ return [show['title'] for show in self.tracked_shows]
# https://github.com/richardasaurus/imdb-pie from imdbpie import Imdb imdb = Imdb() imdb = Imdb(anonymize=True) print(imdb.search_for_title("The Dark Knight")) print() print(imdb.search_for_person("Christian Bale")) print() print(imdb.get_episodes('tt0096697')) top250 = imdb.top_250() for i in range(0, len(top250)): print(top250[i]) print() title = imdb.get_title_by_id("tt1210166") for person in title.credits: # check if they are a writer if person.token == 'writers': print(person.name + ' is a writer') else: print(person.name + ' is not a writer')
def test_get_episodes_raises_when_exclude_episodes_enabled(self): imdb = Imdb(locale='en_US', cache=False, exclude_episodes=True) with pytest.raises(ValueError): imdb.get_episodes('tt0303461')
def test_get_episodes_raises_when_exclude_episodes_enabled(self): imdb = Imdb(locale="en_US", cache=False, exclude_episodes=True) with pytest.raises(ValueError): imdb.get_episodes("tt0303461")
async def imdb(query, api: Imdb, localize): """ Send an api request to imdb using the search query :param query: the search query :param api: the imdb api object :param localize: the localization strings :return: the result """ # FIXME: Use Aiohttp instead of this api wrapper try: names = lambda x: ', '.join((p.name for p in x)) if x else 'N/A' null_check = lambda x: x if x and not isinstance(x, int) else 'N/A' id_ = api.search_for_title(query)[0]['imdb_id'] res = api.get_title_by_id(id_) eps = api.get_episodes(id_) if res.type == 'tv_series' else None ep_count = len(eps) if eps is not None else None season_count = eps[-1].season if eps is not None else None title = null_check(res.title) release = null_check(res.release_date) runtime = res.runtime if runtime is not None: hours, seconds = divmod(runtime, 3600) minutes = seconds / 60 runtime_str = '{} {} {} {}'.format(round(hours), localize['hours'], round(minutes), localize['minutes']) else: runtime_str = 'N/A' rated = null_check(res.certification) genre = ', '.join(res.genres) if res.genres else 'N/A' director = names(res.directors_summary) writer = names(res.writers_summary) cast = names(res.cast_summary) plot = null_check(res.plot_outline) poster = res.poster_url score = f'{res.rating}/10' if res.rating is not None else 'N/A' embed = Embed(colour=0xE5BC26) embed.set_author(name=title) if poster: embed.set_image(url=poster) if season_count is not None: embed.add_field(name=localize['seasons'], value=season_count) if ep_count is not None: embed.add_field(name=localize['episodes'], value=str(ep_count)) embed.add_field(name=localize['release_date'], value=release) embed.add_field(name=localize['rated'], value=rated) embed.add_field(name=localize['runtime'], value=runtime_str) embed.add_field(name=localize['genre'], value=genre) embed.add_field(name=localize['director'], value=director) embed.add_field(name=localize['writer'], value=writer) embed.add_field(name=localize['cast'], value=cast) embed.add_field(name=localize['score'], value=score) embed.add_field(name=localize['plot_outline'], value=plot, inline=False) return embed except (JSONDecodeError, IndexError): return localize['title_not_found']