Twitter Bot


SUBMITTED BY: muroseu

DATE: June 26, 2016, 5:15 p.m.

FORMAT: Python 3

SIZE: 2.3 kB

HITS: 52940

  1. #!/usr/local/bin/python3.3
  2. import tweepy
  3. from tweepy.auth import OAuthHandler
  4. from tweepy.streaming import StreamListener, Stream
  5. ckey = ''
  6. csecret = ''
  7. atoken = ''
  8. asecret = ''
  9. auths = OAuthHandler(ckey, csecret)
  10. auths.set_access_token(atoken, asecret)
  11. api = tweepy.API(auths)
  12. class listener(StreamListener):
  13. def on_data(self, raw_data):
  14. try:
  15. tweet_text = raw_data.lower().split('"text":"')[1].split('","source":"')[0].replace(",", "")
  16. screen_name = raw_data.lower().split('"screen_name":"')[1].split('","location"')[0].replace(",", "")
  17. tweet_cid = raw_data.split('"id":')[1].split('"id_str":')[0].replace(",", "")
  18. accs = ['twitter' , 'twittersupport'] # banned account screen name goes in here
  19. words = ['hate' , 'derp'] # banned words goes in here
  20. if not any(acc in screen_name.lower() for acc in accs):
  21. if not any(word in tweet_text.lower() for word in words):
  22. retweet(tweet_cid)
  23. return True
  24. except Exception as e:
  25. print(str(e)) # prints the error msg, if u dont want it comment it out
  26. pass
  27. def on_error(self, status_code):
  28. try:
  29. print( "error" + status_code)
  30. except Exception as e:
  31. print(str(e))
  32. pass
  33. def retweet(tweet_cid):
  34. try:
  35. api.retweet(tweet_cid)
  36. except Exception as e:
  37. print(str(e))
  38. pass
  39. def fav(tweet_cid):
  40. try:
  41. api.create_favorite(tweet_cid)
  42. except Exception as e:
  43. print(str(e))
  44. pass
  45. def unfav(tweet_cid):
  46. try:
  47. api.destroy_favorite(tweet_cid)
  48. except Exception as e:
  49. print(str(e))
  50. pass
  51. def tweet(myinput):
  52. try:
  53. api.update_status(status=myinput)
  54. except Exception as e:
  55. print(str(e))
  56. pass
  57. track_words = ["#Finland","Finland", "", "", "", "", "", "" ""]
  58. follow_acc = [''] # all username converted to user ids
  59. print("Running...")
  60. try:
  61. twt = Stream(auths, listener())
  62. twt.filter(track= track_words) # or follow = follow_acc
  63. except Exception as e:
  64. print(str(e))
  65. pass

comments powered by Disqus