Tarik Setia
Tarik Setia

Reputation: 93

api_limit_status().remaininghits key ERROR ; Script Not Working; Cannot fetch friends

#!/usr/bin/env python # encoding: utf-8


import tweepy
import networkx 
from networkx import core
import time
import sys
import json
import codecs
import os

consumer_key=""
consumer_secret=""
access_token=""
access_token_secret=""


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)


AN_HOUR = 3630
TWENTY_MIN = 20*60
THIRTY_MIN = 30*60
LIMIT = 500

def create_egonet(twitter_api, seed_user):
    try:
        egonet = networkx.DiGraph()
        if (api.rate_limit_status()['remaining_hits'] < 5):
            print(api.rate_limit_status())
            time.sleep(TWENTY_MIN)

        friends = tweepy.Cursor(api.friends, id=seed_user).items(LIMIT)
        print "processing user" + str(seed_user)
        count = 0
        for fr in friends:
            count = count + 1
            egonet.add_edge(seed_user, str(fr.screen_name))
        print str(count) + ' edges added'
        return egonet
    except tweepy.error.TweepError as e:
        print e.reason
        return False


def build_friends_of_friends_network(twitter_api, cur_network):
    min_degree = 1
    users = nodes_at_degree(cur_network, min_degree)
    for user in users:
        time.sleep(1)
        new_network = create_egonet(twitter_api, user)
        if new_network != False:
            cur_network = networkx.compose(cur_network, new_network)
            print(str(networkx.info(cur_network)))
    return cur_network

def nodes_at_degree(network, min_degree):
    d = network.degree()
    d = d.items()
    return [(user) for (user,degree) in d if (degree >= min_degree)]

def save_network(network, original_seed):
    network._name = "original_seed_network"
    networkx.write_edgelist(network, path="twitter_network_edgelist.csv", delimiter='\t')


def core_the_network(new_network, k):
    G = new_network
    kcores = core.find_cores(G)
    core_items = kcores.items()
    two_core_or_higher = [(a) for (a,b) in core_items if b>(k-1)]
    K = networkx.subgraph(G, two_core_or_higher)
    print(str(networkx.info(K)))
    return K



def find_core_friends(cored_network, original_seed):
    K = cored_network
    # get your current friends from K
    N = K.neighbors(original_seed)
    F = []
    # for each of your current friends, gather their friends.  There hopefully will be repeats.
    for u in N:
        D = K.neighbors(u)
        for i in D:
            if N.count(i) < 1 and i is not original_seed:
                F.append(i)
    return F


def order_new_friends(friend_counts):
    F = friend_counts
    counts = dict((k, F.count(k)) for k in set(F))
    counts_items=counts.items()
    counts_items=[(b,a) for (a,b) in counts_items]
    counts_items.sort()
    counts_items.reverse()
    ff = open('suggested_friends', 'w')
    for i in counts_items:
        ff.write(str(i[0])+' of your friends are already following '+i[1] + '\n')
        print(str(i[0])+' of your friends are already following '+i[1])


from operator import itemgetter    
def find_highest_degree_friends(K):
    items = K.in_degree().items()
    items.sort(key=itemgetter(1))
    items.reverse()
    print(str(items[0:20]))

def highest_centrality(cent_dict):
    cent_items=[(b,a) for (a,b) in cent_dict.iteritems()]
    cent_items.sort()
    cent_items.reverse()
    return tuple(reversed(cent_items[0:20]))

TERMINAL EXECUTION

import TweetRecs as tr
import networkx as nx
main_net=tr.create_egonet(tr.api,'gilad')
Traceback (most recent call last):
File "", line 1, in
File "TweetRecs.py", line 59, in create_egonet
if (api.rate_limit_status()['remaining_hits'] < 5):
KeyError: 'remaining_hits'

Upvotes: 2

Views: 788

Answers (1)

tan
tan

Reputation: 1569

I faced the same problem and didn't find any help on Stackoverflow. Anyways after some trials I was able to figure out and thought of posting an answer to this question, though I am about 2 months late :-) api.rate_limit_status()['remaining_hits'] does not work since Twitter has changed its rate limiting. Twitter has become more strict with its API limits. Its no longer 350 calls for Streaming API and an unknown number (but higher than 350) for Search API.

It now rate limits by resource and the limit is 15 or 180 depending on what resource you are querying. Read rate limit by resource document to find more details. So if you want to find the remaining hits left for searches, you can issue the following command.

limits = api.rate_limit_status()
remain_search_limits = limits['resources']['search']['/search/tweets']['remaining']

Upvotes: 5

Related Questions