0

I'm trying to get tweets by hashtags. It seems to load them but at the point to save the tweet id, I get:

"TypeError: String indices must be integers". 

I think the problem is something like this.

My code:

from twython import Twython, TwythonError
import time
import math
import json

def twitter_oauth_login():
    API_KEY = ""
    API_SECRET = ""
    ACCESS_TOKEN = ""
    ACCESS_TOKEN_SECRET = ""
    twitter = Twython(API_KEY,API_SECRET,ACCESS_TOKEN,ACCESS_TOKEN_SECRET)
    return(twitter)

twitter=twitter_oauth_login()

def get_minimum_id(list_of_tweets):
    ids = [] 
    idsproxy=[]
    for tweet in list_of_tweets:
        this_id = tweet['id'] #The problem appears here
        this_id = int(this_id) 
        print this_id
        ids.append(this_id) 
    if ids == idsproxy:
        minimum = 0
        return minimum
    else:
        minimum = min(ids) 
        return minimum 

def get_complete_timeline(hashtag):
     twitter = twitter_oauth_login()
     all_tweets = [] 
     max_id = None 
     while True: 
        new_tweets = twitter.search(q='#'+hashtag, count=200,    max_id=max_id)
        print u"Identified" + str(len(new_tweets)) + u" new tweet for user  " + hashtag
        all_tweets.extend(new_tweets) 

        print "Wait 10 seconds"
        time.sleep(10) 
        min_id = get_minimum_id(new_tweets) 
        max_id = min_id - 1 
        if len(new_tweets) < 100:
            break 
            print "Task complete: in total " + len(all_tweets) + " tweets were loaded."
     return all_tweets

The traceback:

TypeError Traceback (most recent call last)
<ipython-input-52-708b711e136b> in <module>()
 ----> 1 get_complete_timeline("omg")

<ipython-input-51-34204cb2269e> in get_complete_timeline(hashtag)
     42         print "Wait 10 seconds"
     43         time.sleep(10)
---> 44         min_id = get_minimum_id(new_tweets) 
     45         max_id = min_id - 1 
     46         if len(new_tweets) < 100:

<ipython-input-51-34204cb2269e> in get_minimum_id(list_of_tweets)
     19         for tweet in list_of_tweets: 
     20                 print '1'
---> 21                 this_id = tweet['id'] 
     22                 print '2'
     23                 this_id = int(this_id) 

TypeError: string indices must be integers
Forge
  • 6,538
  • 6
  • 44
  • 64
derskye
  • 11
  • 1
  • could you please copy/paste the full stacktrace here? because TypeError can be raised by many lines in the script you posted :) – Markon Mar 11 '16 at 12:23

1 Answers1

0

The variable list_of_tweets(or new_tweets) as returned from twitter.search is a dictionary Type.

You are not iterating correctly over the dictionary, just like in this example:

d = {'a':1, 'b': 2}
for value in d:
   print value

# This would iterate over the dictionary keys
>> 'a'
>> 'b'

Correct iteration over dictionary is using the iteritems method:

d = {'a':1, 'b': 2}
for key, value in d.iteritems():
   print key, value

>> 'a', 1
>> 'b', 2

To iterate over the statuses returned:

for tweet in list_of_tweets['statuses']:
    print tweet['id'] 
Forge
  • 6,538
  • 6
  • 44
  • 64