diff --git a/django_code/stash_management/apps.py b/django_code/stash_management/apps.py
index fe9cb95..965bf66 100644
--- a/django_code/stash_management/apps.py
+++ b/django_code/stash_management/apps.py
@@ -33,7 +33,7 @@ proxies = {
"https" : proxy_url
}
-destination_folder = "/tmp/stash"
+destination_folder = "/tmp/stash/"
def hello(request):
return HttpResponse('
Hello stash!
')
@@ -42,6 +42,18 @@ def form(request):
def album(request):
return render(request, 'album_form.html',{})
+def check_and_create_destination_folder(name):
+ #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)
+
+ return destination_named_folder
+
def album_form(request):
name = request.GET.get('name')
url = request.GET.get('url')
@@ -64,6 +76,10 @@ def album_downloader(name,url,external_galleries):
page = requests.get(url,proxies=proxies)
webpage = html.fromstring(page.content)
+ #create the prerequisites folder
+ destination_named_folder = check_and_create_destination_folder(name=name)
+
+
#get all URLs from the code source (searching for href tag on the code)
#https://stackoverflow.com/questions/21455349/xpath-query-get-attribute-href-from-a-tag
@@ -82,16 +98,18 @@ def album_downloader(name,url,external_galleries):
webpage = html.fromstring(page.content)
images_list = webpage.xpath("//a[contains(@href,'cdni.pornpics.com/1280/')]/@href")
- images_downloader(name=name,images_url_list=images_list)
+ images_downloader(name=name,images_url_list=images_list,destination_named_folder=destination_named_folder)
print(images_list)
return 0
-def images_downloader(name,images_url_list):
+def images_downloader(name,images_url_list,destination_named_folder):
for image_url in images_url_list:
split = urlparse.urlsplit(image_url)
- filename = destination_folder + split.path.split("/")[-1]
-
- print("downloading: "+filename)
+ #filename = destination_folder + split.path.split("/")[-1]
+ filename = os.path.join(destination_named_folder,split.path.split("/")[-1])
+
+
+ print("downloading: "+filename+" -- "+image_url)
opener = urlrequest.build_opener(proxy)