How to fetch tweets of a given user? - python

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)

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.

Is there a way to search for an edx course using their API?

I have an edX client ID and secret ID, and I'm able to get the list of all edX online courses, but I want to be able to search through all of the edX courses with a query string like "robotics". (Unlike this question, I already understand how to get the list of courses, so this is not a duplicate question...) I tried
import requests
# I also tried with search_query instead of search
edx_course_search_response = requests.get('https://api.edx.org/catalog/v1/catalogs/'+edx_course_catalog_id+'/courses/'+"?search=robotics", headers=headers)
I know the catalog id and headers information is correct because I can get the list of edX courses. Unfortunately, this does not work and instead returns the first 20 online courses in edX's database as usual.
If I can't, I think I'm going to use whoosh.
I don't think so.
query is a response value from this endpoint: GET /catalog/v1/catalogs/{id}/. The endpoint for retrieving info about a specific catalog. It is not a user passed parameter. The user parameter is catalog id.
You might use this endpoint: /catalog/v1/catalogs/{id}/courses/
That endpoint, as mentioned in comments has an answer covering pagination here. You could then filter the returned dataset with python according to your query term. The results array in response values from that endpoint has fields such as short description/long description/title which might be useful to target.

'Wrong' profile ID being returned from Google Analytics python API

I am attempting to set up some code to analyze data from my Google Analytics account via the python API but have run into a problem with the profile_id that is being returned.
I am following the tutorial here. What I have done so far is to create I created a service account which provided an address of the form <projectId>-<uniqueId>#developer.gserviceaccount.com - this was then added to the Google Analytics account that I want to work with.
I then followed the tutorial, and the code below succeeds and returns a profile ID. The problem is that this profile ID isn't the same one that I can find by going to the dashboard for my Google Analytics account and looking by hand.
If I use the string that is returned from the function below to query the API then I get just empty results (ie the code succeeded in querying some metric like users in the past 7 days, but it just returns that the answer is zero).
If instead I just hard code the string I find on the dashboard then everything works fine and the correct results are returned. This obviously isn't very satisfying though and I'd like to understand what I'm doing wrong by trying to extract the profile ID via the python code.
accounts = service.management().accounts().list().execute()
if accounts.get('items'):
firstAccountId = accounts.get('items')[0].get('id')
webproperties = service.management().webproperties().list(
accountId=firstAccountId).execute()
if webproperties.get('items'):
firstWebpropertyId = webproperties.get('items')[0].get('id')
profiles = service.management().profiles().list(
accountId=firstAccountId,
webPropertyId=firstWebpropertyId).execute()
if profiles.get('items'):
return profiles.get('items')[0].get('id')
return None
In case anyone comes across this problem looking for the solution, it turns out that in my case there had been several profiles created and attached to the account I was querying. By taking the first profile I was picking up a now redundant profile.

tweepy get list of failed lookup_users

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.

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