tenor.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python
  2. import requests
  3. import re
  4. import tempfile
  5. import os
  6. import sys
  7. image_viewer = "palemoon"
  8. headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20121201 icecat/17.0.1'}
  9. try:
  10. query = ""
  11. try:
  12. query = sys.argv[1]
  13. except:
  14. while not query:
  15. query = input("Searching for: ")
  16. search = f"https://tenor.com/search/{query}-gifs"
  17. search_result = requests.get(search, headers=headers)
  18. search_data = str(search_result.content)
  19. links = re.findall('<img src=.+?>',search_data)
  20. if "No Results" in search_data:
  21. print("Nothing found :(")
  22. quit()
  23. for i, link in enumerate(links[34:]):
  24. url = re.findall(r'(https?://\S+)', link)
  25. url = url[0].replace("\"", "")
  26. print(i, url)
  27. if i == 10:
  28. break
  29. temp_dir = tempfile.TemporaryDirectory().name
  30. thumbnail_data = requests.get(url)
  31. with open(temp_dir, 'wb') as f:
  32. f.write(thumbnail_data.content)
  33. os.system(image_viewer + " " + temp_dir)
  34. except:
  35. search_result = requests.get(sys.argv[1], headers=headers)
  36. search_data = str(search_result.content)
  37. links = re.findall('<img src=.+?>',search_data)
  38. url = re.findall(r'(https?://\S+)', links[2])
  39. url = url[0].replace("\"", "")
  40. print(url)