Files
Django_kube/django_code/stash_management/apps.py
T
2025-12-02 17:03:30 +00:00

108 lines
3.9 KiB
Python

from django.apps import AppConfig
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect
#from jinja2 import Environment, FileSystemLoader
from django.core.files import File
from kubernetes import client, config
from celery import shared_task
import time
import random
import mimetypes
import ruamel.yaml
import yaml
from urllib import request as urlrequest
proxy_host = "gluetun-svc.gluetun.svc:8888"
proxy_url = 'http://gluetun-svc.gluetun.svc:8888'
proxy = urlrequest.ProxyHandler({
'http': proxy_url,
'https': proxy_url # Add the handler for HTTPS
})
def hello(request):
return HttpResponse('<h1>Hello stash!</h1>')
def form(request):
return render(request, 'stash_form.html',{})
def fapello(request):
return render(request, 'fapello_form.html',{})
def fapello_form(request):
stash_folder = "/tmp/stash"
name = request.GET.get('name')
url = request.GET.get('url')
url_suffix = request.GET.get('url_suffix')
maximum_increment = int(request.GET.get('maximum_increment'))
minimum_increment = request.GET.get('minimum_increment')
increment_and_download.delay(minimum_increment=minimum_increment,maximum_increment=maximum_increment,url=url,url_suffix=url_suffix,stash_folder=stash_folder,name=name)
text_generated = "successfully sent to celery"
return HttpResponse(text_generated, content_type="text/plain")
@shared_task()
def increment_and_download(minimum_increment,maximum_increment,url,url_suffix,stash_folder,name):
if minimum_increment == None:
minimum_increment = 1
text_generated = ""
minimum_increment = int(minimum_increment)
maximum_increment = int(maximum_increment)
for increment in range(minimum_increment,maximum_increment):
if len(str(increment)) == 3:
increment_generated ='0' + str(increment)
elif len(str(increment)) == 2:
increment_generated ='00' + str(increment)
elif len(str(increment)) == 1:
increment_generated ='000' + str(increment)
opener = urlrequest.build_opener(proxy)
urlrequest.install_opener(opener)
urlrequest.urlretrieve(url+increment_generated+url_suffix, stash_folder+"/"+increment_generated+url_suffix)
text_generated = "\n downloading for "+name+" "+url+increment_generated+url_suffix
return text_generated
def handle_survey_form(request):
config.load_kube_config(config_file='/kubeconfig/config')
v1 = client.BatchV1Api()
name = request.GET.get('name')
url = request.GET.get('url')
external_galleries = str(request.GET.get('external_galleries'))
stash_folder = "/stash"
if external_galleries:
print("external_galleries is not null")
performer_external_galleries = stash_folder+"/"+name+"_external_galleries.txt"
with open(performer_external_galleries, "a") as f:
f.write(external_galleries)
skip_searching_gallery = True
else:
print("external_galleries is null")
skip_searching_gallery = False
with open('django_code/stash_management/templates/template.stash-pornpics-dowloader.job.yml', 'r') as file:
#get the manifest in a variable template_manifest from the manifest file
template_manifest = yaml.safe_load(file)
#edit the container name
template_manifest['metadata']['name'] ="stash-pornpics-dowloader-"+name
template_manifest['spec']['template']['spec']['containers'][0]['name'] ="stash-pornpics-dowloader-"+name
#edit the command based on the input
command=['bash', '/config/download.sh', '--http_proxy', 'http://gluetun-svc.gluetun.svc:8888', '--BASE_URL', url, '--pornstars', name]
template_manifest['spec']['template']['spec']['containers'][0]['command']=command
#execute based on the value
api_response = v1.create_namespaced_job(body=template_manifest,namespace='stash')
return HttpResponse(api_response)