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 os import time import random import mimetypes import ruamel.yaml import yaml from urllib import request as urlrequest proxy_url = 'http://gluetun-svc.gluetun.svc:8888' proxy = urlrequest.ProxyHandler({ 'http': proxy_url, 'https': proxy_url # Add the handler for HTTPS }) destination_folder = "/tmp/stash" def hello(request): return HttpResponse('

Hello stash!

') 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,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,name): if minimum_increment == None: minimum_increment = 1 text_generated = "" minimum_increment = int(minimum_increment) maximum_increment = int(maximum_increment) #check if the output directory exist and create if not if not os.path.isdir(destination_folder): os.makedirs(destination_folder) #check if the named directory exist and create if not destination_named_folder = os.path.join(destination_folder,name) if not os.path.isdir(destination_named_folder): os.makedirs(destination_named_folder) print("\n downloading for "+name+" "+url+"XXX"+url_suffix) for increment in range(minimum_increment,maximum_increment): print("increment : "+str(increment)+"/"+str(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) url_generated = url+increment_generated+url_suffix destination_filename = increment_generated+url_suffix destination_file = os.path.join(destination_named_folder,destination_filename) opener = urlrequest.build_opener(proxy) urlrequest.install_opener(opener) urlrequest.urlretrieve(url_generated, destination_file ) text_generated = "\n downloading for "+name+" "+url+increment_generated+url_suffix print("download completed for "+name+" of "+str(maximum_increment)+" images") 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)