-
Notifications
You must be signed in to change notification settings - Fork 1
/
dailyBot.py
executable file
·67 lines (60 loc) · 2.29 KB
/
dailyBot.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
#!/usr/bin/python3.8
import tweepy
import logging
from config import create_api
from random import seed
from random import randint
import time
seed(1)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
class FavRetweetListener(tweepy.StreamListener):
def __init__(self, api):
self.start_time = time.time()
self.limit = 5 * 60
self.api = api
self.me = api.me()
def on_status(self, tweet):
stream_time = time.time() - self.start_time
if stream_time < self.limit:
logger.info(f"Processing tweet id {tweet.id}")
time.sleep(randint(0, 10))
if tweet.in_reply_to_status_id is not None or \
tweet.user.id == self.me.id:
# This tweet is a reply or I'm its author so, ignore it
return
if not tweet.favorited:
# Mark it as Liked, since we have not done it yet
try:
tweet.favorite()
except Exception as e:
logger.error("Error on fav", exc_info=True)
if not tweet.retweeted:
# Retweet, since we have not retweeted it yet
try:
should_retweet = randint(0, 10)
if should_retweet < 4:
logger.info("Retwweting...")
tweet.retweet()
except Exception as e:
logger.error("Error retweet", exc_info=True)
if not tweet.user.following:
# Retweet, since we have not retweeted it yet
try:
if should_retweet < 1:
logger.info("Following...")
self.api.create_friendship(tweet.user.id)
except Exception as e:
logger.error("Error on follow", exc_info=True)
else:
logger.info(f"Closing stream, you have been streaming for: {stream_time}")
return False
def on_error(self, status):
logger.error(status)
def main(keywords):
api = create_api()
tweets_listener = FavRetweetListener(api)
stream = tweepy.Stream(api.auth, tweets_listener)
stream.filter(track=keywords, languages=["en"])
if __name__ == "__main__":
main(["Day #100DaysOfCode"])