Skip to content

Commit aba0de9

Browse files
committed
twitter: remove requirement of two internal file and support multiple searches, users
1 parent 9ce7c52 commit aba0de9

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

zulip/integrations/twitter/twitter-bot

+10-20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import zulip
1111

1212
VERSION = "0.9"
1313
CONFIGFILE = os.path.expanduser("~/.zulip_twitterrc")
14+
CONFIGFILE_INTERNAL = os.path.expanduser("~/.zulip_twitterrc_data")
1415
INSTRUCTIONS = r"""
1516
twitter-bot --config-file=~/.zuliprc --search="@nprnews,quantum physics"
1617
@@ -43,7 +44,7 @@ that will process tweets every 5 minutes is:
4344
4445
== Setting up Twitter authentications ==
4546
46-
Run this on a personal or trusted machine, because your API key is
47+
Run this on a personal or trusted machine, because your APIx key is
4748
visible to local users through the command line or config file.
4849
4950
This bot uses OAuth to authenticate with Twitter. Please create a
@@ -106,10 +107,10 @@ if all([opts.search_terms, opts.twitter_name]):
106107
parser.error("You must only specify either a search term or a username.")
107108
if opts.search_terms:
108109
client_type = "ZulipTwitterSearch/"
109-
CONFIGFILE_INTERNAL = os.path.expanduser("~/.zulip_twitterrc_fetchsearch")
110+
SINCE_ID_NAME = "since_id_search_" + opts.search_terms
110111
elif opts.twitter_name:
111112
client_type = "ZulipTwitter/"
112-
CONFIGFILE_INTERNAL = os.path.expanduser("~/.zulip_twitteruserrc_fetchuser")
113+
SINCE_ID_NAME = "since_id_name_" + opts.twitter_name
113114
else:
114115
parser.error("You must either specify a search term or a username.")
115116

@@ -130,17 +131,9 @@ if not all([consumer_key, consumer_secret, access_token_key, access_token_secret
130131
parser.error("Please provide a ~/.zulip_twitterrc")
131132

132133
try:
133-
since_id = config_internal.getint("twitter", "since_id")
134+
since_id = config_internal.getint("twitter", SINCE_ID_NAME)
134135
except (NoOptionError, NoSectionError):
135136
since_id = 0
136-
try:
137-
previous_twitter_name = config_internal.get("twitter", "twitter_name")
138-
except (NoOptionError, NoSectionError):
139-
previous_twitter_name = ""
140-
try:
141-
previous_search_terms = config_internal.get("twitter", "search_terms")
142-
except (NoOptionError, NoSectionError):
143-
previous_search_terms = ""
144137

145138
try:
146139
import twitter
@@ -166,16 +159,15 @@ client = zulip.init_from_options(opts, client=client_type + VERSION)
166159

167160
if opts.search_terms:
168161
search_query = " OR ".join(opts.search_terms.split(","))
169-
if since_id == 0 or opts.search_terms != previous_search_terms:
170-
# No since id yet, fetch the latest and then start monitoring from next time
171-
# Or, a different user id is being asked for, so start from scratch
172-
# Either way, fetch last 5 tweets to start off
162+
if since_id == 0:
163+
# No since id yet, fetch the latest and then start monitoring from next time.
164+
# Fetch last 5 tweets to start off
173165
statuses = api.GetSearch(search_query, count=5)
174166
else:
175167
# We have a saved last id, so insert all newer tweets into the zulip stream
176168
statuses = api.GetSearch(search_query, since_id=since_id)
177169
elif opts.twitter_name:
178-
if since_id == 0 or opts.twitter_name != previous_twitter_name:
170+
if since_id == 0:
179171
# Same strategy as for search_terms
180172
statuses = api.GetUserTimeline(screen_name=opts.twitter_name, count=5)
181173
else:
@@ -251,8 +243,6 @@ for status in statuses[::-1][: opts.limit_tweets]:
251243

252244
if "twitter" not in config_internal.sections():
253245
config_internal.add_section("twitter")
254-
config_internal.set("twitter", "since_id", str(since_id))
255-
config_internal.set("twitter", "search_terms", str(opts.search_terms))
256-
config_internal.set("twitter", "twitter_name", str(opts.twitter_name))
246+
config_internal.set("twitter", SINCE_ID_NAME, str(since_id))
257247

258248
write_config(config_internal, CONFIGFILE_INTERNAL)

0 commit comments

Comments
 (0)