Ejemplo n.º 1
0
def update_upcoming_episodes_dates():
    today_date = datetime.today().date()

    seasons_to_check = Episode.objects.select_related('tmdb_season') \
        .filter(Q(tmdb_release_date__gte=today_date) | Q(tmdb_release_date=None)) \
        .exclude(tmdb_season__tmdb_season_number=0) \
        .values('tmdb_season__tmdb_id', 'tmdb_season__tmdb_show__tmdb_id', 'tmdb_season__tmdb_season_number') \
        .distinct()

    for season in seasons_to_check:
        show_id = season['tmdb_season__tmdb_show__tmdb_id']
        season_number = season['tmdb_season__tmdb_season_number']
        tmdb_season_id = season['tmdb_season__tmdb_id']
        key = get_tmdb_season_key(show_id, season_number)
        try:
            tmdb_season = tmdb.TV_Seasons(
                show_id, season_number).info(language=LANGUAGE)
        except HTTPError:
            continue
        cache.set(key, tmdb_season, CACHE_TIMEOUT)

        episodes = tmdb_season.get('episodes')
        existed_episodes = Episode.objects.filter(
            tmdb_id__in=[episode.get('id') for episode in episodes])
        episodes_to_create = []

        for episode in episodes:
            exists = False

            for existed_episode in existed_episodes:
                if episode['id'] == existed_episode.tmdb_id:
                    exists = True

                    new_fields = {
                        'tmdb_id':
                        episode.get('id'),
                        'tmdb_episode_number':
                        episode.get('episode_number'),
                        'tmdb_season_id':
                        tmdb_season_id,
                        'tmdb_name':
                        episode.get('name'),
                        'tmdb_release_date':
                        episode.get('air_date')
                        if episode.get('air_date') != "" else None
                    }
                    update_fields_if_needed(existed_episode, new_fields)
                    break

            if not exists:
                episodes_to_create.append(
                    Episode(tmdb_id=episode.get('id'),
                            tmdb_episode_number=episode.get('episode_number'),
                            tmdb_season_id=tmdb_season_id,
                            tmdb_name=episode.get('name')))

            print(episode['name'], episode['id'])
        Episode.objects.bulk_create(episodes_to_create)
Ejemplo n.º 2
0
    def retrieve(self, request, *args, **kwargs):
        try:
            rawg_game, returned_from_cache = get_rawg_game(kwargs.get('slug'))
        except KeyError:
            return Response({ERROR: GAME_NOT_FOUND},
                            status=status.HTTP_404_NOT_FOUND)
        except JSONDecodeError:
            return Response({ERROR: RAWG_UNAVAILABLE},
                            status=status.HTTP_503_SERVICE_UNAVAILABLE)

        try:
            results = get_hltb_search_result(rawg_game.get('name'))
            hltb_game = max(results,
                            key=lambda element: element.similarity).__dict__
            hltb_game_name = hltb_game.get('game_name')
            hltb_game_id = hltb_game.get('game_id')
        except (ValueError, TypeError, ConnectionError):
            hltb_game = None
            hltb_game_name = ''
            hltb_game_id = None

        new_fields = {
            'rawg_slug': rawg_game.get('slug'),
            'rawg_name': rawg_game.get('name'),
            'rawg_release_date': rawg_game.get('released'),
            'rawg_tba': rawg_game.get('tba'),
            'hltb_name': hltb_game_name,
            'hltb_id': hltb_game_id
        }

        with transaction.atomic():
            game, created = Game.objects.select_for_update().get_or_create(
                rawg_id=rawg_game.get('id'), defaults=new_fields)
            if not created and not returned_from_cache:
                update_fields_if_needed(game, new_fields)

        if created or not returned_from_cache:
            for genre in rawg_game.get('genres'):
                genre_obj, created = Genre.objects.get_or_create(
                    rawg_id=genre.get('id'),
                    defaults={
                        'rawg_name': genre.get('name'),
                        'rawg_slug': genre.get('slug')
                    })
                GameGenre.objects.get_or_create(genre=genre_obj, game=game)

        rawg_game.update({
            'playtime':
            f'{rawg_game.get("playtime")} {int_to_hours(rawg_game.get("playtime"))}'
        })
        translate_hltb_time(hltb_game, 'gameplay_main', 'gameplay_main_unit')
        translate_hltb_time(hltb_game, 'gameplay_main_extra',
                            'gameplay_main_extra_unit')
        translate_hltb_time(hltb_game, 'gameplay_completionist',
                            'gameplay_completionist_unit')

        return Response({'rawg': rawg_game, 'hltb': hltb_game})
