@@ -11,6 +11,7 @@ import zulip
11
11
12
12
VERSION = "0.9"
13
13
CONFIGFILE = os .path .expanduser ("~/.zulip_twitterrc" )
14
+ CONFIGFILE_INTERNAL = os .path .expanduser ("~/.zulip_twitterrc_data" )
14
15
INSTRUCTIONS = r"""
15
16
twitter-bot --config-file=~/.zuliprc --search="@nprnews,quantum physics"
16
17
@@ -43,7 +44,7 @@ that will process tweets every 5 minutes is:
43
44
44
45
== Setting up Twitter authentications ==
45
46
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
47
48
visible to local users through the command line or config file.
48
49
49
50
This bot uses OAuth to authenticate with Twitter. Please create a
@@ -106,10 +107,10 @@ if all([opts.search_terms, opts.twitter_name]):
106
107
parser .error ("You must only specify either a search term or a username." )
107
108
if opts .search_terms :
108
109
client_type = "ZulipTwitterSearch/"
109
- CONFIGFILE_INTERNAL = os . path . expanduser ( "~/.zulip_twitterrc_fetchsearch" )
110
+ SINCE_ID_NAME = "since_id_search_" + opts . search_terms
110
111
elif opts .twitter_name :
111
112
client_type = "ZulipTwitter/"
112
- CONFIGFILE_INTERNAL = os . path . expanduser ( "~/.zulip_twitteruserrc_fetchuser" )
113
+ SINCE_ID_NAME = "since_id_name_" + opts . twitter_name
113
114
else :
114
115
parser .error ("You must either specify a search term or a username." )
115
116
@@ -130,17 +131,9 @@ if not all([consumer_key, consumer_secret, access_token_key, access_token_secret
130
131
parser .error ("Please provide a ~/.zulip_twitterrc" )
131
132
132
133
try :
133
- since_id = config_internal .getint ("twitter" , "since_id" )
134
+ since_id = config_internal .getint ("twitter" , SINCE_ID_NAME )
134
135
except (NoOptionError , NoSectionError ):
135
136
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 = ""
144
137
145
138
try :
146
139
import twitter
@@ -166,16 +159,15 @@ client = zulip.init_from_options(opts, client=client_type + VERSION)
166
159
167
160
if opts .search_terms :
168
161
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
173
165
statuses = api .GetSearch (search_query , count = 5 )
174
166
else :
175
167
# We have a saved last id, so insert all newer tweets into the zulip stream
176
168
statuses = api .GetSearch (search_query , since_id = since_id )
177
169
elif opts .twitter_name :
178
- if since_id == 0 or opts . twitter_name != previous_twitter_name :
170
+ if since_id == 0 :
179
171
# Same strategy as for search_terms
180
172
statuses = api .GetUserTimeline (screen_name = opts .twitter_name , count = 5 )
181
173
else :
@@ -251,8 +243,6 @@ for status in statuses[::-1][: opts.limit_tweets]:
251
243
252
244
if "twitter" not in config_internal .sections ():
253
245
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 ))
257
247
258
248
write_config (config_internal , CONFIGFILE_INTERNAL )
0 commit comments