tw.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import tweepy
  2. import logging
  3. from config import create_api
  4. import json
  5. logging.basicConfig(level=logging.INFO)
  6. logger = logging.getLogger()
  7. class FavRetweetListener(tweepy.StreamListener):
  8. def __init__(self, api):
  9. self.api = api
  10. self.me = api.me()
  11. def on_status(self, tweet):
  12. logger.info(f"Processing tweet id {tweet.id}")
  13. if tweet.in_reply_to_status_id is not None or \
  14. tweet.user.id == self.me.id:
  15. # This tweet is a reply or I'm its author so, ignore it
  16. return
  17. if not tweet.favorited:
  18. # Mark it as Liked, since we have not done it yet
  19. try:
  20. tweet.favorite()
  21. except Exception as e:
  22. logger.error("Error on fav", exc_info=True)
  23. if not tweet.retweeted:
  24. # Retweet, since we have not retweeted it yet
  25. try:
  26. tweet.retweet()
  27. except Exception as e:
  28. logger.error("Error on fav and retweet", exc_info=True)
  29. def on_error(self, status):
  30. logger.error(status)
  31. def main(keywords):
  32. api = create_api()
  33. tweets_listener = FavRetweetListener(api)
  34. stream = tweepy.Stream(api.auth, tweets_listener)
  35. stream.filter(track=keywords, languages=["en"])
  36. if __name__ == "__main__":
  37. main(["Python", "Tweepy"])