Add Lesson 7 Task 1
This commit is contained in:
parent
ba3cdad458
commit
0437695d02
3 changed files with 73 additions and 0 deletions
2
Lesson_7/Task 1/.gitignore
vendored
Normal file
2
Lesson_7/Task 1/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
download
|
||||||
|
upload
|
16
Lesson_7/Task 1/templates/index.html
Normal file
16
Lesson_7/Task 1/templates/index.html
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<title>Image download</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form>
|
||||||
|
<label for="url">URL: </label><br>
|
||||||
|
<input type="url" id="url" name="url"><br>
|
||||||
|
<button id="submit" type="submit">Submit</button>
|
||||||
|
</form>
|
||||||
|
<div id="response">
|
||||||
|
{{response|safe}}
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
55
Lesson_7/Task 1/webserver.py
Normal file
55
Lesson_7/Task 1/webserver.py
Normal file
|
@ -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 "<a href=\"" + filename + "\">Image archive</a>"
|
||||||
|
|
||||||
|
@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/<path:filename>')
|
||||||
|
def download(filename):
|
||||||
|
return send_from_directory("upload", filename)
|
||||||
|
|
||||||
|
app.run(host='0.0.0.0', port=8081)
|
Reference in a new issue