download a list users from twitter - twitter

I have a list of "n" Twitter ID representing users I would like to download.
To retrieve their user profile info, should I use n times the api call get_user or there exist a method to pass the entire list and retrieve all the info within one single api call, within the twitter rate time limit?
I tried something like
api.search_users(A)
api.search_users(id in A)
where A contains the list of id
but it does not work.
Anyone helping?

You can use the Twitter API resource https://dev.twitter.com/rest/reference/get/users/lookup. It can return user objects for at most 100 users at a time.
You can use this in Tweepy like:
user_objects = api.lookup_users(user_ids=list_of_at_most_100_user_ids)
Much of this answer first appeared as part of https://stackoverflow.com/a/42946854/1921546

Related

Youtube - How to browse all channels in a given category?

For example, there are 884 channels under beauty and fashion, however, Youtube only shows about 50 of them. How do i get the complete list? Either through API or web.
https://www.youtube.com/channels/beauty_fashion
Thanks,
The first thing to do is to get the Guide Category ID that you're interested in. If you do a call to
https://www.googleapis.com/youtube/v3/guideCategories?part=snippet&hl=en&regionCode=US&key={YOUR_API_KEY}
where the hl parameter is the language and the regionCode is the country code (as some categories may not be available for particular languages/regions), you'll get a list of all the categories and their IDs.
For example, that call tells us that the Beauty and Fashion guide category ID is GCQmVhdXR5ICYgRmFzaGlvbg. With that ID, we can then do a channels list call:
https://www.googleapis.com/youtube/v3/channels?part=snippet&maxResults=50&categoryId=GCQmVhdXR5ICYgRmFzaGlvbg&key={YOUR_API_KEY}
This will give you 50 channels in that category. It will also give you a 'nextPageToken' ... you do the same request as above, but add "&pageToken={WHATEVER THAT NEXT PAGE TOKEN VALUE IS}" to get the next 50, and so on.
You can retrieve up to 500 that way ... that's the limit through the API.
Note that all of these calls require an API key from console.developers.google.com
Visit this site: http://www.channelcrawler.com/
You can list the channel in selected category and many other options.

How to retrieve hashtaged tweets from a list of users

Is there a way retrieving all the tweets from a list of profiles (3) which are tagged with certain #hashtag in a single call to the Twitter API using 1.1?
If not, obviously, I'd be retrieving a hundred tweets from each user, and filtering out those which do not have the #hashtag .. but it's not very efficient, right?
Thanks in advance
Note: I've updated the library so I suggest you grab the newly updated version before trying this - I've made it so you don't need to manually encode each individual character.
This page shows you how to use search, and has a number of operators down toward the bottom of the page. One of these is the OR operator:
Getting tweets for multiple users
OR - Either this or that
love OR hate - containing either "love" or "hate" (or both)
From - From this user
from:twitterapi sent from the user #twitterapi
So, armed with the above knowledge, I'm guessing your query would look like this:
Translated into a GET request:
?q=from:user1+OR+from:user2
Getting tweets for specific hashtags
So that's how you get tweets for multiple users. The next step you want is for certain hashtags.
#haiku - containing the hashtag "haiku"
That translated individually into the correct format becomes:
?#haiku (or %2C haiku, depending on the library / urlencoding you're using)
Combining the above
The standard AND operator looks like this:
twitter search - containing both "twitter" and "search". Default operator
For a get request:
?twitter+search
So let's combine all this!
?q=#hashtag1+#hashtag2+from:user1+OR+from:user2
And, because you're using my lib, here's the code for that:
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=#hashtag1+OR+#hashtag2+from:username1+OR+from:username2';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
var_dump(json_decode($json));

Twitter API and operator

I want to make a simple Twitter API get using JSON. The idea is to get only the posts from a known corporation (say BigCorporation) sent to the main corporation (#BigCorporation). The idea is to just get the main annoncements and filter out anytime the BigCoporation answer a question from a follower (i.e. BigCoporation tweets: #someguy we are anwering your question)
So Tweets from:BigCoporation with the #BigCorporation
I came up with this API get but I seem to get both #BigCorporation or from:BigCorporation tweets. I want only the Tweets that are #BigCorp AND from:BigCorp
http://search.twitter.com/search.json?q=#BigCorporation&q=from:BigCorporation&result_type=mixed
Your search query is malformed.
It should be:
http://search.twitter.com/search.json?q=#edent%20from:edent&result_type=mixed
That will show you all the tweets I have sent which contains the string "#edent"
You can use this URL :
https://api.twitter.com/1/statuses/user_timeline.json?screen_name=BigCorporation&exclude_replies=true
The REST method GET https://api.twitter.com/1/statuses/user_timeline.json returns the most recent tweets (20 by default) of a user whose username (here BigCorporation) is in the parameter screen_name. When set to true (or t or 1), the exclude_replies parameter excludes the tweets that you want to avoid ("#someguy we are anwering your question").
You can visit this page on the Twitter API website for further details.

Twitter API, Twitteroauth and get users lookup order

I use "twitteroauth" and this code fragment:
$userlist='111111,66666,7777,22222';
$Results=$oauth->get( 'users/lookup', array('user_id' => $userlist ) );
After i print out the results, i see that the order of "$Results" is not ok.
For example:
$Results[0] gathers the data of '7777',
$Results[1] gathers the data of '111111'.
I tried this for ten times. Every time the order in array changes.
Is it normal behaviour of twitter API ?
Thank you
Yes this is to be expected. Array order from Twitter is not guaranteed. You will have to use the user_id of each object.

Twitter API - Display all tweets with a certain hashtag?

How would I go about displaying tweets that contain a certain hashtag using the Twitter API? Thanks
I'd also like to know if there is a way to get all tweets from a certain hashtag in a separate file, also the ones that don't show up in your feed anymore. I suppose that's what the earlier question was about, too.
This answer was written in 2010. The API it uses has since been retired. It is kept for historical interest only.
Search for it.
Make sure include_entities is set to true to get hashtag results. See Tweet Entities
Returns 5 mixed results with Twitter.com user IDs plus entities for the term "blue angels":
GET http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&with_twitter_user_id=true&result_type=mixed
UPDATE for v1.1:
Rather than giving q="search_string" give it q="hashtag" in URL encoded form to return results with HASHTAG ONLY. So your query would become:
GET https://api.twitter.com/1.1/search/tweets.json?q=%23freebandnames
%23 is URL encoded form of #. Try the link out in your browser and it should work.
You can optimize the query by adding since_id and max_id parameters detailed here. Hope this helps !
Note: Search API is now a OAUTH authenticated call, so please include your access_tokens to the above call
Updated
Twitter Search doc link:
https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
The answer here worked better for me as it isolates the search on the hashtag, not just returning results that contain the search string. In the answer above you would still need to parse the JSON response to see if the entities.hashtags array is not empty.

Resources