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