tweetifier.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. import re
  3. import irc
  4. from tweety import Twitter
  5. from urllib.parse import urlparse
  6. ## IRC Config
  7. server = "127.0.0.1"
  8. port = 11069
  9. channels = ["#test", "#test1"]
  10. botnick = "tweetifier"
  11. ircc = irc.IRC()
  12. ircc.connect(server, port, channels, botnick)
  13. while True:
  14. text = ircc.get_response().strip()
  15. if not len(text) > 0:
  16. continue
  17. print(text)
  18. text_list = text.split(' ')
  19. if text_list[1] == "PRIVMSG":
  20. channel = text_list[2]
  21. msg = ' '.join(text_list[3:])
  22. url = re.findall(r'(https?://[^\s]+)', msg)
  23. for i in url:
  24. parsed_url = urlparse(i)
  25. if str(parsed_url.path).endswith("/"):
  26. tweetId = str(parsed_url.path)[:-1].split("/")[-1]
  27. else:
  28. tweetId = str(parsed_url.path).split("/")[-1]
  29. print(f"tweet id: {tweetId}")
  30. if not (parsed_url.netloc.lower() in ['twitter.com','t.co', 'x.com'] and parsed_url.scheme == 'https'):
  31. continue
  32. app = Twitter("session")
  33. try:
  34. tweet_text = app.tweet_detail(tweetId)
  35. except:
  36. print("Error: The Identifier provided of the tweet is either invalid or the tweet is private")
  37. continue
  38. author_name = tweet_text.author.name
  39. screen_name = tweet_text.author.screen_name
  40. tt = tweet_text.text.split('\n')
  41. tweet_msg = []
  42. # remove empty lines from tweet body
  43. for line in tt:
  44. if not line.strip():
  45. continue
  46. tweet_msg.append(line)
  47. tweetify = str(' '.join(tweet_msg))
  48. if tweetify.startswith("@"):
  49. tweetify = f"Replying to {tweetify}"
  50. print(tweetify)
  51. ircc.send(channel, f"{author_name}(@{screen_name}): {tweetify}")