def resolve_rating(*_, code: int, type: str, rating: int) -> dict: logger = get_logger_instance() logger.info( "rating was queried -> type:'{}', code:'{}', rating:'{}'".format( type, code, rating)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_rating(database, external_id, type, code, rating)
def resolve_watch_status(*_, code: int, type: str, watchStatus: str) -> dict: logger = get_logger_instance() logger.info( "watchStatus was queried -> code:'{}', type:'{}', watchStatus:'{}'" .format(code, type, watchStatus)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_watch_status(database, external_id, code, type, watchStatus)
def resolve_user(*_) -> dict: """ Get user preferences :return: <dict> currently country & language fields """ logger = get_logger_instance() logger.info("user was queried") external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_user_i18n(external_id, db)
def resolve_favorite(*_, code: int, type: str, favorite: bool, seasonNumber: int, episodeNumber: int) -> dict: logger = get_logger_instance() logger.info( "favorite was queried -> type:'{}', code:'{}', seasonNumber:'{}', episodeNumber:'{}', favorite:'{}'" .format(type, code, seasonNumber, episodeNumber, favorite)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_favorite(tmdb, database, external_id, type, code, seasonNumber, episodeNumber, favorite)
def graphql_playground() -> str: """ If get request was made on "/", route to playground. :return: <str>, <statuscode> Return playground html with status code 200 """ logger = get_logger_instance() logger.debug("Routed to playground -> code:{}".format(200)) return PLAYGROUND_HTML, 200
def resolve_language(*_, language: str) -> dict: """ Mutation endpoint to set the current user's preferred language :param language: <str> RFC 5646 BCP language tag (e.g. 'de-DE' or 'en-US') :return: <dict> Dict containing status of mutation """ logger = get_logger_instance() logger.info("language was queried -> language:'{}'".format(language)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_language(database, external_id, language)
def resolve_country(*_, country: str) -> dict: """ Mutation endpoint to set the current user's preferred country :param country: <str> ISO 3166-1 alpha-2 country code (e.g. 'DE' or 'US') :return: <dict> Dict containing status of mutation """ logger = get_logger_instance() logger.info("country was queried -> country:'{}'".format(country)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_country(database, external_id, country)
def resolve_tv(*_, code: int) -> dict: """ API endpoint for "tv" queries. :param code: <int> Id of the tv show to get details for :return: <dict> Details of the tv show """ logger = get_logger_instance() logger.info("TV was queried -> code:{}".format(code)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_tv(external_id, tmdb, code, db)
def resolve_watch_count(*_, type: str) -> int: """ API endpoint for "watchCount" queries. :param type: <str> Media type, choose between "TV", "Movie", "Season" and "Episode" :return: <int> Count of watched media of provided type """ logger = get_logger_instance() logger.info("watchCount was queired -> type:'{}'".format(type)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_watch_count(tmdb, external_id, type, db)
def resolve_tv_seasons(*_, code: int) -> list: """ API endpoint for "tvSeasons" queries. :param code: <int> Code of the TV show to get the seasons for :return: list of dicts consisting of seasons """ logger = get_logger_instance() logger.info("TVSeasons was queried -> code:'{}'".format(code)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_tv_seasons(external_id, tmdb, code, db)
def resolve_watch_time(*_, type: str) -> int: """ API endpoint for "watchTime" queries. :param type: <str> Type to return count for (Movie, TV) :return: <int> Runtime of watched media of provided type """ logger = get_logger_instance() logger.info("watchTime was queried -> type:'{}'".format((type))) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_watch_time(tmdb, external_id, type, db)
def resolve_popular(*_, type: str, page: int) -> dict: """ API endpoint for "popular" queries. :param type: <str> Choose between "tv" or "movie" to get popular results :param page: <int> Search page (minimum:1 maximum:1000) :return: <dict> Details of the movie/tv """ logger = get_logger_instance() logger.info("Popular was queried -> type:'{}'".format(type)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_popular(external_id, tmdb, type, page, db)
def resolve_popular(*_, type: str, page: int) -> dict: """ API endpoint to get "popular" productions. :param type: <str> Media type, choose between "TV", "Movie" :param page: <int> Search page (minimum:1 maximum:1000) :return: <dict> Results of either popular TV shows or movies """ logger = get_logger_instance() logger.info("Popular was queried -> type:'{}'".format(type)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_popular(external_id, tmdb, type, page, db)
def resolve_number_of_watched_episodes( *_, code: int, seasonNumber: int, numberOfWatchedEpisodes: int) -> dict: logger = get_logger_instance() logger.info( "number of watched episodes was queried -> code:'{}', seasonNumber:'{}', numberOfWatchedEpisodes:'{}'" .format(code, seasonNumber, numberOfWatchedEpisodes)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_number_of_watched_episodes(tmdb, database, external_id, code, seasonNumber, numberOfWatchedEpisodes)
def resolve_search(*_, keyword: str, page: int) -> dict: """ API endpoint for "search" queries. :param keyword: <str> Search string :param page: <int> Search page (minimum:1 maximum:1000) :return: <dict> Results of the search """ logger = get_logger_instance() logger.info("Search was queried -> keyword:'{}'".format(keyword)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_search(external_id, tmdb, keyword, page, db)
def resolve_season(*_, code: int, seasonNumber: str) -> dict: """ API endpoint for "season" queries. :param code: <int> Id of the tv show to get details for :param seasonNumber: <int> Number of the season to get details for :return: <dict> Details of the season """ logger = get_logger_instance() logger.info("Season was queried -> code:{}, season_number:{}".format( code, seasonNumber)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_season(external_id, tmdb, code, seasonNumber, db)
def resolve_list(*_, type: str, watchStatus: str, favorite: bool) -> dict: """ API endpoint for "list" queries. :param type: <str> Choose between "tv", "movie", "season" and "episode" :param watchStatus: <str> Choose between "Plan to watch", "Watching", "Dropped" and "Finished" or "any" :param favorite: <bool> to query media marked as favorite (best used with watchStatus = "any") :return: <dict> Movie, TV, Season or Episode """ logger = get_logger_instance() logger.info("List was queried -> type:'{}', watchStatus:'{}'".format( type, watchStatus)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_list(tmdb, external_id, type, watchStatus, favorite, db)
def resolve_season_episodes(*_, code: str, seasonNumber: int) -> dict: """ API endpoint for "seasonEpisodes" queries. :param code: <int> Code of the TV show to get the episodes for :param seasonNumber: <int> Number of the season to get the episodes for :return: list of dicts consisting of episodes """ logger = get_logger_instance() logger.info( "seasonEpisodes was queried -> code:'{}', seasonNumber:'{}'". format(code, seasonNumber)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_season_episodes(external_id, tmdb, code, seasonNumber, db)
def resolve_rating(*_, code: int, type: str, rating: int) -> dict: """ Mutation endpoint to rate media. :param code: <int> TMDB code of the item :param type: <str> Media type, choose between "TV", "Movie" :param rating: <int> Rating of media to store for user. :return: <dict> Dict containing status of mutation """ logger = get_logger_instance() logger.info( "rating was queried -> type:'{}', code:'{}', rating:'{}'".format( type, code, rating)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_rating(database, external_id, type, code, rating)
def resolve_watch_status(*_, code: int, type: str, watchStatus: str) -> dict: """ Mutation endpoint to set the watchStatus of a TV show or movie :param code: <int> TMDB code of the item to change the watchStatus for :param type: <str> Media type, choose between "TV", "Movie", "Season" and "Episode" :param watchStatus: <str> Choose between "Plan to watch", "Watching", "Dropped" and "Finished" :return: <dict> Dict containing status of mutation """ logger = get_logger_instance() logger.info( "watchStatus was queried -> code:'{}', type:'{}', watchStatus:'{}'" .format(code, type, watchStatus)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_watch_status(database, external_id, code, type, watchStatus)
def resolve_discover(*_, type: str, person: int, similarTo: int, page: int) -> dict: """ Get list of recommended media based on user ratings/favorites (popular as fallback) :param type: <str> Type of the media to query :param person: <int> TMDB code of a person to get media w/ them (e.g. acotr or director) :param similar_to: <int> TMDB code of a movie/tv show to get similar media :param page: <int> Search page (minimum:1 maximum:1000) :return: <dict> recommended media with respect to the values of watchStatus, favorites and rating """ logger = get_logger_instance() logger.info("Discover was queried -> type:'{}'".format(type)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_discover(external_id, tmdb, type, person, similarTo, page, db)
def resolve_by_rating(*_, type: str, tmdbRating: float, minVotes: int, releasedFrom: str, page: int) -> dict: """ API endpoint for (top) rated queries. :param type: <str> Popular item type "movie" or "tv" :param vote_avg: <float> filter media with average rating greater than set value :param vote_count: <int> minimum number of votes :param released_from: <str> search for media released after specified date (YYYY-MM-DD) :param page: <int> Search page (minimum:1 maximum:1000) :return: <dict> Details of the movie/tv """ logger = get_logger_instance() logger.info("byRating was queried -> type:'{}'".format(type)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_by_rating(external_id, tmdb, type, tmdbRating, minVotes, releasedFrom, page, db)
def resolve_by_rating(*_, type: str, tmdbRating: float, minVotes: int, releasedFrom: str, page: int) -> dict: """ API endpoint to filter media based on rating. :param type: <str> Media type, choose between "TV", "Movie" :param tmdbRating: <float> Filter media with average rating greater than set value :param minVotes: <int> Filter results by minimum amount of votes :param releasedFrom: <str> Search for media released after specified date (YYYY-MM-DD) :param page: <int> Search page (minimum:1 maximum:1000) :return: <dict> Results of either TV shows or movies filtered by ratings """ logger = get_logger_instance() logger.info("byRating was queried -> type:'{}'".format(type)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return get_by_rating(external_id, tmdb, type, tmdbRating, minVotes, releasedFrom, page, db)
def resolve_favorite(*_, code: int, type: str, favorite: bool, seasonNumber: int, episodeNumber: int) -> dict: """ Mutation endpoint to set meida as favorite. :param code: <int> TMDB code of the item to set as favorite :param type: <str> Media type, choose between "TV", "Movie", "Season" and "Episode" :param favorite: <bool> Boolean to set favorite to :param seasonNumber: <int> Number of season if type is season else set to null :param episodeNumber: <int> Number of episode if type is episode else set to null :return: <dict> Dict containing status of mutation """ logger = get_logger_instance() logger.info( "favorite was queried -> type:'{}', code:'{}', seasonNumber:'{}', episodeNumber:'{}', favorite:'{}'" .format(type, code, seasonNumber, episodeNumber, favorite)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_favorite(tmdb, database, external_id, type, code, seasonNumber, episodeNumber, favorite)
def resolve_number_of_watched_episodes( *_, code: int, seasonNumber: int, numberOfWatchedEpisodes: int) -> dict: """ Mutation endpoint to set amount of watched episodes :param code: <int> TMDB code of the item :param seasonNumber: <int> Number of season the episodes are part of :param numberOfWatchedEpisodes: <int> Amount of watched episodes :return: <dict> Dict containing status of mutation """ logger = get_logger_instance() logger.info( "number of watched episodes was queried -> code:'{}', seasonNumber:'{}', numberOfWatchedEpisodes:'{}'" .format(code, seasonNumber, numberOfWatchedEpisodes)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_number_of_watched_episodes(tmdb, database, external_id, code, seasonNumber, numberOfWatchedEpisodes)
import datetime from consumatio.external.db.models import * from consumatio.external.logger import get_logger_instance from consumatio.exceptions.invalid_parameter import InvalidParameter logger = get_logger_instance() class Database(): def __init__(self, db): self.db = db def cache(self: object, query: str, body: str) -> None: """ Cache the provided query and the respective result in the database. :param query: <str> Request to store in cache :param body: <str> Response of the query to store :return: None """ cache = Cache(query, body) self.db.session.add(cache) self.db.session.commit() logger.info("Query was saved in the database") def is_cached(self: object, query: str) -> bool: """ Checks if a query is cached in the database. :param query: <str> Tmdb query string :return: <bool> Returns true if the query is cached, else false """
def resolve_language(*_, language: str) -> dict: logger = get_logger_instance() logger.info("language was queried -> language:'{}'".format(language)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_language(database, external_id, language)
def resolve_country(*_, country: str) -> dict: logger = get_logger_instance() logger.info("country was queried -> country:'{}'".format(country)) external_id = request.headers.get(CONSUMATIO_NAMESPACE_HEADER_KEY) return set_country(database, external_id, country)