diff --git a/Lesson_7/Task 1/.gitignore b/Lesson_7/Task 1/.gitignore new file mode 100644 index 0000000..7156e40 --- /dev/null +++ b/Lesson_7/Task 1/.gitignore @@ -0,0 +1,2 @@ +download +upload diff --git a/Lesson_7/Task 1/templates/index.html b/Lesson_7/Task 1/templates/index.html new file mode 100644 index 0000000..4d0a701 --- /dev/null +++ b/Lesson_7/Task 1/templates/index.html @@ -0,0 +1,16 @@ + + + + Image download + + +
+
+
+ +
+
+ {{response|safe}} +
+ + diff --git a/Lesson_7/Task 1/webserver.py b/Lesson_7/Task 1/webserver.py new file mode 100644 index 0000000..10e2c86 --- /dev/null +++ b/Lesson_7/Task 1/webserver.py @@ -0,0 +1,55 @@ +from bs4 import BeautifulSoup +from flask import Flask, render_template, request, send_from_directory +from zipfile import ZipFile +import os, requests, threading, uuid, urllib.request + +app = Flask("Image download") + +if not os.path.exists("download"): + os.makedirs("download") + +if not os.path.exists("upload"): + os.makedirs("upload") + +def downloadImage(url, image, directory): + try: + image_link = image["src"] + except: + pass + + try: + filename = image_link.split("/")[-1] + urllib.request.urlretrieve(url + image_link, directory + "/" + filename) + except: + pass + +def downloadImages(url, uuid): + directory = "download/" + uuid + filename = "upload/" + uuid + ".zip" + os.mkdir(directory) + r = requests.get(url) + soup = BeautifulSoup(r.text, 'html.parser') + images = soup.findAll('img') + for image in images: + downloadImage(url, image, directory) + + with ZipFile(filename, 'w') as zip: + for path, directories, files in os.walk(directory): + for file in files: + file_name = os.path.join(path, file) + zip.write(file_name) + + return "Image archive" + +@app.route('/') +def index(): + result = None + if 'url' in request.args: + response = downloadImages(request.args.get('url'), str(uuid.uuid4())) + return render_template("index.html", response=response) + +@app.route('/upload/') +def download(filename): + return send_from_directory("upload", filename) + +app.run(host='0.0.0.0', port=8081)