@@ -225,6 +225,16 @@ for status in statuses[::-1][:opts.limit_tweets]:
225
225
# https://twitter.com/eatevilpenguins/status/309995853408530432
226
226
composed = "%s (%s)" % (status .user .name , status .user .screen_name )
227
227
url = "https://twitter.com/%s/status/%s" % (status .user .screen_name , status .id )
228
+ # If the tweet is a retweet then check if the original tweet has been sent before
229
+ # and try to edit it, else post new message with the original tweet.
230
+ edit_message = False
231
+ if status .retweeted_status is not None :
232
+ try :
233
+ message_id = config_internal .getint ('twitter' , str (status .retweeted_status .id ))
234
+ edit_message = True
235
+ except (NoOptionError , NoSectionError ):
236
+ edit_message = False
237
+ url = (status .retweeted_status .urls [0 ].expanded_url + "\n " ) + ("Retweets: %s" % (status .retweeted_status .retweet_count ,))
228
238
# This contains all strings that could have caused the tweet to match our query.
229
239
text_to_check = [status .text , status .user .screen_name ]
230
240
text_to_check .extend (url .expanded_url for url in status .urls )
@@ -258,15 +268,33 @@ for status in statuses[::-1][:opts.limit_tweets]:
258
268
subject = search_term_used
259
269
elif opts .twitter_name :
260
270
subject = composed
261
-
262
- message = {
263
- "type" : "stream" ,
264
- "to" : [opts .stream ],
265
- "subject" : subject ,
266
- "content" : url
267
- }
268
-
269
- ret = client .send_message (message )
271
+ # If we are editing message it implies the current tweet is a retweet
272
+ # and has been posted before. Hence try to edit the older tweet and
273
+ # change its number of retweets but if it has passed its edit time
274
+ # do nothing.
275
+ if edit_message is True :
276
+ message_data = {
277
+ "message_id" : message_id ,
278
+ "content" : url ,
279
+ }
280
+ ret = client .update_message (message_data )
281
+
282
+ if edit_message is False :
283
+ message = {
284
+ "type" : "stream" ,
285
+ "to" : [opts .stream ],
286
+ "subject" : subject ,
287
+ "content" : url
288
+ }
289
+
290
+ ret = client .send_message (message )
291
+ if ret ['result' ] == 'success' :
292
+ if 'twitter' not in config_internal .sections ():
293
+ config_internal .add_section ('twitter' )
294
+ if status .retweeted_status is None :
295
+ config_internal .set ('twitter' , str (status .id ), str (ret ['id' ]))
296
+ else :
297
+ config_internal .set ('twitter' , str (status .retweeted_status .id ), str (ret ['id' ]))
270
298
271
299
if ret ['result' ] == 'error' :
272
300
# If sending failed (e.g. no such stream), abort and retry next time
0 commit comments