Ejemplo n.º 3
0
    def retrieve(self, request, *args, **kwargs):
        try:
            tmdb_movie, returned_from_cache = get_tmdb_movie(
                kwargs.get('tmdb_id'))
            tmdb_cast_crew = get_cast_crew(kwargs.get('tmdb_id'))
            tmdb_movie['cast'] = tmdb_cast_crew.get('cast')
            tmdb_movie['crew'] = tmdb_cast_crew.get('crew')
        except HTTPError as e:
            error_code = int(e.args[0].split(' ', 1)[0])
            if error_code == 404:
                return Response({ERROR: MOVIE_NOT_FOUND},
                                status=status.HTTP_404_NOT_FOUND)
            return Response({ERROR: TMDB_UNAVAILABLE},
                            status=status.HTTP_503_SERVICE_UNAVAILABLE)
        except ConnectionError:
            return Response({ERROR: TMDB_UNAVAILABLE},
                            status=status.HTTP_503_SERVICE_UNAVAILABLE)

        new_fields = {
            'imdb_id':
            tmdb_movie.get('imdb_id'),
            'tmdb_original_name':
            tmdb_movie.get('original_title'),
            'tmdb_name':
            tmdb_movie.get('title'),
            'tmdb_runtime':
            tmdb_movie.get('runtime'),
            'tmdb_release_date':
            tmdb_movie.get('release_date')
            if tmdb_movie.get('release_date') != "" else None
        }

        with transaction.atomic():
            movie, created = Movie.objects.select_for_update().get_or_create(
                tmdb_id=tmdb_movie.get('id'), defaults=new_fields)
            if not created and not returned_from_cache:
                update_fields_if_needed(movie, new_fields)

        if created or not returned_from_cache:
            for genre in tmdb_movie.get('genres'):
                genre_obj, created = Genre.objects.get_or_create(
                    tmdb_id=genre.get('id'),
                    defaults={'tmdb_name': genre.get('name')})
                MovieGenre.objects.get_or_create(genre=genre_obj, movie=movie)

        return Response({'tmdb': tmdb_movie})
Ejemplo n.º 4
0
def update_upcoming_shows_dates():
    today_date = datetime.today().date()

    shows = Show.objects \
        .filter(Q(tmdb_release_date__gte=today_date) | Q(tmdb_release_date=None))

    for show in shows:
        tmdb_id = show.tmdb_id
        key = get_tmdb_show_key(tmdb_id)
        tmdb_show = tmdb.TV(tmdb_id).info(language=LANGUAGE)
        cache.set(key, tmdb_show, CACHE_TIMEOUT)
        update_fields_if_needed(
            show, {
                'tmdb_release_date':
                tmdb_show.get('first_air_date')
                if tmdb_show.get('first_air_date') != "" else None
            })
        print(show.tmdb_name)
Ejemplo n.º 5
0
def update_upcoming_movies_dates():
    today_date = datetime.today().date()

    movies = Movie.objects \
        .filter(Q(tmdb_release_date__gte=today_date) | Q(tmdb_release_date=None))

    for movie in movies:
        tmdb_id = movie.tmdb_id
        key = get_tmdb_movie_key(tmdb_id)
        tmdb_movie = tmdb.Movies(tmdb_id).info(language=LANGUAGE)
        cache.set(key, tmdb_movie, CACHE_TIMEOUT)
        update_fields_if_needed(
            movie, {
                'tmdb_release_date':
                tmdb_movie.get('release_date')
                if tmdb_movie.get('release_date') != "" else None
            })
        print(movie.tmdb_name)
Ejemplo n.º 6
0
def update_upcoming_games_dates():
    today_date = datetime.today().date()

    games = Game.objects \
        .filter(Q(rawg_release_date__gte=today_date) | (Q(rawg_release_date=None) & Q(rawg_tba=True)))

    for game in games:
        slug = game.rawg_slug
        key = get_rawg_game_key(slug)
        rawg_game = rawg.get_game(slug).json
        cache.set(key, rawg_game, CACHE_TIMEOUT)
        update_fields_if_needed(
            game,
            {
                'rawg_release_date': rawg_game.get('released'),
                'rawg_tba': rawg_game.get('tba')
            },
        )
        print(game.rawg_name)
