Using Tweepy in a simple loop to print Tweets

266 views
Skip to first unread message

Callahan

unread,
Oct 30, 2013, 9:03:52 AM10/30/13
to twe...@googlegroups.com
Hi,

So I have written a simple loop to pull down the last 10 mentions and display them on the screen then wait 90 seconds before rechecking. I'm having an issue with printing out the tweets themselves. As the code runs, it pulls down the last 10 mentions, adds them to a list, checks if that the ID of the tweet has already been shown on screen and if not, displays the tweet ID and the tweet text. The problem is that the tweet ID on the screen increments based on the tweet being displayed but the text displayed is always the text of the last tweet. I can't figure out why this is happening as if the ID is changing the text should be changing as its part of the same loop.

Here's the code below. Any help would be much appreciated.

flashed = []
tweet_ids = []

while True:
    countdown = 90
    for tweet in api.mentions_timeline(count = 10):
        if tweet.id not in tweet_ids:
            tweet_ids.append(tweet.id)
    for tweet.id in tweet_ids: ## Work through the tweet_ids list and check each tweet.id entry
            if tweet.id not in flashed: ## Check if the tweet.id exists in the "flashed" list. If it doesn't, continue
                print ""
                print "******************************************************"
                print "*                  Mention detected!                 *"
                print "*               %s" % time.ctime(), "*".rjust(13,  )
                print "******************************************************"
                print "                       Tweet ID:"
                print "                  %d" % (tweet.id)
                print "                        Tweet:"
                print "@%s: %s" % (tweet.author.screen_name, tweet.text)        ## Print the tweet on screen
                print ""
    while countdown > 0: ## Start while loop countdown
        print "Rechecking in:" ## Show extra info on screen
        time.sleep(1)  ## Sleep for 1 second
        print(countdown) ## Print the current value of the variable "countdown"
        countdown -= 1 ## Minus 1 from the current value of the variable "countdown"
        if countdown == 0: ## When countdown gets to zero, print extra info on screen
            print "Rechecking NOW..."
            print ""

nils.g...@gmail.com

unread,
Nov 21, 2013, 2:59:05 AM11/21/13
to twe...@googlegroups.com

Your error is here:


    for tweet.id in tweet_ids: ## Work through the tweet_ids list and check each tweet.id entry

You're setting a property on the last tweet object to new numbers, but seem to think that will change the rest of the object as well.

Either save each tweet object, instead of only its id, or save id and text of each object, for example in a dictionary.

Nils

Reply all
Reply to author
Forward
0 new messages