Reputation: 3993
I give my code as in the below MWE
which works fine.
import tweepy
BEARER = "my-bearer-token"
class MyListener(tweepy.StreamingClient):
def on_data(self, data):
try:
with open('python.json', 'a') as f:
f.write(str(data))
print(data)
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
def on_connect(self):
print('Connected..!')
def on_error(self, status):
print(status)
return True
And I define the following rules to get location specific data:
stream = MyListener(BEARER)
stream.add_rules(tweepy.StreamRule('place:Lagos OR place:Abuja has:geo'))
stream.filter()
And successfully retrieved the following data (from print(data)
statement):
$ python tweetify.py
Connected..!
b'{"data":{"id":"1559694255035138048","text":"@halimahthebird N*gga the next day "},"matching_rules":[{"id":"1559694230766895104","tag":""}]}'
b'{"data":{"id":"1559694261976764421","text":"@O_luwatomiwa Mental illness I tell you \xf0\x9f\x98\x82"},"matching_rules":[{"id":"1559694230766895104","tag":""}]}'
b'{"data":{"id":"1559694339709730816","text":"@elonmusk He gon buy united at the dip"},"matching_rules":[{"id":"1559694230766895104","tag":""}]}'
...
Good that the filter works, those tweets came from the defined place in rule.
*Question
But I want to retrieve the location coordinates [LONG, LAT]
not the tweets. I added the operator has:geo
but not sure how to retrieve the coordinates.
Upvotes: 2
Views: 414
Reputation:
You'll need to set your code to request the fields you need, something like:
class MyListener(tweepy.StreamingClient):
def on_data(self, data):
data_obj = json.loads(data.decode('utf8'))
print(json.dumps(data_obj,indent=2))
return True
def on_connect(self):
print('Connected..!')
def on_error(self, status):
print(status)
return True
stream = MyListener(BEARER)
stream.add_rules(tweepy.StreamRule('place:London OR place:Paris has:geo', tag="london-paris"))
stream.filter(tweet_fields=["geo","created_at","author_id"],place_fields=["id","geo","name","country_code","place_type","full_name","country"],expansions=["geo.place_id"])
Lat/lon information would be in the includes.places.geo.bbox
object.
Upvotes: 3