Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class twitter_streamer():
- """
- Class for streaming and processing live tweets.
- """
- def stream_tweets(self, fetched_tweets_filename, query_list):
- # Handles Authentication and connection to Twitter and their Streaming API
- listener = twitter_listener(fetched_tweets_filename)
- stream = Stream(auth, listener)
- stream.filter(track=query_list) # Filter tweets based on keywords
- class twitter_listener(StreamListener):
- """
- Stream listener to check for and print tweets
- """
- def __init__(self, fetched_tweets_filename):
- self.fetched_tweets_filename = fetched_tweets_filename
- def on_data(self, data):
- try:
- tweet = json.loads(data)
- with open(self.fetched_tweets_filename, "a") as outfile:
- json.dump(tweet, outfile, indent=4)
- print(tweet["text"])
- # manipulate_tweet(tweet)
- move, user_mentions = manipulate_tweet(tweet)
- game_id = lichess.get_game_id(lichess_token)
- print(game_id)
- make_move(user_mentions, game_id, move)
- return True
- except BaseException as e:
- print(f"Error on_data {str(e)}")
- return True
- def on_error(self, status):
- print(status)
- if status == 420:
- return False
- def manipulate_tweet(tweet):
- str_tweet = tweet["text"]
- try:
- split_tweet = str_tweet.split(" ")
- move = str(split_tweet[-1])
- print(f"Move is {move}")
- except IndexError:
- print("Move not found!")
- return False
- try:
- user_mentions = tweet["entities"]["user_mentions"][0]["screen_name"]
- except IndexError:
- print("No user mentions")
- return False
- return move, user_mentions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement