work on the simpsons generator using the DB
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6m57s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6m57s
This commit is contained in:
@@ -3,13 +3,38 @@ from django.http import HttpResponse
|
||||
from django.shortcuts import render
|
||||
from django.shortcuts import redirect
|
||||
|
||||
from django.core.files import File
|
||||
from django.core.files import File
|
||||
|
||||
#from django.template.loader import render_to_string
|
||||
import time
|
||||
import psycopg2
|
||||
import random
|
||||
import mimetypes
|
||||
|
||||
def DB_generation(host_in,port_in,user_in,password_in,db_in):
|
||||
#db = pymysql.connect(host=host_in, port=port_in, user=user_in, passwd=password_in,db=db_in)
|
||||
db = psycopg2.connect(host=host_in, port=port_in, user=user_in, password=password_in,database=db_in)
|
||||
return db
|
||||
|
||||
def Launch_sql_query(sql,db):
|
||||
#sql query with 1 object returned
|
||||
cursor = db.cursor()
|
||||
|
||||
try:
|
||||
# Execute the SQL command
|
||||
cursor.execute(sql)
|
||||
# Fetch all the rows in a list of lists.
|
||||
results = cursor.fetchall()
|
||||
for row in results:
|
||||
return row[0]
|
||||
|
||||
except:
|
||||
print ("Error: unable to fetch data from the query:")
|
||||
print (sql)
|
||||
print("---")
|
||||
cursor.close()
|
||||
|
||||
|
||||
def hello(request):
|
||||
return HttpResponse('<h1>Hello Simpsons!</h1>')
|
||||
#<h1>href="{{ STATIC_URL }}/files/somefile</h1>)
|
||||
@@ -25,8 +50,8 @@ def download_file(request):
|
||||
|
||||
# response = HttpResponse(fl, content_type=mime_type)
|
||||
# response['Content-Disposition'] = "attachment; filename=%s" % filename
|
||||
some_file = open('/data/kubernetes/Backup/nextcloud/notes/2024-07-28/Course.md', "r")
|
||||
django_file = File(some_file)
|
||||
some_file = open('/data/kubernetes/Backup/nextcloud/notes/2024-07-28/Course.md', "r")
|
||||
django_file = File(some_file)
|
||||
return HttpResponse("<h1>Hello download!</h1><h1><a href='{{ MEDIA_URL }}{{ django_file.relative_path }}'>{{ django_file.name }}</a></h1><h1>bye download!</h1>")
|
||||
# return response
|
||||
|
||||
@@ -42,27 +67,42 @@ def download_mo(request,file):
|
||||
#return HttpResponse('<h1>Hello !</h1>')
|
||||
|
||||
def simpsons(request):
|
||||
# simpsons_url = "https://www1.kisscartoon.online/episode/the-simpsons-season-";
|
||||
simpsons_url = "https://kisscartoon.top/episode/the-simpsons-season-";
|
||||
number_of_seasons = 35
|
||||
seasons_maximum_number = [13,22,24,22,22,25,25,25,25,23,22,21,22,22,22,21,22,22,20,21,23,22,22,22,22,22,22,22,21,23,22,22,22,22,18]
|
||||
generated_db = DB_generation("192.168.88.50",32345, "django_username", "Suffrage-Moneybags-Illusion7","django")
|
||||
cursor = generated_db.cursor()
|
||||
|
||||
#print(simpsons_url)
|
||||
#get max number of season
|
||||
maximum_number_of_seasons = Launch_sql_query("SELECT COUNT(DISTINCT season) as result FROM episodes;",generated_db)
|
||||
|
||||
random_seasons = random.randint(1, number_of_seasons)
|
||||
print("random seasons: "+ str(random_seasons))
|
||||
boolean_restart = True;
|
||||
while (boolean_restart):
|
||||
|
||||
#random_seasons-1 because list start at index 0
|
||||
print("maximum of episode for this season: "+str(seasons_maximum_number[random_seasons-1]))
|
||||
|
||||
random_episode = random.randint(1, seasons_maximum_number[random_seasons-1])
|
||||
|
||||
print("random episode: "+ str(random_episode))
|
||||
random_seasons = random.randint(1, maximum_number_of_seasons)
|
||||
print("random season: " + str(random_seasons))
|
||||
|
||||
|
||||
final_url = simpsons_url+str(random_seasons)+ "-episode-"+str(random_episode)#
|
||||
print(final_url)
|
||||
#get max number of epiosde for this season
|
||||
maximum_number_of_episodes_query = "SELECT COUNT(url) as result FROM episodes WHERE season = " + str(random_seasons) + ";"
|
||||
|
||||
return HttpResponse(str(final_url))
|
||||
# time.sleep(10)
|
||||
# return redirect(str(final_url))
|
||||
maximum_number_of_episodes = Launch_sql_query(maximum_number_of_episodes_query,generated_db)
|
||||
|
||||
random_episode = random.randint(1, maximum_number_of_episodes)
|
||||
print("random episode: " + str(random_episode))
|
||||
|
||||
#check if the episode was already seen
|
||||
check_if_already_seen_query = "select seen from episodes where season="+ str(random_seasons) +" and episode = "+ str(random_episode) +";"
|
||||
if not Launch_sql_query(check_if_already_seen_query, generated_db):
|
||||
boolean_restart = False
|
||||
else:
|
||||
print("episode "+str(random_episode)+" from season "+str(random_seasons)+" already seen")
|
||||
|
||||
url_from_episode_season_query = "select url from episodes where season="+ str(random_seasons) +" and episode = "+ str(random_episode) +";"
|
||||
generated_url = Launch_sql_query(url_from_episode_season_query,generated_db)
|
||||
|
||||
#mark as seen
|
||||
url_from_episode_season_query = "update episodes set seen = True where season=" + str(random_seasons) + " and episode = " + str(random_episode) + ";"
|
||||
Launch_sql_query(url_from_episode_season_query, generated_db)
|
||||
|
||||
generated_db.commit()
|
||||
generated_db.close()
|
||||
#return HttpResponse(str(generated_url))
|
||||
return redirect(str(generated_url))
|
||||
|
||||
Reference in New Issue
Block a user