Ejemplo n.º 7
0
    def retrieve(self, request, *args, **kwargs):
        try:
            tmdb_show, returned_from_cache = get_show(kwargs.get('tmdb_id'))
        except HTTPError as e:
            error_code = int(e.args[0].split(' ', 1)[0])
            if error_code == 404:
                return Response({ERROR: SHOW_NOT_FOUND}, status=status.HTTP_404_NOT_FOUND)
            return Response({ERROR: TMDB_UNAVAILABLE}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
        except ConnectionError:
            return Response({ERROR: TMDB_UNAVAILABLE}, status=status.HTTP_503_SERVICE_UNAVAILABLE)

        episode_run_time = tmdb_show['episode_run_time'][0] if len(tmdb_show['episode_run_time']) > 0 else 0

        new_fields = {
            'tmdb_original_name': tmdb_show['original_name'],
            'tmdb_name': tmdb_show['name'],
            'tmdb_episode_run_time': episode_run_time,
            'tmdb_backdrop_path': tmdb_show['backdrop_path'],
            'tmdb_release_date': tmdb_show['first_air_date'] if tmdb_show['first_air_date'] != "" else None
        }

        with transaction.atomic():
            show, created = Show.objects.select_for_update().get_or_create(tmdb_id=tmdb_show['id'],
                                                                           defaults=new_fields)
            if not created and not returned_from_cache:
                update_fields_if_needed(show, new_fields)

        if created or not returned_from_cache:
            for genre in tmdb_show.get('genres'):
                genre_obj, created = Genre.objects.get_or_create(tmdb_id=genre.get('id'),
                                                                 defaults={
                                                                     'tmdb_name': genre.get('name')
                                                                 })
                ShowGenre.objects.get_or_create(genre=genre_obj, show=show)

        return Response({'tmdb': tmdb_show})
Ejemplo n.º 8
0
    def retrieve(self, request, *args, **kwargs):
        try:
            tmdb_season, returned_from_cache = get_season(kwargs.get('show_tmdb_id'),
                                                          kwargs.get('number'))
        except HTTPError as e:
            error_code = int(e.args[0].split(' ', 1)[0])
            if error_code == 404:
                return Response({ERROR: SEASON_NOT_FOUND}, status=status.HTTP_404_NOT_FOUND)
            return Response({ERROR: TMDB_UNAVAILABLE}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
        except ConnectionError:
            return Response({ERROR: TMDB_UNAVAILABLE}, status=status.HTTP_503_SERVICE_UNAVAILABLE)

        try:
            show_info = get_show_info(kwargs.get('show_tmdb_id'))
        except Show.DoesNotExist:
            return Response({ERROR: SHOW_NOT_FOUND}, status=status.HTTP_404_NOT_FOUND)

        tmdb_season.update(show_info)

        new_fields = {
            'tmdb_season_number': tmdb_season.get('season_number'),
            'tmdb_name': tmdb_season.get('name'),
            'tmdb_show_id': tmdb_season.get('show_pk')
        }

        with transaction.atomic():
            season, created = Season.objects.select_for_update().get_or_create(tmdb_id=tmdb_season.get('id'),
                                                                               defaults=new_fields)
            if not created and not returned_from_cache:
                update_fields_if_needed(season, new_fields)

        with transaction.atomic():
            episodes = tmdb_season.get('episodes')
            existed_episodes = Episode.objects.select_related('tmdb_season') \
                .select_for_update().filter(tmdb_season=season)
            episodes_to_create = []
            for episode in episodes:
                exists = False
                for existed_episode in existed_episodes:
                    if episode['episode_number'] == existed_episode.tmdb_episode_number:
                        exists = True
                        new_fields = {
                            'tmdb_id': episode.get('id'),
                            'tmdb_season': season,
                            'tmdb_name': episode.get('name'),
                            'tmdb_release_date': episode.get('air_date') if episode.get('air_date') != "" else None
                        }
                        update_fields_if_needed(existed_episode, new_fields)
                        break

                if not exists:
                    episodes_to_create.append(Episode(tmdb_id=episode.get('id'),
                                                      tmdb_episode_number=episode.get('episode_number'),
                                                      tmdb_season=season,
                                                      tmdb_release_date=episode.get('air_date')
                                                      if episode.get('air_date') != "" else None,
                                                      tmdb_name=episode.get('name')))

            Episode.objects.bulk_create(episodes_to_create)

        return Response({'tmdb': tmdb_season})