· 6 years ago · Apr 06, 2020, 09:08 AM
1import os
2import tweepy
3import json
4class MyStreamListener(tweepy.StreamListener):
5 def on_connect(self):
6 print("on connect is executed here")
7
8 def on_status(self, status):
9 #FILENAME = "data.txt"
10 #tweet_id = status.id
11 #f = open(FILENAME, 'w')
12 #f.write(str(tweet_id))
13 parent_tweet_id = status.in_reply_to_status_id
14 parent_tweet = api.get_status(parent_tweet_id)
15 # video_link = (parent_tweet.extended_entities["media"][0]["video_info"]["variants"][3]["url"])
16 try:
17 api.create_favorite(status.id)
18 except tweepy.TweepError as e:
19 print(e)
20
21 print(parent_tweet)
22
23 def on_error(self, status_code):
24 if status_code == 420:
25 print("Received 420 response")
26 return False
27
28# Authentication
29consumer_key = os.getenv("CONSUMER_KEY")
30consumer_secret = os.getenv("CONSUMER_SECRET")
31key = os.getenv("key")
32secret = os.getenv("secret")
33
34auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
35auth.set_access_token(key,secret)
36
37# Create API object
38try:
39 api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
40except tweepy.TweepError as e:
41 print(e)
42tweets_listener = MyStreamListener(api)
43stream = tweepy.Stream(api.auth, tweets_listener)
44stream.filter(track=["@bulundindia1337"]