You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.1KB

  1. import requests
  2. from bs4 import BeautifulSoup
  3. class imgur():
  4. """Class for all interactions with Imgur"""
  5. def __init__(self):
  6. pass
  7. def removed(self,url):
  8. page = requests.get(url)
  9. soup = BeautifulSoup(page.content, 'html.parser')
  10. if "removed.png" in soup.img["src"]:
  11. return True
  12. else:
  13. return False
  14. def get(self, url):
  15. if url.split(".")[-1] in ("png", "jpg", "jpeg", "gif", "gifv"):
  16. return url
  17. #elif url.split(".")[-1] == "gifv":
  18. # urlsplit = url.split(".")
  19. # urlsplit[-1] = "gif"
  20. # url = ".".join(urlsplit)
  21. # return url"""
  22. else:
  23. if self.removed(url):
  24. return False
  25. page = requests.get(url)
  26. soup = BeautifulSoup(page.content, 'html.parser')
  27. links = []
  28. for img in soup.find_all("img"):
  29. if "imgur" in img["src"]:
  30. if not img["src"] in links:
  31. links.append(img["src"])
  32. for video in soup.find_all("source"):
  33. if "imgur" in video["src"]:
  34. if not video["src"] in links:
  35. links.append(video["src"])
  36. if len(links) > 1:
  37. return url
  38. else:
  39. print(links)
  40. if not "http" in links[0]:
  41. links[0] = "https:" + links[0]
  42. return links[0]