How improve my code to get friends in tweepy - python

I wrote a code to pick friends for a list of ids in twitter.
But API issues make this code very slow.
It's possible improve?
My code is:
import tweepy
consumer_key = ''
consumer_key_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
for cara in fin1:
if cara in dici1.keys(): next
else:
amigos=[]
for user in tweepy.Cursor(api.friends, screen_name=cara).items():
time.sleep(60)
try:
amigos.append(user.screen_name)
comum = [pessoa for pessoa in amigos if pessoa in fin1]
dici = {cara : comum}
dici1.update(dici)
except: time.sleep(60), next
fin1 is the list of ids(name of the user, 39 in total)
dici1 is a dict is where i store the information.

Remove the time.sleep call, it's not necessary, you also have some stuff that make no sense at all, like those next
import tweepy
consumer_key = ''
consumer_key_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
for cara in fin1:
if cara in dici1
continue
amigos = []
for user in tweepy.Cursor(api.friends, screen_name=cara).items():
try:
amigos.append(user.screen_name)
comum = [pessoa for pessoa in amigos if pessoa in fin1]
dici1[cara] = comum
except:
time.sleep(60)

Related

How to get twitter userID from username using tweepy?

I am getting error : help me I tried many times. But it's not showing id from username.
for tweet in tweepy.Cursor(api..........:
try:
screen_name= "NFTfemmefatale"
id = screen_name
get = api.get_user(id)
print("id:" + str (get))
except:
print("error")
try this:
import tweepy
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)
# calling the api
api = tweepy.API(auth)
# the screen name of the user
screen_name = "yourname"
# fetching the user
user = api.get_user(screen_name)
# fetching the ID
ID = user.id_str
print("The ID of the user is : " + ID)

Twitter, tweepy, search_30_day - doesn't produce any results comparing to norma look

Could somebody point me what i am doing wrong in here?
When using normal look without premium feature i got results
import tweepy
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
OAUTH_KEYS = {'consumer_key': consumer_key, 'consumer_secret': consumer_secret,
'access_token_key': access_token, 'access_token_secret': access_token_secret}
auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)
cricTweet = tweepy.Cursor(api.search, q='', geocode='60.00,10.00,40km').items(10)
how_many_tweets = 0
for tweet in cricTweet:
how_many_tweets += 1
print(how_many_tweets)
premiumTweet = api.search_30_day(environment_name='dev', query='point_radius:[60.00 10.00 40km]',
fromDate=202012300000, maxResults=100)
how_many_tweets = 0
for tweet in premiumTweet:
how_many_tweets += 1
print(how_many_tweets)
but when trying to do the same with premium search_30_day i got no results back and my question is what i am doing wrong

Tweepy: Trying to fetch 3200 tweets from a public twitter account

With the python code below I tried to fetch 3200 tweets from a public twitter profile, but so far I only get different amounts of tweets which are way less than the maximum of 3200 tweets and I can't really understand the problem. Can someone please explain me what I am doing wrong here?
import tweepy
import json
import pandas as pd
consumer_key = "xxx"
consumer_secret = "xxx"
access_token = "xxx"
access_token_secret = "xxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
results=[]
timeline = tweepy.Cursor(api.user_timeline, screen_name='#realDonaldTrump', tweet_mode="extended").items()
for status in timeline:
data = (
status.user.id,
status.user.screen_name,
status.user.name,
status.full_text,
status.created_at,
status.lang)
results.append(data)
cols = "user_id screen_name name text date lang".split()
df = pd.DataFrame(results, columns=cols)

Getting more than 700k followers ID from twitter using python

I was able to get the number of 75,000. After that, It keeps on pulling duplicate IDs. Here is my code. Can I get any suggestions so that I can correctly pull the large amount of follower Ids without duplicates?
import tweepy
import time
access_token = "..."
access_token_secret = "..."
consumer_key = "..."
consumer_secret = "..."
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
ids = []
while True:
try:
for page in tweepy.Cursor(api.followers_ids, screen_name = "...").pages():
ids.extend(page)
except tweepy.TweepError:
time.sleep(60*15)
continue
except StopIteration:
pass
break
I don't know why you are getting duplicates, but you could put the values into a set rather than a list to remove them efficiently.
Just change ids = [] to ids = set()
and ids.extend(page) to ids.update(page)

Parsing Twitter JSON object in Python

