def search_by_id(query, id_type='imdb'): """Perform a search query by using a Trakt.tv ID or other external ID :param query: Your search string :param id_type: The type of object you're looking for. Must be one of 'trakt-movie', 'trakt-show', 'trakt-episode', 'imdb', 'tmdb', 'tvdb' or 'tvrage' """ valids = ('trakt-movie', 'trakt-show', 'trakt-episode', 'imdb', 'tmdb', 'tvdb', 'tvrage') if id_type not in valids: raise ValueError('search_type must be one of {}'.format(valids)) data = yield 'search?id={query}&id_type={id_type}'.format( query=slugify(query), id_type=id_type) for media_item in data: extract_ids(media_item) results = [] for d in data: if 'episode' in d: from trakt.tv import TVEpisode show = d.pop('show') extract_ids(d['episode']) results.append(TVEpisode(show['title'], **d['episode'])) elif 'movie' in d: from trakt.movies import Movie results.append(Movie(**d.pop('movie'))) elif 'show' in d: from trakt.tv import TVShow results.append(TVShow(**d.pop('show'))) elif 'person' in d: from trakt.people import Person results.append(Person(**d.pop('person'))) yield results
def search_by_id(query, id_type='imdb', media_type=None): """Perform a search query by using a Trakt.tv ID or other external ID :param query: Your search string, which should be an ID from your source :param id_type: The source of the ID you're looking for. Must be one of 'trakt', trakt-movie', 'trakt-show', 'trakt-episode', 'trakt-person', 'imdb', 'tmdb', or 'tvdb' :param media_type: The type of media you're looking for. May be one of 'movie', 'show', 'episode', or 'person', or a comma-separated list of any combination of those. Null by default, which will return all types of media that match the ID given. """ valids = ('trakt', 'trakt-movie', 'trakt-show', 'trakt-episode', 'trakt-person', 'imdb', 'tmdb', 'tvdb') id_types = {'trakt': 'trakt', 'trakt-movie': 'trakt', 'trakt-show': 'trakt', 'trakt-episode': 'trakt', 'trakt-person': 'trakt', 'imdb': 'imdb', 'tmdb': 'tmdb', 'tvdb': 'tvdb'} if id_type not in valids: raise ValueError('search_type must be one of {}'.format(valids)) source = id_types.get(id_type) media_types = {'trakt-movie': 'movie', 'trakt-show': 'show', 'trakt-episode': 'episode', 'trakt-person': 'person'} # If there was no media_type passed in, see if we can guess based off the # ID source. None is still an option here, as that will return all possible # types for a given source. if media_type is None: media_type = media_types.get(source, None) # If media_type is still none, don't add it as a parameter to the search if media_type is None: uri = 'search/{source}/{query}'.format( query=slugify(query), source=source) else: uri = 'search/{source}/{query}?type={media_type}'.format( query=slugify(query), source=source, media_type=media_type) data = yield uri for media_item in data: extract_ids(media_item) results = [] for d in data: if 'episode' in d: from trakt.tv import TVEpisode show = d.pop('show') extract_ids(d['episode']) results.append(TVEpisode(show['title'], **d['episode'])) elif 'movie' in d: from trakt.movies import Movie results.append(Movie(**d.pop('movie'))) elif 'show' in d: from trakt.tv import TVShow results.append(TVShow(**d.pop('show'))) elif 'person' in d: from trakt.people import Person results.append(Person(**d.pop('person'))) yield results
def test_get_person(): bryan = Person('Bryan Cranston') assert bryan.name == 'Bryan Cranston' assert bryan.birthday == '1956-03-07' assert bryan.death is None assert bryan.birthplace == 'San Fernando Valley, California, USA' assert bryan.homepage == 'http://www.bryancranston.com/'
def test_get_movie_credits(): bryan = Person('Bryan Cranston') assert isinstance(bryan.movie_credits, MovieCredits) assert len(bryan.movie_credits.cast) == 1 assert len(bryan.movie_credits.crew) == 3 for job in ('directing', 'writing', 'production'): assert job in bryan.movie_credits.crew
def test_credit_magic_methods(): bryan = Person('Bryan Cranston') for credits in (bryan.movie_credits, bryan.tv_credits): assert isinstance(credits.cast, list) assert isinstance(credits.crew, dict) for credit in credits.cast: assert str(credit).startswith('<ActingCredit>') for department, dep_credits in credits.crew.items(): for credit in dep_credits: assert str(credit).startswith('<CrewCredit>')
def people(self): """A :const:`list` of all of the :class:`People` involved in this :class:`TVShow`, including both cast and crew """ if self._people is None: data = yield (self.ext + '/people') crew = data.get('crew', {}) cast = [] for c in data.get('cast', []): person = c.pop('person') character = c.pop('character') cast.append(Person(character=character, **person)) _crew = [] for key in crew: for department in crew.get(key): # lists person = department.get('person') person.update({'job': department.get('job')}) _crew.append(Person(**person)) self._people = cast + _crew yield self._people
def get_search_results(query, search_type=None, slugify_query=False): """Perform a search query against all of trakt's media types. :param query: Your search string :param search_type: The types of objects you're looking for. Must be specified as a list of strings containing any of 'movie', 'show', 'episode', or 'person'. :param slugify_query: A boolean indicating whether or not the provided query should be slugified or not prior to executing the query. """ # if no search type was specified, then search everything if search_type is None: search_type = ['movie', 'show', 'episode', 'person'] # If requested, slugify the query prior to running the search if slugify_query: query = slugify(query) uri = 'search/{type}?query={query}'.format( query=query, type=','.join(search_type)) data = yield uri # Need to do imports here to prevent circular imports with modules that # need to import Scrobblers results = [] for media_item in data: extract_ids(media_item) result = SearchResult(media_item['type'], media_item['score']) if media_item['type'] == 'movie': from trakt.movies import Movie result.media = Movie(**media_item.pop('movie')) elif media_item['type'] == 'show': from trakt.tv import TVShow result.media = TVShow(**media_item.pop('show')) elif media_item['type'] == 'episode': from trakt.tv import TVEpisode show = media_item.pop('show') result.media = TVEpisode(show.get('title', None), **media_item.pop('episode')) elif media_item['type'] == 'person': from trakt.people import Person result.media = Person(**media_item.pop('person')) results.append(result) yield results
def search(query, search_type='movie', year=None): """Perform a search query against all of trakt's media types :param query: Your search string :param search_type: The type of object you're looking for. Must be one of 'movie', 'show', 'movie,show', 'episode', or 'person' """ valids = ('movie', 'show', 'episode', 'person', 'movie,show') if search_type not in valids: raise ValueError('search_type must be one of {}'.format(valids)) uri = 'search?query={query}&type={type}'.format( query=slugify(query), type=search_type) if year is not None: uri += '&year={}'.format(year) data = yield uri for media_item in data: extract_ids(media_item) # Need to do imports here to prevent circular imports with modules that # need to import Scrobblers if search_type == 'movie': from trakt.movies import Movie yield [Movie(d['score'], **d.pop('movie')) for d in data] elif search_type == 'show': from trakt.tv import TVShow yield [TVShow(d['score'], **d.pop('show')) for d in data] elif search_type == 'movie,show': from trakt.movies import Movie from trakt.tv import TVShow yield [Movie(d['score'], **d.pop('movie')) if d['type'] == 'movie' else TVShow(d['score'], **d.pop('show')) for d in data] elif search_type == 'episode': from trakt.tv import TVEpisode episodes = [] for episode in data: show = episode.pop('show') extract_ids(episode['episode']) episodes.append(TVEpisode(show.get('title', None), **episode['episode'])) yield episodes elif search_type == 'person': from trakt.people import Person yield [Person(**d.pop('person')) for d in data]
def get_items(self): """A list of the list items using class instances instance types: movie, show, season, episode, person """ data = yield 'users/{user}/lists/{id}/items'.format(user=slugify( self.creator), id=self.slug) for item in data: # match list item type if 'type' not in item: continue item_type = item['type'] item_data = item.pop(item_type) extract_ids(item_data) if item_type == 'movie': self._items.append( Movie(item_data['title'], item_data['year'], item_data['slug'])) elif item_type == 'show': self._items.append( TVShow(item_data['title'], item_data['slug'])) elif item_type == 'season': show_data = item.pop('show') extract_ids(show_data) season = TVSeason(show_data['title'], item_data['number'], show_data['slug']) self._items.append(season) elif item_type == 'episode': show_data = item.pop('show') extract_ids(show_data) episode = TVEpisode(show_data['title'], item_data['season'], item_data['number']) self._items.append(episode) elif item_type == 'person': self._items.append(Person(item_data['name'], item_data['slug'])) yield self._items
def test_person_search(): results = Person.search("cranston") assert len(results) == 10 assert all(isinstance(p, Person) for p in results)
def get_items(self): """A list of the list items using class instances instance types: movie, show, season, episode, person """ data = yield 'users/{user}/lists/{id}/items'.format(user=self.creator, id=self.slug) for item in data: # match list item type if 'type' not in item: continue item_type = item['type'] item_data = item.pop(item_type) extract_ids(item_data) if item_type == 'movie': movie = Movie(item_data.pop('title'), item_data.pop('year'), item_data.pop('slug'), **item_data) self._items.append(movie) elif item_type == 'show': show = TVShow(item_data.pop('title'), slug=item_data.pop('slug'), **item_data) seasons = show.seasons show._seasons = [] for season in seasons: season._episodes = [] show._seasons.append(season) self._items.append(show) elif item_type == 'season': show_data = item.pop('show') extract_ids(show_data) show = TVShow(show_data.pop('title'), slug=show_data.pop('slug'), **show_data) _season = TVSeason(show=show.title, season=item_data.pop('number'), slug=show.slug, **item_data) for season in show.seasons: if season.trakt != _season.trakt: continue season._episodes = [] show._seasons = [season] self._items.append(show) break # season._episodes = [] # show._seasons = [season] # self._items.append(show) # extract_ids(show_data) # show=TVShow(show_data['title'], show_data['slug']) # season = TVSeason( # show=show.title, # season=item_data['number'], # slug=show.slug, # **item_data # ) # self._items.append(self.__class__.ListTVSeason(show=show, season=season)) elif item_type == 'episode': show_data = item.pop('show') extract_ids(show_data) show = TVShow(show_data.pop('title'), slug=show_data.pop('slug'), **show_data) episode = TVEpisode(show=show.title, season=item_data.pop('season'), slug=show.slug, **item_data) for season in show.seasons: if season.season != episode.season: continue season._episodes = [episode] show._seasons = [season] self._items.append(show) break # show=TVShow(show_data['title'], show_data['slug']) # show._seasons = None # for season in show.seasons: # if season.season != item_data['number']: # continue # episode = TVEpisode(show.title, season.season, item_data['number'], show.slug) # self._items.append(self.__class__.ListTVEpisode(show=show, season=season, episode=episode)) # break elif item_type == 'person': self._items.append(Person(item_data['name'], item_data['slug'])) yield self._items
def test_get_tv_credits(): bryan = Person('Bryan Cranston') assert isinstance(bryan.tv_credits, TVCredits) assert len(bryan.tv_credits.cast) == 1 assert len(bryan.tv_credits.crew) == 1 assert 'production' in bryan.tv_credits.crew
def test_person_search(): results = Person.search('cranston') assert len(results) == 10 assert all(isinstance(p, Person) for p in results)
def test_person_magic_methods(): bryan = Person('Bryan Cranston') assert str(bryan) == '<Person>: {name}'.format(name=bryan.name) assert str(bryan) == repr(bryan)
def test_get_person_images(): bryan = Person('Bryan Cranston') assert isinstance(bryan.images, dict)
def com_net_trakt_get_person(self, person_name, search=False): if search: return Person(person_name) else: return Person.search(person_name)[0]