tweepy get list of failed lookup_users - python

I'd like to check if a set of twitter users are exist or not. As I couldn't find any better method, I used api.lookup_users() but it returns nothing when a user name is not found. How can I get list of users that are failed to look up.
>>> api = tweepy.API(auth)
>>> user_ids = api.lookup_users(screen_names['hamid', 'python'])
>>> print user_ids
[19877641]
But I need the failed ones also something like: [19877641,None] or [19877641,""] etc.

That's more on Twitter's side than tweepy. If a user is not found using GET users/lookup nothing is returned. So tweepy is returning what is being received from the API.
From their docs:
There are a few things to note when using this method.
You must be following a protected user to be able to see their most recent status update. If you don’t follow a protected user their
status will be removed.
The order of user IDs or screen names may not match the order of users in the returned array.
If a requested user is unknown, suspended, or deleted, then that user will not be returned in the results list.
If none of your lookup criteria can be satisfied by returning a user object, a HTTP 404 will be thrown.
You are strongly encouraged to use a POST for larger requests.
A possible workaround is to send the users individually and if you get None then change your list accordingly. Unfortunately that can hinder you limit.

Related

When the get_user(..) function is called, additional information cannot be loaded

I'm having a similar problem, but I haven't been able to find a solution.
api.get_user with Tweepy will not give description
I use tweepy (4.8.0) and auth 2.0 (bearer_token)
I tried to load user information by using get_user(...) like this
client = tweepy.Client(
bearer_token=bearer_token
)
result = client.get_user(username="name", user_fields=['created_at'])
I expected to get additional data put in the user_fields, but only simple basic data was passed.
Response(data=<User id=1234 name=NAME username=name>, includes={}, errors=[], meta={})
Maybe I'm missing something or made a mistake?
save me plz...
My answer to that question applies here as well.
From the relevant FAQ section in Tweepy's documentation:
Why am I not getting expansions or fields data with API v2 using Client?
If you are simply printing the objects and looking at that output, the string representations of API v2 models/objects only include the default attributes that are guaranteed to exist.
The objects themselves still include the relevant data, which you can access as attributes or by key, like a dictionary.

Checking username availability - Handling of AJAX requests (Google App Engine)

I want to add the 'check username available' functionality on my signup page using AJAX. I have few doubts about the way I should implement it.
With which event should I register my AJAX requests? We can send the
requests when user focus out of the 'username' input field (blur
event) or as he types (keyup event). Which provides better user
experience?
On the server side, a simple way of dealing with requests would be
to query my main 'Accounts' database. But this could lead to a lot
of request hitting my database(even more if we POST using the keyup
event). Should I maintain a separate model for registered usernames
only and use that to get better results?
Is it possible to use Memcache in this case? Initializing cache with
every username as key and updating it as we register users and use a
random key to check if cache is actually initialized or pass the
queries directly to db.
Answers -
Do the check on blur. If you do it on key up, you will be hammering your server with unnecessary queries, annoying the user who is not yet done typing, and likely lag the typing anyway.
If your Account entity is very large, you may want to create a separate AccountName entity, and create a matching such entity whenever you create a real Account (but this is probably an unnecessary optimization). When you create the Account (or AccountName), be sure to assign id=name when you create it. Then you can do an AccountName.get_by_id(name) to quickly see if the AccountName has already been assigned, and it will automatically pull it from memcache if it has been recently dealt with.
By default, GAE NDB will automatically populate memcache for you when you put or get entities. If you follow my advice in step 2, things will be very fast and you won't have to mess around with pre-populating memcache.
If you are concerned about 2 people simultaneously requesting the same user name, put your create method in a transaction:
#classmethod
#ndb.transactional()
def create_account(cls, name, other_params):
acct = Account.get_by_id(name)
if not acct:
acct = Account(id=name, other param assigns)
acct.put()
I would recommend the blur event of the username field, combined with some sort of inline error/warning display.
I would also suggest maintaining a memcache of registered usernames, to reduce DB hits and improve user experience - although probably not populate this with a warm-up, but instead only when requests are made. This is sometimes called a "Repository" pattern.
BUT, you can only populate the cache with USED usernames - you should not store the "available" usernames here (or if you do, use a much lower timeout).
You should always check directly against the DB/Datastore when actually performing the registration. And ideally in some sort of transactional method so that you don't have race conditions with multiple people registering.
BUT, all of this work is dependant on several things, including how busy your app is and what data storage tech you are using!

