Collect accounts followed by an user efficiently - python

I am using this function to collect the usernames of the accounts followed by a user. The problem is that is very unefficient because it collects the whole user object. Is there a way to collect only the username or to do it more efficiently?
def collect_followed_by_user(api,target_user):
all_followed = []
ids = []
try:
for page in tweepy.Cursor(api.friends_ids, screen_name=target_user).pages():
ids.extend(page)
for identifier in ids:
name = api.get_user(identifier).screen_name
all_followed.append(name)
except:
exit(1)
return all_followed
The function uses the Twitter API with tweepy.

As I see, tweepy is based on Twitter API (without surprise!). As it is in code of tweepy get_user() used this reference to get user of Twitter API.
It is not possible to get only names or increase efficiency with tweepy. But you could try users/lookup directly with Twitter API or find the same method in another lib.

Related

Block multiple users using Tweepy?

Using Tweepy on anaconda3, I can block a single user.
How do I block multiple users? There are 10k users. for the hurdle
How can I remove a username from the list such as excel, txt and block it?
# calling the api
api = tweepy.API(auth)
# the screen name of the user
screen_name = "xyzz"
# blocking the user
api.create_block(screen_name)
Twitter's API only allows blocking a single user with each request.
You'll have to loop over your list of users and block them one at a time.

vimeo api python using keywords to search for videos

I'm trying to use the Vimeo API with Python, but I'm stuck trying to find video's using keywords.
What I have is this, after successfully registering with Vimeo:
import vimeo
client = vimeo.VimeoClient(
token='my_token',
key='my_key',
secret='my_secret_key'
)
about_me = client.get('/me',params={'fields':'uri,name'})
json.loads(about_me.text)
these return my user credentials. Now I want to use a similar approach to get videos using Query keywords, like on their page. But I cannot get it to work.
So, I want to have a JSON returned with movies based on keywords (like 'interstellar trailer' and not the URI or id of Vimeo).
But for some reason, I can't get the query keyword to work, and from the linked page above, I cannot figure it out how to implement it in my code.
27-02-19: UPDATE: I figured it out, and will update my solution here, soon.
The /me endpoint only returns the user object for the authenticated user.
To get and search for videos, use the /me/videos or /videos endpoint (depending on if you are searching for videos on your own account, or searching for videos from other users public on Vimeo).

Tweepy mentions_timeline returns an empty list

I just started to make a Twitter Api. Normally I don't have a Twitter account, for this api I created one. I Tweeted 4 times, including some mentions. But when I use mentions_timeline like this;
my_mentions = api.mentions_timeline()
#print(my_mentions)
#output: []
After then I do a for loop on my_mentions with parameters text, screen_name but nothing returned.
What I'm doing wrong here? Why it's an empty list since I mentioned some people in the tweets + how can I search mentions for another user? Is there a parameter in mentions_timeline() object like screen_name or id ?
Try using the new Cursor Object as follows:
api = tweepy.API(auth)
for mentions in tweepy.Cursor(api.mentions_timeline).items():
# process mentions here
print mentions.text
as per Twitters documentation here
Returns the 20 most recent mentions (tweets containing a users’s
#screen_name) for the authenticating user.
so you cannot check other users mentions using this method. To achieve this, you will have to uses twitters search api. for tweepy's documentation check here
import tweepy
api = tweepy.API(auth)
api.mentions_timeline()
Try going to your profile using the same of which you're using the API and see whether the mentions exists in your profile.
and try mentioning the twitter account from different account which you trying from.
This might be the case where twitter had limited your activities and replies/tweets are not visible of that account.

Twitter search API: search by tweet id

I'd like send query to twitter search API using the tweet id but it seems you cannot search a tweet by having its id (maybe because you don't need to search it if you already have the id). For example imagine we have a tweet https://twitter.com/great_watches/status/643389532105256961 and we want to send 643389532105256961 to the search API to see if the tweet is available on the search api or not.
I need it because I just want to compare twitter search api with twitter streaming api.
I have a python script which is listening to the stream for some keywords and whenever a tweet is comming I like to search it on twitter search api to see if it is available there also or not. meaningless huh?
You can't compare the the Search API to the Streaming API the way you're doing it due to the fact they're both retrieving different types of information.
From the Search API docs:
The Twitter Search API is part of Twitter’s v1.1 REST API. It allows
queries against the indices of recent or popular Tweets and behaves
similarily to, but not exactly like the Search feature available in
Twitter mobile or web clients, such as Twitter.com search.
Before getting involved, it’s important to know that the Search API is
focused on relevance and not completeness. This means that some Tweets
and users may be missing from search results. If you want to match for
completeness you should consider using a Streaming API instead.
Here's to explain the scenario based on the information you've given.
You're streaming the word python and you find a match.
You instantly take that match and look for it on search API
The issue with that is by the time you're going from Streaming API (which is in real time) and you're looking to find the same one on the search API you'll get in conflict where more relevant tweets and popular one that will supersede it.
You'll need to redefine the query sent to the search API to include that exact same one (i.e. include more than python as you have done with the Streaming API).
You can get it using the tweepy api. Get the consumer key, secret and access key, secret from https://apps.twitter.com/ .Then run the following:
consumer_key = 'XXXX'
consumer_secret = 'XXXX'
access_key = 'XXXX-XXXX'
access_secret = 'XXXX'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
tweet = api.statuses_lookup(['643389532105256961'])
a1[0].text # Prints the message
More info here http://docs.tweepy.org/en/v3.5.0/api.html#API.statuses_lookup

How to get tweets from a user using any python library?

I'm using twitter python tools for extracting twitter information. And I'm lost when it comes to a the very simple task of getting tweets from a user.
search_results = twitter_api.search.tweets(q="", user="daguilaraguilar", count=10)
tweets = search_results['statuses']
for tweet in tweets:
print tweet['text']
But I'm getting this error message at last of HTTP 400 response:
details: {"errors":[{"code":25,"message":"Query parameters are missing."}]}
You did not define your query. According to the twitter api, it is required. Looking at the docs for the python api wrapper, I see these steps for getting the tweets of a specific user. You can probably define a since variable also according to the twitter api:
# Get your "home" timeline
t.statuses.home_timeline()
# Get a particular friend's timeline
t.statuses.friends_timeline(id="billybob")

Categories

Resources