from django.db.models import Count from django.core.exceptions import MultipleObjectsReturned from celery.worker.state import active_requests from requests.compat import str from django.utils import timezone from apps.betaserie.utils import betaserieUtils from .models import simpsons_data_object,urlData betaserie = betaserieUtils() simpsons_id = 12 def mark_episode_watched(season,episode,action,verbose=False): #mark episode as seen in DB simpsons_data_object.filter(season=season, episode=episode).values('seen').update(seen=True,modified_at=timezone.now()) if verbose : print("season_number: "+str(season)) if verbose : print("episode_number: "+str(episode)) #mark episode as seen in betaserie episode_number = "S"+str(season)+"E"+str(episode) episode_id = betaserie.identify_episode(simpsons_id,episode_number) if action == "watched": betaserie.mark_episodes_watched(episode_id) elif action == "unwatched": betaserie.unmark_episodes_watched(episode_id) else: print("Parameter action not in range") def unwatch_all_episodes(): maximum_number_of_seasons = simpsons_data_object.aggregate(unique_season=Count('season', distinct=True))['unique_season'] print("maximum_number_of_seasons: "+str(maximum_number_of_seasons)) for season in range(1,maximum_number_of_seasons+1): maximum_number_of_episodes = simpsons_data_object.filter(season=season).count() print("maximum_number_of_episodes for "+str(season)+": "+str(maximum_number_of_episodes)) for episode in range(1,maximum_number_of_episodes+1): print("S"+str(season)+"E"+str(episode)) mark_episode_watched(season,episode,"unwatched") def generate_new_urls_for_episode(verbose=False): # kisscartoons didn't work anymore so moving to another website with the format: # URL.com/simpsons/season=XX&episode=XX # example # https://www.1shows.org/tv/456-the-simpsons?streaming=true&season=33&episode=18 simpsons_series_information = betaserie.series_information(simpsons_id) seasons_information = simpsons_series_information[0] #for the simpsons, there is a season 0 which increase the count of seasons by 1 # we need to remove this entry , # so we cannot trust the maximum_number_season coming from simpsons_series_information season_0_to_remove = seasons_information[0] if verbose : print(season_0_to_remove) seasons_information.remove(season_0_to_remove) maximum_number_season = len(seasons_information) for season in range(1,maximum_number_season+1): # the list doesn't start with season 0 anymore so we cannot use the season number as index # we need to add -1 number_episodes_in_season = int(seasons_information[season-1]['episodes']) for episode in range(1,number_episodes_in_season+1): if verbose : print("Season: "+str(season)) if verbose : print("episode: "+str(episode)) new_url_object = urlData.objects.all().order_by('-addedDate').first() new_url = str(new_url_object.prefix)+str(season)+str(new_url_object.infix)+str(episode)+str(new_url_object.suffix) if verbose : print(new_url) try: obj,created = simpsons_data_object.get_or_create( season=season, episode=episode, defaults={"url":new_url,"seen":"False"} ) obj.url=new_url obj.save() except MultipleObjectsReturned: print("duplicated object for season: "+str(episode)+" episode: "+str(episode)+", ignoring") return 0