-3

Trying to run this twitter python bot. It just basicly needs to setup the tokens (already seted) and hashtag to search and favorite. I'm newbie into coding so i really dunno where i'm going here. Just cloned this code from github and trying to run it. Running python 2.7.2

"""

A simple Twitter bot using the Twitter Python library that finds users who tweet about "Christmas gift ideas,"
favorites their tweet, follows the users and sends them a friendly tweet with Amazon links of popular gift ideas.

"""

import urllib
import simplejson
import twitter

consumer_key = ''
consumer_secret = ''
access_token_key = ''
access_token_secret = ''

def searchTweets(query):
        search = urllib.urlopen("http://search.twitter.com/search.json?q="+query)
        dict = simplejson.loads(search.read())
        return dict

api = twitter.Api(consumer_key = 'consumer_key', consumer_secret = 'consumer_secret', access_token_key = 'access_token_key', access_token_secret = 'access_token_secret')
tweets = searchTweets("hashtag")
msg = "Tweet message"

for i in range(len(tweets["results"])):
        tweeter = tweets["results"][i]["from_user"]
        status = twitter.Api.GetStatus(api, tweets["results"][i]["id"])
        api.CreateFavorite(status)
        api.CreateFriendship(tweeter)
        api.PostUpdate('@' + tweeter + ' ' + msg)

I've changed the tokens info and stuff, but i'm getting this error:

Traceback (most recent call last):
  File "TwitterBot.py", line 26, in <module>
    for i in range(len(tweets["results"])):
KeyError: 'results'

What am I doing wrong? I'm using python-twitter module installed with pip.

fortes
  • 7
  • 1

1 Answers1

0

There is no key `'results' in the dictionary 'tweets'.

To find out what there is, add this line of code before the for loop:

print tweets

and it will print something like this:

result = {"items": [{ "owner": { "reputation": 1480, "user_id": 2254048 },"is_accepted": false,"score": 0, "answer_id": 32548557, "question_id": 32546733}]}

Here, there is a nested dictionary in a nested list. That could be your issue. In this example you need to use this code:

print result["items"][0]["owner"]["user_id"]

just to view the user ID of 2254048. Check that twitter isn't giving something like that, and ensure you are a) referencing a key that exists and b) if it's nested, you're referencing it with the other nested items correctly.

Tim
  • 2,563
  • 1
  • 23
  • 31
  • Now i got this new error: ` {'errors': [{'message': 'The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.', 'code': 64}]} Traceback (most recent call last): File "TwitterBot.py", line 28, in for i in range(len(tweets["results"])): KeyError: 'results' ` I think this might bt the problem. – fortes Sep 13 '15 at 10:24
  • @fortes well you didn't really listen but it tells you the issue there - 1) API v1 isn't active. You need to use the twitter API v1.1. That also explains why there was no results - there is no item results in the dictionary! – Tim Sep 13 '15 at 10:39