twitchemotesdl.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. import requests
  3. import re
  4. import sys
  5. import mimetypes
  6. import argparse
  7. def get_arguments():
  8. parser = argparse.ArgumentParser(description='IMDB trailer downloader')
  9. parser.add_argument('-c', '--channel', type=str, metavar='CHANNEL', help='Twitch channel with emotes')
  10. parser.add_argument('-g', '--general', action="store_true", default=False, help='Get general emotes')
  11. args = parser.parse_args()
  12. return args
  13. def check(r):
  14. if r.status_code not in [200, 302]:
  15. sys.exit("Could not connect")
  16. def scrape(r, imgRegex, titleRegex, titleRegex2='<br \/>(.*?)[\s]<\/center>'):
  17. findimg = re.findall(imgRegex, r)[1:]
  18. findtitle = re.findall(titleRegex, r)
  19. findtitle2 = re.findall(titleRegex2, r)
  20. findtitle = findtitle + findtitle2
  21. for i, result in enumerate(findimg):
  22. result = result.replace("2.0", "3.0").replace("1.0", "3.0")
  23. response = requests.get(result)
  24. try:
  25. ext = mimetypes.guess_all_extensions(response.headers["content-type"], strict=False)[0]
  26. except:
  27. ext = ".png"
  28. filename = findtitle[i].replace(":", "_").replace("/", "_").replace("\\", "_").replace("|", "_")+ext
  29. print(f"Downloading {filename}")
  30. with open(filename, 'wb') as f:
  31. f.write(response.content)
  32. def main():
  33. args = get_arguments()
  34. if args.channel:
  35. headers = {
  36. "Host": "twitchemotes.com",
  37. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/ 537. 36",
  38. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
  39. "Accept-Language": "en-US,en;q=0.5",
  40. "Accept-Encoding": "gzip, deflate, br",
  41. "Referer": "https://twitchemotes.com/channels/75508096",
  42. "Origin": "https://twitchemotes.com",
  43. "Connection": "keep-alive"
  44. }
  45. location = requests.post("https://twitchemotes.com/search/channel", allow_redirects=False, headers=headers, data={"query": args.channel, "source": "nav-bar"})
  46. check(location)
  47. location = location.headers["Location"]
  48. data = requests.get(f"https://twitchemotes.com{location}")
  49. check(data)
  50. scrape(data.text, '<img src="(.+?)"', '<br \/>(.*?)<\/center>')
  51. elif args.general:
  52. data = requests.get("https://twitchemotes.com")
  53. check(data)
  54. scrape(data.text, '<img src="(.+?)"', '</a>(.+?)</center>', "")
  55. if __name__ == '__main__':
  56. main()