Extraction of the all tweets of particular user with timestamp - python-2.7

I am trying to extract the all tweets and timestamp of particular person. I am new in python and tweepy. I have the working code from internet search, but my desire is to print only all tweets of particular user.
import tweepy
# Authentication details. To obtain these visit dev.twitter.com
consumer_key = 'nWGEdfoaBt7d6wWhiAw5Tw'
consumer_secret = 'qM4QfDPqG9JQp6n0fqTCMrj6LJjES6vu2IzqpZLc'
access_token = '2284416938-JbD4F32m9xQPMxKoh6UikpCLoJm8F6xy8wDPS9P'
access_token_secret = 'XvJZQWa6zz5vHcHkUcYBacQKZJE9pcxbpxUUgNo9rN4AG'
if __name__ == '__main__':
# Create authentication token
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
print 'Getting statistics for #BarackObama:'
# Get information about the user
data = api.get_user('BarackObama')
print 'Followers: ' + str(data.followers_count)
print 'Tweets: ' + str(data.statuses_count)
print 'Favouries: ' + str(data.favourites_count)
print 'Friends: ' + str(data.friends_count)
print 'Appears on ' + str(data.listed_count) + ' lists'
print(data)
print(data) or print(status) only gives certain tweets(not all) along with other unwanted information in JSON format.
I found extracting only tweets from home and own timeline by use of following code
statuses = tweepy.Cursor(api.user_timeline).items(2)
data = [s.text.encode('utf8') for s in statuses]
print data

Anyway I got the way to download tweet at single window by passing screenname or twitter id, it works for multiple user at a same time as well

Related

How to fetch old data from twitter

I am using tweepy api to fetch old tweets
Here is my code which only fetching around most recent 3200 tweets from twitter.
Code :
import tweepy #https://github.com/tweepy/tweepy
import pymssql
#Twitter API credentials
access_key = ''
access_secret = ''
consumer_key = ''
consumer_secret = ''
#connect to local database
conn = pymssql.connect('(local)','','','mydbname')
x = conn.cursor()
def get_all_tweets(sc):
#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 = sc,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 = sc,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))
print len(alltweets)
#save tweets to database
for tweet in alltweets:
insert_post_details(tweet)
#insert tweets data into database
def insert_post_details(tweet):
try:
qry="INSERT INTO MPTTwitterPosts(Id,Created_At,Favorite_Count ,Retweet_Count,Text) VALUES(%s,%s,%s,%s,%s)"
x.execute(qry, (str(tweet.id), tweet.created_at,tweet.favorite_count,tweet.retweet_count,str(tweet.text.encode("utf-8","ignore"))))
conn.commit()
except Exception,e:
print str(e)
if __name__ == '__main__':
#pass in the username of the account you want to download
get_all_tweets("Google")
If I tried to get older tweets with max_id,it is giving blank list of tweets
so How can I get old tweets, any suggestion would be greatly helpfull.
Thanks

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")

How to get user tweets on Twitter on particular topic using Tweepy package in Python

Suppose user 'ABCD' has tweeted on topic, how to find the comments for that tweet ?
I am using tweepy to get twitter data.
It will also be good, If it possible using any other python packages.
Code for the following is:
Import tweepy
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)` # This is authentication process.
new_tweets = api.user_timeline(screen_name ='ABCD',count=20) # Code to download 20 timelines for user 'ABCD'
Till here i can get 20 timelines from user ABCD.
Q: How to get tweets for each timeline.
Thanks.

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

Tweet details from ID

Hi maybe I'm missing something really obvious but I've been using tweepy to collect tweets from a keyword.
I cannot understand, and have been searching for most of the day, how to access details about the tweets that I have, for instance their retweet count or favorite count. This using the tweet ' s id number.
Any help to a to do this would be very helpful.
You can use the statuses/show endpoint (usable in Tweepy through api.get_status. Unfortunately, any data not provided by the endpoint can only be accessed by scraping the website - there's no way to have Twitter send more data.
import tweepy
consumer_key=""
consumer_secret=""
access_key = ""
access_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
id_list = ['tweetidexample', 'tweetidexample',"....etc" ]
def get_retweet_count(tweet_id):
tweet = api.get_status(tweet_id)
return tweet.retweet_count
for id in id_list:
print get_retweet_count(id), id
This enables retweet count from ID at least.