def get_full_series(result): """ Retrieves the full series via id from tmdb. Args: result (dict): Search result from tmdb. Returns: False: On failure dict: Full series if successfull """ tmdb_tv = TV() # start the task to get the full series from tmdb full_series = tmdb_tv.get_series_info_by_id(result['id']) # wait for the task to finish or fail while full_series.status in states.UNREADY_STATES: time.sleep(0.1) # if the task exited successfull if full_series.status == states.SUCCESS: # the series has seasons and episodes if (full_series.result and full_series.result['number_of_seasons'] and (full_series.result['number_of_seasons'] != 0) and full_series.result['number_of_episodes'] and (full_series.result['number_of_episodes'] != 0)): return full_series.result return False
def _convert_season(tmdb_series_id, season_number, new_series): """Retrieve, convert and store a season of the series with it's episodes Args: tmdb_series_id (int): TMDB ID of the series season_number (int): Number of the season to be converted new_series (Series): Tellylog series the season belongs to Returns: False: on failure True: on success """ tmdb_tv = TV() # start the task to get the full season full_season = tmdb_tv.get_season_info_by_number(tmdb_series_id, season_number) # wait for the task to finish or fail while full_season.status in states.UNREADY_STATES: time.sleep(0.1) # if the task succeded if full_season.status == states.SUCCESS: full_season = full_season.result if full_season: # set a dict of update values update_values = { 'air_date': full_season['air_date'], 'name': full_season['name'], 'episode_count': len(full_season['episodes']) } # use update or create to store or update the season updated_season = models.Season.objects.update_or_create( number=season_number, tmdb_id=full_season['id'], series=new_series, defaults=update_values) # get the updated_season from the tupel new_season = updated_season[0] # check if a poster is already present if not new_season.poster_large: posters = _get_posters(poster_path=full_season['poster_path']) # a poster is already present - set posters to False else: posters = False # check if posters equals to True # and if the posters are in the dict if (posters and ('poster_large' in posters) and ('poster_small' in posters)): # create a temporary poster in the cache temp_poster = BytesIO() # save the large poster in the temporary poster posters['poster_large'][1].save(temp_poster, 'JPEG') # set the offset to the beginning of the temp poster temp_poster.seek(0) # save the poster in the season with the hash as filename new_season.poster_large.save( posters['poster_large'][0] + '.jpg', ContentFile(temp_poster.read()), save=False) # close the temp poster temp_poster.close() # repeat the process with the small poster temp_poster = BytesIO() posters['poster_small'][1].save(temp_poster, 'JPEG') temp_poster.seek(0) new_season.poster_small.save( posters['poster_small'][0] + '.jpg', ContentFile(temp_poster.read()), save=False) temp_poster.close() # save the whole season new_season.save() # convert and store every episode for episode in full_season['episodes']: # values that are updated or filled update_episode_values = { 'name': episode['name'], 'air_date': episode['air_date'], 'overview': episode['overview'], } # update or create an episode models.Episode.objects.update_or_create( tmdb_id=episode['id'], number=episode['episode_number'], series=new_series, season=new_season, defaults=update_episode_values) return True return False