knowyourmeme.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python
  2. import requests
  3. import re
  4. headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20121201 icecat/17.0.1'}
  5. query = ""
  6. try:
  7. query = sys.argv[1]
  8. print(query)
  9. except:
  10. while not query:
  11. query = input("Searching for: ")
  12. search = f"https://knowyourmeme.com/search?q={query}"
  13. search_result = requests.get(search, headers=headers)
  14. search_data = str(search_result.content)
  15. if "No results" in search_data:
  16. print("Nothing found :(")
  17. quit()
  18. links = re.findall('<a href="/memes/.+?">',search_data)
  19. for i, link in enumerate(links[15:]):
  20. url = "https://knowyourmeme.com" + link.replace("<a href=\"", "").replace("\">", "").replace("\" rel=\"nofollow","")
  21. if "trending" in url:
  22. break
  23. print(i, url)
  24. while True:
  25. try:
  26. c = int(input(f"Which meme from 0-{i}: "))
  27. except ValueError:
  28. continue
  29. else:
  30. break
  31. selected = "https://knowyourmeme.com"+links[c+15].replace("<a href=\"", "").replace("\">", "").replace("\" rel=\"nofollow","")
  32. result = requests.get(selected, headers=headers)
  33. data = str(result.content)
  34. paragraph = re.findall('<p>(.+?)</p>',data)
  35. cleaner = re.compile('<.*?>')
  36. about = re.sub(cleaner, '', paragraph[2])
  37. print("\nAbout: " + about.replace("\\'","'"))
  38. origin = re.sub(cleaner, '', paragraph[3])
  39. print("\nOrigin: " + origin.replace("\\'","'"))
  40. spread = re.sub(cleaner, '', paragraph[4])