Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. class twitter_streamer():
  2.     """
  3.    Class for streaming and processing live tweets.
  4.    """
  5.  
  6.     def stream_tweets(self, fetched_tweets_filename, query_list):
  7.         # Handles Authentication and connection to Twitter and their Streaming API
  8.         listener = twitter_listener(fetched_tweets_filename)
  9.         stream = Stream(auth, listener)
  10.         stream.filter(track=query_list)  # Filter tweets based on keywords
  11.  
  12.  
  13. class twitter_listener(StreamListener):
  14.     """
  15.    Stream listener to check for and print tweets
  16.    """
  17.  
  18.     def __init__(self, fetched_tweets_filename):
  19.         self.fetched_tweets_filename = fetched_tweets_filename
  20.  
  21.     def on_data(self, data):
  22.         try:
  23.             tweet = json.loads(data)
  24.             with open(self.fetched_tweets_filename, "a") as outfile:
  25.                 json.dump(tweet, outfile, indent=4)
  26.             print(tweet["text"])
  27.             # manipulate_tweet(tweet)
  28.             move, user_mentions = manipulate_tweet(tweet)
  29.             game_id = lichess.get_game_id(lichess_token)
  30.             print(game_id)
  31.             make_move(user_mentions, game_id, move)
  32.             return True
  33.         except BaseException as e:
  34.             print(f"Error on_data {str(e)}")
  35.         return True
  36.  
  37.     def on_error(self, status):
  38.         print(status)
  39.         if status == 420:
  40.             return False
  41.  
  42.  
  43. def manipulate_tweet(tweet):
  44.     str_tweet = tweet["text"]
  45.     try:
  46.         split_tweet = str_tweet.split(" ")
  47.         move = str(split_tweet[-1])
  48.         print(f"Move is {move}")
  49.     except IndexError:
  50.         print("Move not found!")
  51.         return False
  52.     try:
  53.         user_mentions = tweet["entities"]["user_mentions"][0]["screen_name"]
  54.     except IndexError:
  55.         print("No user mentions")
  56.         return False
  57.     return move, user_mentions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement