Files
Django_kube/django_code/apps/random_simpsons/utils.py
T
pierre geeraert f1ffa5c949
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
rewriting url and adding new one if they were not created
2026-07-04 13:11:06 +01:00

85 lines
3.4 KiB
Python

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 apps.betaserie.utils import betaserieUtils
from .models import simpsons_data_object
betaserie = betaserieUtils()
simpsons_id = 12
def mark_episode_watched(season,episode,action):
print("season_number: "+str(season))
print("episode_number: "+str(episode))
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 = "https://www.1shows.org/tv/456-the-simpsons?streaming=true&season="+str(season)+"&episode="+str(episode)
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