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