您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

download.py 1.8 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # coding: utf-8
  2. import cgi
  3. import cgitb; cgitb.enable()
  4. import glob
  5. import random
  6. import string
  7. import requests
  8. form = cgi.FieldStorage()
  9. def get_random_string(length):
  10. letters = string.ascii_lowercase
  11. result_str = ''.join(random.choice(letters) for i in range(length))
  12. return result_str
  13. header = """
  14. <!DOCTYPE html>
  15. <head>
  16. <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  17. <title>Download the image</title>
  18. </head>
  19. <body>
  20. """
  21. link = form.getvalue("link")
  22. choice = form.getvalue("choice")
  23. print(header)
  24. if link is not None :
  25. print("""<h2 class="w3-center w3-teal"> Add an image </h2>""")
  26. print("<title> Adding the image </title>")
  27. path = str(choice) + '/' + get_random_string(6)
  28. print("<h4> Generating a unique URL... </h4>")
  29. with open("links.txt", "a") as myfile:
  30. myfile.write(choice + " - " + path + " - " + link + "\n")
  31. if True:
  32. print("<h4>Valid URL, ready for download! </h4> ")
  33. r = requests.get(link, allow_redirects=True)
  34. print("<h4> " + r.headers.get('content-type') + "</h4>")
  35. header = r.headers.get('content-type')
  36. header = header.split("/")
  37. print("<h4> File identification : " + header[0] + "</h4>")
  38. if header[0] == "image":
  39. print("<h4> Downloading... </h4>")
  40. open(path, 'wb').write(r.content)
  41. print(""" <div class="w3-panel w3-green">
  42. <h2 class="w3-opacity">Done ! </h2>
  43. </div>""")
  44. print("<img class='w3-round' src='%s' >" % path)
  45. else :
  46. print(""" <div class="w3-panel w3-red">
  47. <h2 class="w3-opacity">Echec ! </h2>
  48. </div>""")
  49. else :
  50. print("Echec")