lightextender.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os.path
  2. import urllib.request
  3. from random import choice
  4. from bs4 import BeautifulSoup
  5. from requests import get
  6. from os import system
  7. class lightextender:
  8. def generate(_symbols: str="qwertyuiopasdfghjklzxcvbnm1234567890", _len=6):
  9. """This function returns a random link index with a screenshot to lightshot"""
  10. _index = ""
  11. for i in range(0, _len):
  12. _index += choice(_symbols)
  13. return _index
  14. def request(_index: str):
  15. """This function gets a link to the exact picture"""
  16. opener = urllib.request.build_opener()
  17. opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
  18. urllib.request.install_opener(opener)
  19. html = urllib.request.urlopen("https://prnt.sc/"+_index).read()
  20. soup = BeautifulSoup(html, 'html.parser')
  21. try:
  22. picture_url = soup.find(id='screenshot-image')['src']
  23. except TypeError:
  24. return False
  25. return picture_url
  26. def bashdownload(_source_url: str):
  27. """This function loads the image.
  28. However, the download is done with
  29. commands to terminal. Wget must already
  30. be installed."""
  31. if os.path.exists("images") != True:
  32. system("mkdir images")
  33. if _source_url != False:
  34. return system("wget -P images/ -q "+ _source_url)
  35. def download(_source_url: str):
  36. """This function downloads the picture using the urlretrieve"""
  37. urllib.request.urlretrieve(_source_url, _source_url[32:])
  38. def download_content(_source_url: str):
  39. """This function downloads the picture using the requests.get"""
  40. if type(_source_url) not in [' ', bool]:
  41. _output_url = _source_url.replace("https://i.imgur.com/", "").replace("https://image.prntscr.com/image/", "")
  42. open(f"images/{_output_url}", "wb").write(get(_source_url).content)