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