-
Notifications
You must be signed in to change notification settings - Fork 2
/
stream.py
98 lines (80 loc) · 2.95 KB
/
stream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import tweepy
import time
import logging
from connection import connect_account
logging.basicConfig(level=logging.INFO, filename='app.log',
filemode='w', format='%(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger()
class Listener(tweepy.StreamListener):
"""
This is a class which overrides tweepy.StreamListener and adds own code.
"""
usernames = []
def __init__(self, api):
self.api = api
self.me = api.me()
def on_status(self, tweet):
"""
Attributes:
self (object): Listener object.
tweet (object): Status object.
"""
print("On status block")
logger.info(f"Processing tweet id {tweet.id}")
self.usernames.append(tweet.user.screen_name)
if not tweet.favorited:
# Like if not done yet
try:
tweet.favorite()
except Exception as e:
logger.info(f"Already Liked tweet {tweet.id}")
logger.error("Error on liking", exc_info=True)
logger.info(f"Liked tweet {tweet.id}")
user = self.api.get_user(tweet.user.screen_name)
follow(self.api, user)
time.sleep(2.4)
def on_error(self, status_code):
"""
Attributes:
self (object): Listener object.
status_code (int): Error code.
"""
if status_code == 420:
# returning False in on_error disconnects the stream
return False
def follow_usernames(api, usernames):
"""
Attributes:
api (object): tweepy.API() object which is the bot's account authenticated using the given keys.
usernames (list): List of usernames input which have to be followed.
"""
for name in usernames:
try:
user = api.get_user(name)
follow(api, user)
except Exception as e:
logger.error("User doesnot exist", exc_info=True)
def follow(api, user):
"""
Attributes:
api (object): tweepy.API() object which is the bot's account authenticated using the given keys.
user (object): User object.
"""
print("Follow block")
if not user.following:
try:
api.create_friendship(user.id)
except Exception as e:
logger.info(f"Already following {user.name}")
logger.error("Error on following", exc_info=True)
logger.info(f"Followed {user.name}")
def listen(api, keywords):
"""
Attributes:
api (object): tweepy.API() object which is the bot's account authenticated using the given keys.
keywords (list): List of hashtags that have to be searched for
"""
print("Listen block")
tweets_listener = Listener(api)
stream = tweepy.Stream(api.auth, tweets_listener)
stream.filter(track=keywords, languages=["en"])