Error 401 while extracting using python tweepy - python-2.7

I am using Twitter API.
I am getting an error while I am running this code; it prints 401 at each line like this:
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
access_token = "ENTER YOUR ACCESS TOKEN"
access_token_secret = "ENTER YOUR ACCESS TOKEN SECRET"
consumer_key = "ENTER YOUR API KEY"
consumer_secret = "ENTER YOUR API SECRET"
class StdOutListener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keywords:
stream.filter(track=['python'])
I have entered my access token and consumer key generated by the help of Twitter.
What am I doing wrong?

Related

Tweepy in python 2.7 Auth for streamlistener, receives error 401 as response

I am new to streaming tweets using the tweetpy I followed the examples, found here that explain that I need to obtain keys from tweeters. I did so and used this code to establish a connection for this streamListener. However, I only get an "Error:401". Any ideas are welcome! Thanks!
from slistener import SListener
import time, tweepy, sys
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''
OAUTH_KEYS = {'consumer_key':CONSUMER_KEY, 'consumer_secret':CONSUMER_SECRET,
'access_token_key':OAUTH_TOKEN, 'access_token_secret':OAUTH_TOKEN_SECRET}
auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)
def main():
track = ['#Matthew', '#HurricaneMatthew']
listen = SListener(api, 'myprefix')
stream = tweepy.Stream(auth, listen)
print "Streaming started..."
try:
stream.filter(track = track, languages=['en'])
except:
print "error!"
stream.disconnect()
if __name__ == '__main__':
main()
I got the code to work changing this lines in the program. with the insight found in other posts.
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
# Creation of the actual interface, using authentication
api = tweepy.API(auth)

extracting tweet from Twitter API using Python

I want to get every tweet of the HousingWire on Twitter (https://twitter.com/HousingWire). I understood how to authenticate into the twitter account but how I can get the tweet of HousingWire?
I know how to stream the data based on the keywords,but I want to stream the HousingWire tweet. how I can do that?
import time
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
ckey=''
csecret=''
atoken=''
asecret=''
class listener(StreamListener):
def on_data(self,data):
try:
print data
#tweet=data.split(',"text":"')[1].split('","source')[0]
#print tweet
#savethis=str(time.time())+'::'+tweet
savefile=open('tweetdb.txt','a')
savefile.write(data)
savefile.write('\n')
savefile.close()
return True
except BaseException,e:
print 'failed on data',str(e)
time.sleep(5)
def on_error(self,status):
print status
auth=OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
twitterStream=Stream(auth,listener())
twitterStream.filter(track=["stock"])
You can use the below Python script to grab the last 3,240 tweets from HousingWire (Twitter only allows access to that many tweets from a user - no way to grab the complete history). Usage: Simply put their twitter screen name in the script.
#!/usr/bin/env python
# encoding: utf-8
import tweepy #https://github.com/tweepy/tweepy
import csv
#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
def get_all_tweets(screen_name):
#Twitter only allows access to a users most recent 3240 tweets with this method
#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
#initialize a list to hold all the tweepy Tweets
alltweets = []
#make initial request for most recent tweets (200 is the maximum allowed count)
new_tweets = api.user_timeline(screen_name = screen_name,count=200)
#save most recent tweets
alltweets.extend(new_tweets)
#save the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
#keep grabbing tweets until there are no tweets left to grab
while len(new_tweets) > 0:
print "getting tweets before %s" % (oldest)
#all subsiquent requests use the max_id param to prevent duplicates
new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)
#save most recent tweets
alltweets.extend(new_tweets)
#update the id of the oldest tweet less one
oldest = alltweets[-1].id - 1
print "...%s tweets downloaded so far" % (len(alltweets))
#transform the tweepy tweets into a 2D array that will populate the csv
outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]
#write the csv
with open('%s_tweets.csv' % screen_name, 'wb') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","text"])
writer.writerows(outtweets)
pass
if __name__ == '__main__':
#pass in the username of the account you want to download
get_all_tweets("J_tsar")

Search the twitter stream based on keyword, username and date interval

I would like to write a program that can pull tweets from Twitter based on keywords, usernames and a date range. I have used Tweepy to write a program to extract tweets from the Streaming API. Currently, it brings tweets based on keywords OR usernames.
I would like to change the program to include a filtering critera: keywords AND usernames AND date_range. How should I modify the program ?
Python Version Used: 2.7
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
# Removed my authentication parameters because of confidentiality
consumer_key=
consumer_secret=
access_token=
access_token_secret=
class StdOutListener(StreamListener):
def on_data(self, data):
saveFile= open('save_tweets.txt','a')
saveFile.write(data)
saveFile.write('\n')
return True
def on_error(self, status):
print(status)
def on_limit(self, track):
return
def on_timeout(self):
print ('time out')
return
def on_disconnect(self, notice):
print (notice)
return
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
track_list=['art','gallery']
follow_list=['70100659','12804422']
stream.filter(track=track_list, follow=follow_list)

401 error while using tweepy

I am running the below program.But am getting the below error Message.
401 ****
401 ****
(keeps on repeating)
The code(got from some forum) basically tries to connect to Twitter and fetch tweets.
When its ran on ubuntu terminal the 401 error message appear.
import sys
import json
import pymongo
import tweepy
consumer_key="XX" ##all the keys and codes have to be strings
consumer_secret="XX"
access_token = "XX"
access_token_secret = "XX"
# This is the listener, resposible for receiving data
class StdOutListener(tweepy.StreamListener):
def on_data(self, data):
# Twitter returns data in JSON format - we need to decode it first
decoded = json.loads(data)
# Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users
print '#%s: %s' % (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'))
print ''
return True
def on_error(self, status):
print status
if __name__ == '__main__':
l = StdOutListener()
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
print "Showing all new tweets for #programming:"
# There are different kinds of streams: public stream, user stream, multi-user streams
# In this example follow #programming tag
# For more details refer to https://dev.twitter.com/docs/streaming-apis
stream = tweepy.Stream(auth, l)
stream.filter(track=['programming'])
This is how it works..!!!
Twitter keeps track of the current time.
If an API request to authenticate comes from a server that claims it is a time that is outside of 15 minutes of Twitter time, it will fail with a 401 error.
Just reset your system's clock according to world clock or let it govern by internet your problem will be solved.
Good luck..!!!

incomplete twitter friend list in python using tweepy

I tried printing my twitter friendlist.I have 112 friends but i am able to print screennames of only 20 friends. Is it because of some API limit/difference between friends and following/conceptual error.This is my code:
import tweepy
import sys
import time
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
data = api.get_user('')
count = 0
for friend in data.friends():
print 'Friend:'+ friend.screen_name
count=count+1
if count%5==0:
print count
time.sleep(20)
My interpreter is not throwing any error.
By default Twitter returns 15 records per call. To get more:
ask Tweepy for more (count=100) -- doc
or
use a Cursor to iterate over all the data; see Tutorial