api.get_user with Tweepy will not give description - python

I'm trying to use Tweepy (version 4.4.0) to get a user's description but it's seemingly not working:
u = api.get_user(username='XXXX', user_fields=['description'])
but the output of this is simply:
Response(data=<User id=123 name=XXX username=XXX>, includes={}, errors=[], meta={})
So it's getting me the name and id fine, but it's returning an empty for any user fields.
Note I've also tried with user_auth: 1, but I get 'Unauthorized: 401' - but from what I've seen around, I don't think user authentication is the problem here... but maybe it is?
Any advice would be great!

api seems to be an instance of tweepy.Client here.
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.
The user object being returned in the response should have a description field with the user's description.

You can access the description with: u.data.description
Keep in mind that the description may be blank some times, try with created_at to be sure it works.

Sample code for extracting description of any twitter user is as follows
import tweepy
auth = tweepy.OAuth2BearerHandler(os.environ.get("TWITTER_API_KEY"))
api = tweepy.API(auth)
user = api.get_user(screen_name="TechWiser", include_entities=False)
description = user._json['description']
user._json contain many other key, value pair that you can explore

Related

Getting SalesforceMalformedRequest: Malformed request error

I am trying to execute this following code to push data to Salesforce using the simple_salesforce python library :
from simple_salesforce import Salesforce
staging_df = hive.execute("select * from hdmni")
staging_df = staging_df.toPandas()
# # staging_df['birth_date']= staging_df['birth_date'].dt.date
staging_df['birth_date'] = staging_df['birth_date'].astype(str)
staging_df['encounter_start_date'] = staging_df['encounter_start_date'].astype(str)
staging_df['encounter_end_date'] = staging_df['encounter_end_date'].astype(str)
bulk_data = []
for row in staging_df.itertuples():
d= row._asdict()
del d['Index']
bulk_data.append(d)
sf = Salesforce(password='', username='', security_token='')
sf.bulk.Delivery_Detail__c.insert(bulk_data)
I am getting this error while trying to send dictionary to salesforce :
SalesforceMalformedRequest: Malformed request
https://subhotutorial-dev-ed.my.salesforce.com/services/async/38.0/job/7500o00000HtWP6AAN/batch/7510o00000Q15TnAAJ/result.
Response content: {'exceptionCode': 'InvalidBatch',
'exceptionMessage': 'Records not processed'}
There's something about your query that is not correct. While I don't know your use case, by reading this line, you can tell that you are attempting to insert into a custom object/entity in Salesforce:
sf.bulk.Delivery_Detail__c.insert(bulk_data)
The reason you can tell is because of the __c suffix, which gets appended onto custom objects and fields (that's two underscores, by the way).
Since you're inserting into a custom object, your fields would have to be custom, too. And note, you've not appended that suffix onto them.
Note: Every custom object/entity in Salesforce does come with a few standard fields to support system features like record key (Id), record name (Name), audit fields (CreatedById, CreatedDate, etc.). These wouldn't have a suffix. But none of the fields you reference are any of these standard system fields...so the __c suffix would be expected.
I suspect that what Salesforce is expecting in your insert operation are field names like this:
Birth_Date__c
Encounter_Start_Date__c
Encounter_End_Date__c
These are referred to as the API name for both objects and fields, and anytime code interacts with them (whether via integration, or on code that executes directly on the Salesforce platform) you need to make certain you're using this API name.
Incidentally, you can retrieve this API name through a number of ways. Probably easiest is to log into your Salesforce org, and in Setup > Object Manager > [some object] > Fields and Relationships you can view details of each field, including the API name. Here's a screen shot.
You can also use SObject describe APIs, either in native Apex code, or via integration and either the REST or SOAP APIs. Here's part of the response from the describe API request to the describe REST endpoint for the same object as my UI example above, found here at https://[domain]/services/data/v47.0/sobjects/Expense__c/describe:
Looking at the docs for the simple-salesforce python library you're using, they've surfaced the describe API. You can find some info under Other Options. You would invoke it as sf.SObject.describe where "SObject" is the actual object you want to find the information about. For instance, in your case you would use:
sf.Delivery_Detail__c.describe()
As a good first troubleshooting step when interacting with a Salesforce object, I'd always recommend double-checking correctly referencing an API name. I can't tell you how many times I've bumped into little things like adding or missing an underscore. Especially with the __c suffix.

How to query specific information for an issue?

I'm trying to use JIRA python api to receive the list of tickets that have been raised in the last 30 days but whenever i run
Issue = main.jira.issue("PLAT-38592")
i = main.jira.issue(issue, "Summary")
print(i)
All that gets returned is
PLAT-38592
Then i try to poke at the issue
Issue = main.jira.issue("PLAT-38592")
print (Issue)
And all that gets returned is
PLAT-38592
I need to be able receive information from this ticket but it only returns a string
Issues are objects. You can access their content by accessing fields.
If you for example want to access the summary, you can use (according to the docs):
issue = jira.issue('PLAT-38592', fields='summary')
summary = issue.fields.summary
print(summary)

Unable to access jira worklogs via python-jira

I am trying to access the worklogs in python by using the jira python library. I am doing the following:
issues = jira.search_issues("key=MYTICKET-1")
print(issues[0].fields.worklogs)
issue = jira.search_issues("MYTICKET-1")
print(issue.fields.worklogs)
as described in the documentation, chapter 2.1.4. However, I get the following error (for both cases):
AttributeError: type object 'PropertyHolder' has no attribute 'worklogs'
Is there something I am doing wrong? Is the documentation outdated? How to access worklogs (or other fields, like comments etc)? And what is a PropertyHolder? How to access it (its not described in the documentation!)?
This is because it seems jira.JIRA.search_issues doesn't fetch all "builtin" fields, like worklog, by default (although documentation only uses vague term "fields - [...] Default is to include all fields"
- "all" out of what?).
You either have to use jira.JIRA.issue:
client = jira.JIRA(...)
issue = client.issue("MYTICKET-1")
or explicitly list fields which you want to fetch in jira.JIRA.search_issues:
client = jira.JIRA(...)
issue = client.search_issues("key=MYTICKET-1", fields=[..., 'worklog'])[0]
Also be aware that this way you will get at most 20 worklog items attached to your JIRA issue instance. If you need all of them you should use jira.JIRA.worklogs:
client = jira.JIRA(...)
issue = client.issue("MYTICKET-1")
worklog = issue.fields.worklog
all_worklogs = client.worklogs(issue) if worklog.total > 20 else worklog.worklogs
This question here is similar to yours and someone has posted a work around.
There is a also a similar question on Github in relation to attachments (not worklogs). The last answer in the comments has workaround that might assist.

