AGH_TORN
AGH_TORN

Reputation: 833

How to get Twitter username from Tweepy?

I've been using Tweepy to gather Tweets for an area via the streaming API and I've been only collecting latitude/longitude for a tweet but I'd like to add more to it and I'm not sure what the specifics are. I'm using this block of code to get lat/long values:

import json, tweepy
from html.parser import HTMLParser

consumer_key = ""
consumer_secret = ""
access_token = ""
access_secret = ""

count = 0

class StdOutListener(tweepy.StreamListener):
   def on_data(self, data):
      global count
      decoded = json.loads(HTMLParser().unescape(data))
      if decoded.get('coordinates',None) is not None:
         coordinates = decoded.get('coordinates','').get('coordinates','')
         name = decoded.get('name','')
         with open("C:\\Users\\gchre\\Desktop\\Tweets.txt", "a") as text_file:
            print(decoded['coordinates'], file=text_file)
         print(decoded['coordinates'])
         count += 1
      return True
   def on_error(self, status):
      print(status)

l = StdOutListener()
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
stream = tweepy.Stream(auth, l)

while count < 1000000:
   stream.filter(locations=[-88.853859,41.220047,-86.953073,42.758134])

I'd like for this to also print out into the text file the specific username (@handle) and the time the Tweet was created. I'm not sure if I should be doing this inside the if decoded.get('coordinates',None) is not None: loop or not.

Upvotes: 0

Views: 3255

Answers (2)

AGH_TORN
AGH_TORN

Reputation: 833

For those interested, I figured it out, within the if decoded.get() loop, I added the following:

user = decoded.get('user','').get('screen_name','')
date = decoded.get('created_at','')

Then within the print line I added the values:

print((decoded['coordinates'], user, date), file=text_file)

Upvotes: 2

rojeeer
rojeeer

Reputation: 2011

I think you need to read the documentation from Twitter Dev to understand the data structure of a tweet.

Thanks.

Upvotes: 1

Related Questions