コード例 #1
0
def get_show_aliases(session: sqlalchemy.orm.Session, tmdb_id: int, is_movie: bool) \
        -> List[TmdbAlias]:
    """
    Search for a show's aliases, using TMDB.

    :param session: the db session.
    :param tmdb_id: the tmdb id of the show.
    :param is_movie: if the show is a movie.
    :return: the list of TmdbAlias.
    """

    if is_movie:
        show_type = 'movie'
    else:
        show_type = 'tv'

    cache_key = 'tmdb|aliases|%s-%s' % (show_type, tmdb_id)

    cache_entry = db_calls.get_cache(session, cache_key)

    # If there's a valid entry of cache for this request
    if cache_entry:
        response = cache_entry.result
    else:
        # Make the request
        request = urllib.request.Request(
            'https://api.themoviedb.org/3/%s/%s/alternative_titles?api_key=%s'
            % (show_type, tmdb_id, configuration.tmdb_key))

        try:
            response = urllib.request.urlopen(request).read()
        except urllib.error.HTTPError:
            return []

        # Save the result in the cache
        db_calls.register_cache(session, cache_key, response.decode("utf-8"))

    # Parse the response
    response_dict = json.loads(response)

    # Create a TmdbTranslation for each entry
    tmdb_aliases = []

    if is_movie:
        for entry in response_dict['titles']:
            tmdb_alias = TmdbAlias()
            tmdb_alias.fill_from_dict(tmdb_id, entry)

            tmdb_aliases.append(tmdb_alias)
    else:
        for entry in response_dict['results']:
            tmdb_alias = TmdbAlias()
            tmdb_alias.fill_from_dict(tmdb_id, entry)

            tmdb_aliases.append(tmdb_alias)

    return tmdb_aliases
コード例 #2
0
def search_shows_by_text(session: sqlalchemy.orm.Session, search_text: str, is_movie: bool) -> List[TraktShow]:
    """
    Search shows by text, using trakt.

    :param session: the db session.
    :param search_text: the search text.
    :param is_movie: if the show is a movie.
    :return: the list of TraktShow.
    """

    if is_movie:
        show_type = 'movie'
    else:
        show_type = 'show'

    cache_key = 'trakt|text|%s-%s' % (show_type, search_text)

    cache_entry = db_calls.get_cache(session, cache_key)

    # If there's a valid entry of cache for this request
    if cache_entry:
        response = cache_entry.result
    else:
        # Make the request
        request = urllib.request.Request('https://api.trakt.tv/search/%s?extended=full&query=%s'
                                         % (show_type, urllib.parse.quote(search_text)))
        request.add_header('trakt-api-key', configuration.trakt_key)

        try:
            response = urllib.request.urlopen(request).read()
        except urllib.error.HTTPError:
            return []

        # Save the result in the cache
        db_calls.register_cache(session, cache_key, response.decode("utf-8"))

    # Parse the response
    response_dict = json.loads(response)

    # Create a TraktShow for each entry
    trakt_shows = []

    for entry in response_dict:
        if entry['type'] != show_type:
            continue

        else:
            trakt_shows.append(TraktShow(entry, show_type))

    return trakt_shows
コード例 #3
0
def get_show_using_id(session: sqlalchemy.orm.Session, tmdb_id: int, is_movie: bool, language: str = None) \
        -> Optional[TmdbShow]:
    """
    Get a show's information, from TMDB.

    :param session: the db session.
    :param tmdb_id: the tmdb id of the show.
    :param is_movie: if the show is a movie.
    :param language: the language in which we want the response (pt-PT, en-US...).
    :return: the TmdbShow.
    """

    if is_movie:
        show_type = 'movie'
    else:
        show_type = 'tv'

    cache_key = 'tmdb|id|%s-%s-%s' % (show_type, language, tmdb_id)

    cache_entry = db_calls.get_cache(session, cache_key)

    # If there's a valid entry of cache for this request
    if cache_entry:
        response = cache_entry.result
    else:
        # Make the request
        url = 'https://api.themoviedb.org/3/%s/%s?api_key=%s' % (
            show_type, tmdb_id, configuration.tmdb_key)

        if language is not None:
            url += '&language=%s' % language

        show_request = urllib.request.Request(url)

        try:
            response = urllib.request.urlopen(show_request).read()
        except urllib.error.HTTPError:
            return None

        # Save the result in the cache
        db_calls.register_cache(session, cache_key, response.decode("utf-8"))

    # Parse the response to json
    response_dict = json.loads(response)

    tmdb_show = TmdbShow()
    tmdb_show.fill_from_dict(response_dict, is_movie)

    return tmdb_show