How to fetch tweets of a given user?

I'm trying to use the Twitter API in order to automatically fetch the last tweet of a given user, but I'm having trouble :/
I am using this library : https://code.google.com/p/python-twitter/
I installed it, everything seems working, but when I try to fetch the timeline of a user, I only get all my timeline :(
Here is my code:
import twitter
api = twitter.Api(consumer_key='***', consumer_secret='****', access_token_key='***', access_token_secret='****')
statuses = api.GetUserTimeline('#twitterapi')
print [s.text for s in statuses]
Is there something I missed ?
I believe you have to provide the userId rather than the screen_name in order for the GetUserTimeLine to work.
Also, although you might expect that this would return the equivalent of that user's home status page, it does not. Instead, it returns just the tweets from that user.
The twitter API documentation mentions another method - GetFriendsTimeline, but, despite being listed in the documentation, it doesn't seem to exist as far as I can tell.
You must explicitly enter screen_name= or user_id= otherwise the value defaults to the authenticated user.
Examples:
statuses = api.GetUserTimeline(screen_name='some_handle')
or
statuses = api.GetUserTimeline(user_id=22233344)

How to be sure that a user is in a group

When a user is added to a group using client.AddMemberToGroup(group_id, member_id) they are not appearing in the groups UI of the cPanel they are also not reported as a member when client.RetrieveAllMembers(group_id) is run.
However, when client.RetrieveGroups(member_id) is run it does return an entry in the feed for the group in question.
How do I know for sure which is correct? Is the person in the group or not?
How can I verify without doubt whether a given user is in a group or not?
I have already submited this as an issue into the issue tracker but wondered if anyone here has any ideas?
http://code.google.com/a/google.com/p/apps-api-issues/issues/detail?id=3327
If it is a very large group, it may take some time for the full group list to be refreshed and the user to appear in it.
Try waiting 24 hours and checking the CPanel / RetrieveAllMembers() calls again.
In the meantime, you're doing things right using RetrieveGroups() to determine if the user is a member.
Update: The Google CPanel and the RetrieveAllMembers() calls will never show indirect group members but your RetrieveGroups() call has direct_only set to False meaning that indirect members are being returned. If the user's you are testing for membership are indirect members then your test results are consistent with what I would expect to see.
Can you try setting direct_only to True? If that results in RetrieveGroups() no longer returning True, then we know the issue is that the user is an indirect member.

weird Twitter API behaviour

I know sure is it me or everyone, I have a following code
http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=barbara_volkwyn
http://api.twitter.com/1/statuses/user_timeline.xml?user_id=248623669
Apparently according to Twitter api, user with screen_name = "barbara_volkwyn" has the user id = 248623669, however, when I run the above API call I get totally different result, one thing that's even weirder is if I try to run the second API call, the users object contain in the returned result is not even the same user.
I wonder anyone has the same problem, feel free to give it a try.
Regards,
Andy.
your userID of barbara_volkwyn isn't valid. It should be: 264882189
You can fetch userID's trough the api or with https://tweeterid.com/
The user_ids reported by the Search API aren't the same as the user_ids used in the Twitter REST API -- unsure if that's where you found the user_id 248623669 or not though.
A timeline contains tweets which in turn contain embedded (but cached) user objects, usually reflecting the state of the user at the time the Tweet was published. Sometimes users change their screen_names, so a user by the name of #barbara_volkwyn might be user_id 1234 one day and user_id 5678 the next day, while the tweets that belonged to user_id 1234 will always belong to user_id 1234, regardless of the screen_name.
The user_id for #babara_volkwyn according to the REST API is 264882189. It's entirely possible that someone held the same screen name but a different user_id at another time. The only way to ever be certain about the identity of a Twitter user is to refer to them by their REST API user_id -- screen_names are transitory and can be modified by the end-user at any time.
As I mentioned, cached user objects used within statuses can become stale -- the most reliable source for up-to-date information about a single user account is the user/show API method. The most reliable source for up-to-date information on recent Tweets by an account is the statuses/user_timeline method.
The embedded objects work for most scenarios, but if you're looking for maximum accuracy, the distinct resources are best.
Thanks, Taylor.

Categories

Resources