user6035961
user6035961

Reputation:

Getting Twitter usernames in Python

I'm trying to read tweets for the following keywords on Twitter (cycling, running, sports suck) and would also like to get the username of the person who sent the Tweet. I've scoured the web, but haven't come up with an answer that seems to work. I don't think I understand exactly what it is I'm looking for? Any help would be greatly appreciated, my code so far is below...

Thanks

import tweepy
from time import sleep 
import sys
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)

CONSUMER_KEY = 'xxxx'
CONSUMER_SECRET = 'xxxx'
ACCESS_KEY = 'xxxx'
ACCESS_SECRET = 'xxxx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
auth.secure = True
api = tweepy.API(auth)

class MyStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        if 'cycling' in status.text.lower():
                print('BIKE!')
                print(status.text.translate(non_bmp_map))
        elif 'running' in status.text.lower():
            print('RUN!')
            print(status.text.translate(non_bmp_map))
        else:
            print('SPORTS SUCK!')
            print(status.text.translate(non_bmp_map))

myStreamListener = MyStreamListener
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener())

myStream.filter(track=['cycling', 'running', 'sports suck'])

I've added this code... (the lines with # in). It keeps coming up with an error message: TypeError: 'type' object is not iterable Any ideas?

class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
    for tweet in tweepy.Cursor: #I'VE ADDED THIS LINE
        if 'cycling' in status.text.lower():
                print('BIKE!')
                print(status.text.translate(non_bmp_map))
                print(tweet.user.screen_name) #I'VE ADDED THIS LINE
        elif 'running' in status.text.lower():
            print('RUN!')
            print(status.text.translate(non_bmp_map))

Upvotes: 1

Views: 320

Answers (1)

ThaoD5
ThaoD5

Reputation: 183

You should definitely look into this Python API that allows to receive the tweet sent and the username of the sender. I had no experience with python and I made it work really fast and easy.

All useful source code & way of getting it to work is on the git page :-)

Upvotes: 2

Related Questions