0

I'm writing a simple Twitter bot in Python and was wondering if anybody could answer and explain the question for me.

I'm able to make Tweets, but I haven't had the bot retweet anyone yet. I'm afraid of tweeting a user's tweet multiple times. I plan to have my bot just run based on Windows Scheduled Tasks, so when the script is run (for example) the 3rd time, how do I get it so the script/bot doesn't retweet a tweet again?

To clarify my question: Say that someone tweeted at 5:59pm "#computer". Now my twitter bot is supposed to retweet anything containing #computer. Say that when the bot runs at 6:03pm it finds that tweet and retweets it. But then when the bot runs again at 6:09pm it retweets that same tweet again. How do I make sure that it doesn't retweet duplicates?

Should I create a separate text file and add in the IDs of the tweets and read through them every time the bot runs? I haven't been able to find any answers regarding this and don't know an efficient way of checking.

simplycoding
  • 2,770
  • 9
  • 46
  • 91

4 Answers4

0

You should store somewhere the timestamp of the latest tweet processed, that way you won't go throught the same tweets twice, hence not retweeting a tweet twice.

This should also make tweet processing faster (because you only process each tweet once).

halflings
  • 1,540
  • 1
  • 13
  • 34
  • I've been trying to think of how to store the timestamp 'internally' within the program itself - is it possible? I can only think of opening, writing and reading a txt file and check against that. – simplycoding Apr 12 '13 at 15:37
  • Well, if you don't save the timestamp of the last processed tweet in a file (locally or online), there's no way you could retrieve it after the program have been closed. So you should probably process the tweets one after another and save the last processed tweet when the program exits so you don't re-process it next time you open it ! – halflings Apr 12 '13 at 20:12
  • @simplycoding Can you please accept an answer or tell us what you still need ? – halflings Apr 17 '13 at 17:09
0

I wrote a twitter bot in python a few months ago and this link helped a lot. I also used this github repo which although is in Ruby, was quite helpful for logic flow. This repo uses a similar approach to what you mentioned, creating a local datastore of previous retweets to compare against each tweet.

Stedy
  • 7,359
  • 14
  • 57
  • 77
  • Any chance you could explain what you mean by a local datastore? Is it just another text file or something else? I tried going through both those repos and didn't understand much of what was going on... – simplycoding Apr 12 '13 at 15:39
  • yeah you could use a text file, or a sqlite database. I found another link that helped me a lot and the author did the same thing, wrote tweet IDs to a text file. Link here: http://inventwithpython.com/yhobos_script.py – Stedy Apr 12 '13 at 16:20
0

This is how I did it. I grabbed the list of things to retweet and a list of my feed. I cut the lists down to only posts within the past 24 hours. Then for each item in retweetable I check to see if it's in my feed list. If not I post RT @user retweet content.

I also wrote a function to chop the str down to 140 chars (137 + '...')

E.G.

TO_RT = 'a post to post'
MYTWT = ('old post', 'other old post')

if TO_RT not in MYTWT
  Tweet(TO_RT)
6ft Dan
  • 2,365
  • 1
  • 33
  • 46
0

Twitter is set such that you can't retweet the same thing more than once. So if your bot gets such a tweet, it will be redirected to an Error 403 page by the API. You can test this policy by reducing the time between each run by the script to about a minute; this will generate the Error 403 link as the current feed of tweets remains unchanged.