Twitter API: Get top tweets by query and WOEID place

1.7k Views Asked by At

Preferably via Tweepy in Python, I want to obtain from the Twitter API a list of top tweets for a given search query and WOEID place identifier (Yahoo's Where On Earth IDentifier).

In my example, I obtain trending queries for a WOEID id via Tweepy's API.trends_place(id) wrapper for the Twitter REST API's GET trends/place; I then want to print the top tweets for each trending query within this place (same WOEID).

Currently, I obtain tweets for the trending query, but

  1. not within the given place;
  2. not necessarily the "top" tweets (as opposed to, for example, "recent").

How can I add these two restrictions to my search?


MWE:

import tweepy
from tweepy import OAuthHandler

consumer_key = 'YOUR-CONSUMER-KEY'
consumer_secret = 'YOUR-CONSUMER-SECRET'
access_token = 'YOUR-ACCESS-TOKEN'
access_secret = 'YOUR-ACCESS-SECRET'

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)

locationid = 23424775  # WOEID for Canada

trendqueries = [trend['query'] for trend in api.trends_place(locationid)[0]['trends']]

for trendquery in trendqueries:
    print(api.search(q=trendquery))

What I have tried:

I can search by longitude/latitude using Tweepy's API.search(q, geocode), but I do not see an obvious way to search by WOEID.

1

There are 1 best solutions below

1
On

Partial answer

API.search(q[, lang][, locale][, rpp][, page][, since_id][, geocode][, show_user]) Returns tweets that match a specified query.

Parameters:

geocode – Returns tweets by users located within a given radius of the given latitude/longitude. The location is preferentially taking from the Geotagging API, but will fall back to their Twitter profile. The parameter value is specified by “latitide,longitude,radius”, where radius units must be specified as either “mi” (miles) or “km” (kilometers). Note that you cannot use the near operator via the API to geocode arbitrary locations; however you can use this geocode parameter to search near geocodes directly. show_user – When true, prepends “:” to the beginning of the tweet. This is useful for readers that do not display Atom’s author field. The default is false.