Search code examples
pythontwitter

TwitterHTTPError When retrieving Twitter Trends


I am trying to understand why I am getting this error. This code is from an online class I am taking. I am changing the LOCAL_WOE_ID to my local area's WOE ID and that is when I get the error. If I use an ID for a major city, seems ok.

import pickle
import os
if not os.path.exists('secret_twitter_credentials.pkl'):
    Twitter={}
    Twitter['Consumer Key'] = '...'
    Twitter['Consumer Secret'] = '...'
    Twitter['Access Token'] = '...'
    Twitter['Access Token Secret'] = '...'
    with open('secret_twitter_credentials.pkl','wb') as f:
        pickle.dump(Twitter, f)
else:
    Twitter=pickle.load(open('secret_twitter_credentials.pkl','rb'))

WORLD_WOE_ID = 1
US_WOE_ID = 23424977

LOCAL_WOE_ID=2344925

# Prefix ID with the underscore for query string parameterization.
# Without the underscore, the twitter package appends the ID value
# to the URL itself as a special case keyword argument.

world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
us_trends = twitter_api.trends.place(_id=US_WOE_ID)
local_trends = twitter_api.trends.place(_id=LOCAL_WOE_ID)

I get this error.

TwitterHTTPError: Twitter sent status 404 for URL: 1.1/trends/place.json using parameters: (id=2344925&oauth_consumer_key=...&oauth_nonce=...&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1553037060&oauth_token=...&oauth_version=1.0&oauth_signature=...)
details: {'errors': [{'code': 34, 'message': 'Sorry, that page does not exist.'}]}

Solution

  • The problem seems to be due to the LOCAL_WOE_ID of 2344925 not being valid for the Twitter API.

    Twitter provides another API twitter.trends.available() which gives a list of all the available WOEIDs supported. API docs are at: https://developer.twitter.com/en/docs/trends/locations-with-trending-topics/api-reference/get-trends-available.html

    2344925 does NOT appear in this list (though it is found in some other WOEID lookups e.g. https://www.flickr.com/places/info/2344925), the discrepancy presumably due to Yahoo! not fully supporting their "Where On Earth" data currently (https://en.wikipedia.org/wiki/WOEID).