-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscan_and_retweet.py
101 lines (79 loc) · 2.86 KB
/
scan_and_retweet.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
99
100
101
import json
import time
import os
import sys
import requests
import twitter
# Number of home timeline tweets to fetch in each batch of pagination
BATCH_SIZE = 200
def fetch_rules():
return json.loads(
# Read the local config file and convert to lower case
open("config.json")
.read()
.lower()
)
def tweet_matches_rules(screen_name, full_text, rules):
# Extract information about the tweet
screen_name = screen_name.lower()
full_text = full_text.lower()
if screen_name in rules:
# For this User, what list of hashtags are we looking at
hashtags = rules[screen_name]
# Retweet everything from an account if we have configured it
# with a wildcard, represented as an empty list
if [] == hashtags:
return True
# If any of the above hashtags is present in the text then it matches
for hashtag in hashtags:
if ("#" + hashtag) in full_text:
return True
# If we get here then no hashtags matched that tweet for that usert
return False
def get_tweets(api, num_pages_to_fetch=1):
# Fetch BATCH_SIZE recent tweets from users we follow
max_id = None
tweets = []
for i in range(num_pages_to_fetch):
print("max_id: %r" % max_id)
tweets.extend(api.GetHomeTimeline(count=BATCH_SIZE, max_id=max_id))
max_id = min([tweet.id for tweet in tweets]) - 1
print("FETCHED %d tweets" % len(tweets))
print()
return tweets
def scan_and_retweet(tweets):
# Fetch our rules
rules = fetch_rules()
print("LOADED %d rules" % len(rules))
# For every tweet, see if it matches a rule
for tweet in reversed(tweets):
print()
print(tweet.AsJsonString())
print('CONSIDER @%s "%r"' % (tweet.user.screen_name, tweet.full_text))
# Have we retweeted this already?
if tweet.retweeted:
print("... SKIPPING, we have retweeted already")
continue
# Does it match a rule?
elif tweet_matches_rules(tweet.user.screen_name, tweet.full_text, rules):
print("... TWEETING Matched a rule! Gonna retweet it")
# Retweet it!
print(api.PostRetweet(tweet.id))
# Sleep before next possible retweet
time.sleep(2.5)
else:
print("... IGNORING")
if __name__ == "__main__":
api = twitter.Api(
tweet_mode="extended",
consumer_key=os.environ["CONSUMER_KEY"],
consumer_secret=os.environ["CONSUMER_SECRET"],
access_token_key=os.environ["ACCESS_TOKEN_KEY"],
access_token_secret=os.environ["ACCESS_TOKEN_SECRET"],
)
num_pages_to_fetch = 1
if "--backfill" in sys.argv:
print("Running backfill (last 800 tweets)")
num_pages_to_fetch = 4
tweets = get_tweets(api, num_pages_to_fetch)
scan_and_retweet(tweets)