I am trying to download tweets from twitter.
I have used python and Tweepy for this. Though I am new to both Python and Twitter API.
My Python script is as follow:
#!usr/bin/python
#import modules
import sys
import tweepy
import json
#global variables
consumer_key = ''
consumer_secret = ''
token_key = ''
token_secret = ''
#Main function
def main():
print sys.argv[0],'starts'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(token_key, token_secret)
print 'Connected to Twitter'
api = tweepy.API(auth)
if not api.test():
print 'Twitter API test failed'
print 'Experiment with cursor'
print 'Get search method returns json objects'
json_search = api.search(q="football")
#json.loads(json_search())
print json_search
#Standard boilerplate to call main function if this file runs
if __name__ == '__main__':
main()
I am getting result as follows:
[<tweepy.models.SearchResult object at 0x9a0934c>, <tweepy.models.SearchResult object at 0x9a0986c>, <tweepy.models.SearchResult object at 0x9a096ec>, <tweepy.models.SearchResult object at 0xb76d8ccc>, <tweepy.models.SearchResult object at 0x9a09ccc>, <tweepy.models.SearchResult object at 0x9a0974c>, <tweepy.models.SearchResult object at 0x9a0940c>, <tweepy.models.SearchResult object at 0x99fdfcc>, <tweepy.models.SearchResult object at 0x99fdfec>, <tweepy.models.SearchResult object at 0x9a08cec>, <tweepy.models.SearchResult object at 0x9a08f4c>, <tweepy.models.SearchResult object at 0x9a08eec>, <tweepy.models.SearchResult object at 0x9a08a4c>, <tweepy.models.SearchResult object at 0x9a08c0c>, <tweepy.models.SearchResult object at 0x9a08dcc>]
Now I am confused how to extract tweets from this information?
I tried to use json.loads method on this data. But it gives me error as JSON expects string or buffer.
Example code would be highly appreciated.
Thanks in advance.
Tweepy gives you richer objects; it parsed the JSON for you.
The SearchResult objects have the same attributes as the JSON structures that Twitter sent; just look up the Tweet documentation to see what is available:
for result in api.search(q="football"):
print result.text
Demo:
>>> import tweepy
>>> tweepy.__version__
'3.3.0'
>>> consumer_key = '<consumer_key>'
>>> consumer_secret = '<consumer_secret>'
>>> access_token = '<access_token>'
>>> access_token_secret = '<access_token_secret>'
>>> auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
>>> auth.set_access_token(access_token, access_token_secret)
>>> api = tweepy.API(auth)
>>> for result in api.search(q="football"):
... print result.text
...
Great moments from the Women's FA Cup http://t.co/Y4C0LFJed9
RT #freebets: 6 YEARS AGO TODAY:
Football lost one of its great managers.
RIP Sir Bobby Robson. http://t.co/NCo90ZIUPY
RT #Oddschanger: COMPETITION CLOSES TODAY!
Win a Premier League or Football League shirt of YOUR choice!
RETWEET & FOLLOW to enter. http…
Berita Transfer: Transfer rumours and paper review – Friday, July 31 http://t.co/qRrDIEP2zh [TS] #nobar #gosip
#ajperry18 im sorry I don't know this football shit😂😅
#risu_football おれモロ誕生日で北辰なんすよ笑
NFF Unveils Oliseh As Super Eagles Coach - SUNDAY Oliseh has been unveiled by the Nigeria Football... http://t.co/IOYajD9bi2 #Sports
RT #BilelGhazi: RT #lequipe : Gourcuff, au tour de Guingamp http://t.co/Dkio8v9LZq
#EDS_Amy HP SAUCE ?
RT #fsntweet: マンCの塩対応に怒りの炎!ベトナム人ファン、チケットを燃やして猛抗議 - http://t.co/yg5iuABy3K
なめるなよ、プレミアリーグ!マンチェスターCのプレシーズンツアーの行き先でベトナム人男性が、衝撃的な行
RT #peterMwendo: Le football cest un sport collectif ou on doit se faire des passe http://t.co/61hy138yo8
RT #TSBible: 6 years ago today, football lost a true gentleman. Rest in Peace Sir Bobby Robson. http://t.co/6eHTI6UxaC
6 years ago today the greatest football manger of all time passed away SIR Bobby Robson a true Ipswich and footballing legend
The Guardian: PSG close to sealing £40m deal for Manchester United’s Ángel Di María. http://t.co/gAQEucRLZa
Sir Bobby Robson, the #football #legend passed away 6 years ago.
#Barcelona #newcastle #Porto http://t.co/4UXpnvrHhS
You can use the JSON parser to achieve this, here is my code on App Engine that handles a JSONP response ready to be used in with a JQuery client:
import webapp2
import tweepy
import json
from tweepy.parsers import JSONParser
class APISearchHandler(webapp2.RequestHandler):
def get(self):
CONSUMER_KEY = 'xxxx'
CONSUMER_SECRET = 'xxxx'
ACCESS_TOKEN_KEY = 'xxxx'
ACCESS_TOKEN_SECRET = 'xxxx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, parser=JSONParser())
# Query String Parameters
qs = self.request.get('q')
max_id = self.request.get('max_id')
# JSONP Callback
callback = self.request.get('callback')
max_tweets = 100
search_results = api.search(q=qs, count=max_tweets, max_id=max_id)
json_str = json.dumps( search_results )
if callback:
response = "%s(%s)" % (callback, json_str)
else:
response = json_str
self.response.write( response )
So the key point is
api = tweepy.API(auth, parser=JSONParser())
Instead of using global variables, I would reorganize the code in a python class:
import tweepy
class TweetPrinter():
"""
Simple class to print tweets
"""
def __init__(self, consumer_key, consumer_secret, access_token,
access_token_secret):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.access_token = access_token
self.access_token_secret = access_token_secret
self.auth = tweepy.OAuthHandler(self.consumer_key,
self.consumer_secret)
self.auth.set_access_token(access_token, access_token_secret)
def tweet_print(self):
api = tweepy.API(self.auth)
football_tweets = api.search(q="football")
for tweet in football_tweets:
print(tweet.text)
def main():
tweet_printer = TweetPrinter(my_consumer_key, my_consumer_secret,
my_access_token, my_access_token_secret)
tweet_printer.tweet_print()
if __name__ == '__main__':
main()
take my code for tweepy:
def twitterfeed():
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
statuses = tweepy.Cursor(api.home_timeline).items(20)
data = [s.text.encode('utf8') for s in statuses]
print data

Categories

Resources