|
- # coding: utf-8
-
- import cgi
- import cgitb; cgitb.enable()
- import glob
- import random
- import string
- import requests
- form = cgi.FieldStorage()
-
- def get_random_string(length):
- letters = string.ascii_lowercase
- result_str = ''.join(random.choice(letters) for i in range(length))
- return result_str
-
- header = """
- <!DOCTYPE html>
- <head>
- <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
- <title>Download the image</title>
- </head>
- <body>
- """
-
- link = form.getvalue("link")
- choice = form.getvalue("choice")
- print(header)
-
- if link is not None :
- print("""<h2 class="w3-center w3-teal"> Add an image </h2>""")
- print("<title> Adding the image </title>")
-
- path = str(choice) + '/' + get_random_string(6)
- print("<h4> Generating a unique URL... </h4>")
- with open("links.txt", "a") as myfile:
- myfile.write(choice + " - " + path + " - " + link + "\n")
-
- if True:
- print("<h4>Valid URL, ready for download! </h4> ")
- r = requests.get(link, allow_redirects=True)
- print("<h4> " + r.headers.get('content-type') + "</h4>")
- header = r.headers.get('content-type')
- header = header.split("/")
- print("<h4> File identification : " + header[0] + "</h4>")
- if header[0] == "image":
- print("<h4> Downloading... </h4>")
- open(path, 'wb').write(r.content)
- print(""" <div class="w3-panel w3-green">
- <h2 class="w3-opacity">Done ! </h2>
- </div>""")
- print("<img class='w3-round' src='%s' >" % path)
- else :
- print(""" <div class="w3-panel w3-red">
- <h2 class="w3-opacity">Echec ! </h2>
- </div>""")
-
-
- else :
- print("Echec")
|