コード例 #4
0
def search_show_by_id(session: sqlalchemy.orm.Session, trakt_id: int, is_movie: bool) -> Optional[TraktShow]:
    """
    Get a show's information, from trakt.

    :param session: the db session.
    :param trakt_id: the trakt id of the show.
    :param is_movie: if the show is a movie.
    :return: the TraktShow.
    """

    if is_movie:
        show_type = 'movie'
    else:
        show_type = 'show'

    cache_key = 'trakt|id|%s-%s' % (show_type, trakt_id)

    cache_entry = db_calls.get_cache(session, cache_key)

    # If there's a valid entry of cache for this request
    if cache_entry:
        response = cache_entry.result
    else:
        # Make the request
        request = urllib.request.Request('https://api.trakt.tv/search/trakt/%s?extended=full&id_type=trakt&type=%s'
                                         % (trakt_id, show_type))
        request.add_header('trakt-api-key', configuration.trakt_key)

        try:
            response = urllib.request.urlopen(request).read()
        except urllib.error.HTTPError:
            return None

        # Save the result in the cache
        db_calls.register_cache(session, cache_key, response.decode("utf-8"))

    # Parse the list of translations from the request
    response_dict = json.loads(response)

    # Choose the correct entry from the response
    for entry in response_dict:
        if entry['type'] != show_type:
            continue

        else:
            return TraktShow(entry, show_type)

    return None
コード例 #5
0
def get_show_aliases(session: sqlalchemy.orm.Session, trakt_slug: str, is_movie: bool) -> List[TraktAlias]:
    """
    Get a show's aliases.

    :param session: the db session.
    :param trakt_slug: the trakt slug of the show.
    :param is_movie: if the show is a movie.
    :return: a list of TraktAlias.
    """

    if is_movie:
        show_type = 'movie'
    else:
        show_type = 'show'

    cache_key = 'trakt|aliases|%s-%s' % (show_type, trakt_slug)

    cache_entry = db_calls.get_cache(session, cache_key)

    # If there's a valid entry of cache for this request
    if cache_entry:
        response = cache_entry.result
    else:
        # Make the request
        request = urllib.request.Request('https://api.trakt.tv/%ss/%s/aliases' % (show_type, trakt_slug))
        request.add_header('trakt-api-key', configuration.trakt_key)

        try:
            response = urllib.request.urlopen(request).read()
        except urllib.error.HTTPError:
            print('Slug was not found!')
            return []

        # Save the result in the cache
        db_calls.register_cache(session, cache_key, response.decode("utf-8"))

    # Parse the list of aliases from the request
    response_dict = json.loads(response)

    aliases = []

    for entry in response_dict:
        aliases.append(TraktAlias(entry))

    return aliases
コード例 #6
0
def search_shows_by_text(session: sqlalchemy.orm.Session,
                         search_text: str,
                         language: str = None,
                         is_movie: bool = None,
                         page: int = 1,
                         show_adult: bool = False,
                         year: int = None) -> Tuple[int, List[TmdbShow]]:
    """
    Search shows by text, using TMDB.

    :param session: the db session.
    :param search_text: the search text.
    :param is_movie: if the show is a movie.
    :param language: the language in which we want the response (pt-PT, en-US...).
    :param page: the page to obtain.
    :param show_adult: whether to show adult results or not.
    :param year: the year of the show.
    :return: a tuple with the total number of pages and the list of TmdbShow.
    """

    if is_movie is None:
        show_type = 'multi'
    else:
        if is_movie:
            show_type = 'movie'
        else:
            show_type = 'tv'

    # The values need to be like this
    if show_adult:
        include_adult = 'true'
    else:
        include_adult = 'false'

    cache_key = 'tmdb|text|%s-%s-%s-%s-%s-%s' % (
        show_type, language, search_text, include_adult, page, year)

    cache_entry = db_calls.get_cache(session, cache_key)

    # If there's a valid entry of cache for this request
    if cache_entry:
        response = cache_entry.result
    else:
        # Create the url
        url = 'https://api.themoviedb.org/3/search/%s?api_key=%s&language=%s&query=%s&include_adult=%s' \
              % (show_type, configuration.tmdb_key, language, urllib.parse.quote(search_text), include_adult)

        if year is not None:
            url += '&year=%d' % year

        # Make the request
        search_request = urllib.request.Request(url)

        # Add the page, when it exists
        if page:
            search_request.full_url = '%s&page=%d' % (search_request.full_url,
                                                      page)

        try:
            response = urllib.request.urlopen(search_request).read()
        except urllib.error.HTTPError:
            return 0, []

        # Save the result in the cache
        db_calls.register_cache(session, cache_key, response.decode("utf-8"))

    # Parse the response
    response_dict = json.loads(response)

    # Create a TmdbShow for each entry
    tmdb_shows = []

    for entry in response_dict['results']:
        # TODO: Need to change this, if I want to allow for searches to include people
        if show_type != 'multi' or entry['media_type'] == 'tv' or entry[
                'media_type'] == 'movie':
            tmdb_show = TmdbShow()
            tmdb_show.fill_from_dict(entry, is_movie)

            tmdb_shows.append(tmdb_show)
        elif entry['media_type'] == 'person':
            # Get the "known for" entries in the people found
            for show in entry['known_for']:
                tmdb_show = TmdbShow()
                tmdb_show.fill_from_dict(show, is_movie,
                                         entry['known_for_department'])

                tmdb_shows.append(tmdb_show)

    return response_dict['total_pages'], tmdb_shows