Can I access a users' description (bio) directly through Tweepy? - python-3.x

I just started tinkering with Tweepy to see the extent of what's possible, and I'm wondering if I can grab a user's description (the short bio on their profile, under the handle) directly. I know the description can be accessed by using a workaround like this:
api = tweepy.API(auth)
pastrytweet = api.search('pastries')
for twit in pastrytweet:
print(twit)
This gives us an output that is a list (not in the Python sense) of Status objects, each of which contains a ton of information, eg:
Status(_api=<tweepy.api.API object at 0x038F6EF8>, _json={'created_at': 'XXXXXXXXXXX', 'id': XXXXXXXXXXXX, 'id_str': 'XXXXXXXXXXX', 'text': 'RT #XXXXXXXXXXX XXXXXX pastries XXXXXX', 'description': 'XXXXXXXXX')
I'm trying to access this "description" parameter seen here at the end directly, but any attempt I make returns a "AttributeError: 'Status' object has no attribute 'description'".
So if I wanted, say, to search for a term and grab the bio of a user that published a tweet with that term for some reason, how would I proceed? Or even to search people's bios directly? Is it even possible? I read the Tweepy documentation and I didn't really find an answer.

Each status object return by api.search has an attribute user and under user you'll find the description. The following code should help:
api = tweepy.API(auth)
pastrytweet = api.search('pastries')
for twit in pastrytweet:
print(twit.user.description)

Related

Twitter Academic Research with japanese language

This is my first question. I would like to seek your advice on this matter.<(_ _)>
I got a Twitter Academic Research account. To get the tweets of a specific user, Using official sample code as a reference, I made the "param" as follows.
query_params = {'query': '(from:jtwfihT7bFRPzEx -is:retweet)','lang': 'ja', 'tweet.fields': 'author_id'}
But if I specify "lang" as an argument to the "query" parameter, I get the following error.
Exception: (400, '{"errors":[{"parameters":{"lang":["ja"]},"message":"The query parameter [lang] is not one of [query,start_time,end_time,since_id,until_id,max_results,next_token,expansions,tweet.fields,media.fields,poll.fields,place.fields,user.fields]"}],"title":"Invalid Request","detail":"One or more parameters to your request was invalid.","type":"https://api.twitter.com/2/problems/invalid-request"}')
Does Twitter Academic Research still not support Japanese?
Off topic, I was using Standard API version 1.1 to get tweets, but does Academic Account work with API v1.1?

How do I fetch data using user ID on StackAPI?

Is it possible to fetch data using StackAPI using UserID instead of "fromdate" or "questionID" etc?
I want to fetch the questions and tags used by a particular user.
What's the syntax for it.
I have tried
from stackapi import StackAPI
SITE = StackAPI('stackoverflow')
SITE._api_key = None
associated_users = SITE.fetch('/users/{}/associated'.format(10286273),
pagesize=1)
associated_users
But I don't know how to print data from it.
I don't see how "associated" is relevant to your stated goal.
You are interested in queries like:
tags = SITE.fetch('/users/{}/tags'.format(10286273), site='stackoverflow')
questions = SITE.fetch('/users/{}/questions'.format(10286273), site='stackoverflow')
which retrieve details on seven posts,
mentioning tags like python and chatbot.
cf https://api.stackexchange.com/docs/questions-by-ids

Can't seem to find the issue with the requestID parameter for the request header

I am trying to pull data from a REST API that uses a "similar standard to JSON RPC". The params I am passing look right according to the documentation here and here.
The error I am receiving is ...message:"Header missing request ID".... I am unsure what I am missing that would properly declare the requestID.
I have looked at the documentation provided via the API I am trying to pull data from but it's not very helpful considering it's all in PHP and cURL. I am trying to complete this task using python-requests.
getParams = {'method': 'getCustomers', 'params':{'where':'', 'limit': 2}, 'id': 'getCustomers'}
Result:
{"result":null,"error":{"code":102,"message":"Header missing request ID","data":[]},"id":null}
The return result should contain a list of All Customers and their attributes in JSON format.
Turns out there is nothing wrong with the code I am using. There is an issue with the API I am attempting to call.
In my situation, I was getting the same error back and was required to send a X-Request-ID header. I fixed it by adding the following to my headers:
headers = {
'X-Request-ID': str(uuid.uuid1()) # generate GUID based on host & time
...
Note that (for me) the GUID needed to be of a specific format (e.g. matching the Regex ^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$
taken from https://www.geeksforgeeks.org/how-to-validate-guid-globally-unique-identifier-using-regular-expression/). For example, it still gave the same error if I just sent "test".

Stripe: Getting Credit Card's Last 4 Digits

I have upgraded the Stripe.net to the latest version which is 20.3.0 and now I don't seem to find the .Last4 for the credit card. I had the following method:
public void CreateLocalCustomer(Stripe.Customer stipeCustomer)
{
var newCustomer = new Data.Models.Customer
{
Email = stipeCustomer.Email,
StripeCustomerId = stipeCustomer.Id,
CardLast4 = stipeCustomer.Sources.Data[0].Card.Last4
};
_dbService.Add(newCustomer);
_dbService.Save();
}
But now the stipeCustomer.Sources.Data[0].Card.Last4 says 'IPaymentSource' does not contain a definition for 'Card'. Does anyone know how I can get the card details now? The flow is that I create the customer by passing the Stripe token to Stripe, then I get the above stripeCustomer. So I expect it to be somewhere in that object. But I can't find it. The release notes can be found here.
Thank you.
In the old world of Stripe, there only used to be one type of payment method you could attach to a Customer; specifically, Card-objects. You would create a Card-object by using Stripe.js/v2 or the Create Token API Endpoint to first create a Token-object and then attach that token to a Customer-object with the Create Card API Endpoint.
Once Stripe expanded to support a number of other payment methods though, Stripe built support for a new object type that encapsulated a number of payment methods (including credit cards) called Source-objects. A Source-object is created either by using Stripe.js/v3 or the Create Source API Endpoint. It can also be attached to a Customer-object in much the same way as the Card-objects mentioned above, except they retain their object type. They're still a Source. You use the Attach Source API Endpoint to do this (that is notably identical to the Create Card API Endpoint mentioned above).
What I'm getting at here, is there are now two different object types (or more) that you can expect to see returned in the sources-array (or Sources in .NET). All of these methods though inherit from the IPaymentSource-interface. So if you know you have a Card-object getting returned, you can simply cast the returned object to the Card-class.
Something like this should get you going:
CardLast4 = ((Card) stipeCustomer.Sources.Data[0]).Last4
You can see what I mean by inheritance by looking at this line in the Card-class file:
https://github.com/stripe/stripe-dotnet/blob/master/src/Stripe.net/Entities/Cards/Card.cs#L7
Good luck!
As of Stripe.net.21.4.1, this is what works:
var chargeService = new ChargeService();
var charge = chargeService.Get(id);
CardLast4 = ((Card)charge.Source).Last4;
It's getting hard not to panic when code breaks because of all the micro-changes Stripe makes.
So after debugging, it looks like the Data[0] needs to be cast as Card to get the card.
So it will be CardLast4 = ((Card)stipeCustomer.Sources.Data[0]).Last4.

Using the Spotify iOS SDK, how can I tell what market the user is connected to?

Is it possible to tell the country associated with the current user's account with the iOS Spotify SDK? How? I have not been able to find anything in the docs.
(I'd like to get the ISO 3166-1 alpha-2 country code so that another (unauthenticated) user can do searches with the web api and only find tracks that we will ultimately be able to play using the authenticated user that is using the SDK.)
Found it. It is available from the territory field of the SPTUser object. Here is how you might get it after having logged in and received a session object, example code in Swift:
SPTUser.requestCurrentUserWithAccessToken(session.accessToken) { error, object in
guard let user = object as? SPTUser,
territory = user.territory else {
print("Could not get territory, error: \(error)"
return
}
print(territory)
}
However, you will get nil values for the territory unless you have included SPTAuthUserReadPrivateScope in requestedScopes when setting up your SPTAuth object.

Resources