tweepy get_user does not work with a list of user_ids

I am using tweepy library to pull informations about Twitter users. What I want is, given a list of Twitter ids, get the number of followers of each user that relates to the ids. The function looks like this.
infos = api.get_user(user_id=xxx)
return infos.followers_count
In the Twitter documentation, it says I can input a list of up to 100 user ids to get_user. Thing is, whatever I pass to that function other than a single id, I get the error
[{'message': 'Sorry, that page does not exist', 'code': 34}]
For example if I write:
infos = api.get_user(user_id=[user1,user2,user3])
I get the error. But if I write
infos = api.get_user(user_id=user1)
It works perfectly well. Did you encounter this problem before? Is the problem in Tweepy library? Should I use another library?
Thanks for your support
You need to use the function lookup_users for this, and the parameter is user_ids:
infos = api.lookup_users(user_ids=[user1,user2,user3])
https://github.com/tweepy/tweepy/blob/master/tweepy/api.py#L154

python-twitter retrieving all followers

I'm using the python-twitter library and while I can get the code to retrieve a 'set' of results, apparently in the first 'page' ... I for the life of me can't find the right code to get a set of ALL the followers for a specific account/handle.
I'm using this snip...
target = sys.argv[1]
#returns a twitter.User instance for each follower
users = api.GetFollowers(target)
print [u.screen_name for u in users]
I'd appreciate any pointers, if someone's figured this out already?
Thanks in advance.
/Raf
According to this patch, I guess it has been fixed. Check this sample code.

Categories