I've been playing around the the Reddit API and I've come across this endpoint:
GET /api/user_data_by_account_ids
According the the documentation, the query parameter for this endpoint is a comma separated list of fullnames. Reddit defines a fullname as:
"a combination of a thing's type (e.g. Link) and its unique ID which forms a compact encoding of a globally unique ID on reddit. Fullnames start with the type prefix for the object's type, followed by the thing's unique ID in base 36. For example, t3_15bfi0."
So I'm wondering how I can determine a user's fullname? Is it listed somewhere in their profile? Or is there another endpoint in Reddit's api for getting a user's fullname?
When you send a query to /user/username/about, the base-36 ID is included as the value of id in the data property of the response. Concatenate this to the value of the kind property in the result (this should be 't2' for any user) with an underscore in between, and you'll have their fullname.
Related
I'm really stuck on this one.
I'm using Tweepy to get the IDs of all users that liked a specific tweet. I seem to get a list of "User" structures that contain "id", "name" and "username", but I'm not able to get only the "id".
The code is simple:
client = tweepy.Client(
bearer_token=bearer_token,
consumer_key=api_key, consumer_secret=api_secret,
access_token=user_token, access_token_secret=user_token_secret,
wait_on_rate_limit=True
)
for response in tweepy.Paginator(client.get_liking_users, id=tweetid, max_results=100, limit=10):
for item in response:
print("ITEM:\n", item)
if item is not None:
for user in item:
if user is not None:
print(user)
The print of "item" gets me this (simplified, of course; the number of structures is high, that's why I have to use Paginator):
[<User id=0000001 name=user1 username=UserName1>, <User id=0002 name=user2 username=UserName2>, <User id=000003 name=user3 username=UserName3>]
and the print of "user" just gets me the individual usernames: "UserName1", etc.
But no way to get user.id, user.User.id, nor anything similar. And I'm frustrated, because the information is right there, just I can't access it easily.
Thank you!
Tweepy documentation provides an example of something very similar to what you want to do: https://docs.tweepy.org/en/stable/examples.html -> API v2 -> Get Tweet’s Liking Users
import tweepy
bearer_token = ""
client = tweepy.Client(bearer_token)
# Get Tweet's Liking Users
# This endpoint/method allows you to get information about a Tweet’s liking
# users
tweet_id = 1460323737035677698
# By default, only the ID, name, and username fields of each user will be
# returned
# Additional fields can be retrieved using the user_fields parameter
response = client.get_liking_users(tweet_id, user_fields=["profile_image_url"])
for user in response.data:
print(user.username, user.profile_image_url)
This example prints the user's username and profile image URL, but note the comment says the id is also returned, so something like user.id should work. Otherwise, you can also add id to user_fields to make sure it's returned, although that shouldn't be necessary.
Unfortunately, I am not able to test it myself because I don't have a Twitter developer account with the required elevated access.
Edit: I got access to an API account with elevated access and I was able to test your code, see the update below
Iterating paginated results
The reason why you need a double for loop to iterate the paginated results and it eventually crashes after showing some results with an error saying you are trying to access a non-existent id attribute on an str object is because you are not iterating the Paginator results correctly.
For the sake of simplicity, I'm going to label your three nested for loops:
loop 0: for response in tweepy.Paginator(...
loop 1: for item in response
loop 2: for user in item
Paginator returns a Response object with all the results in the data attribute. The object has other attributes like meta, count, etc.
When you do loop 1, you are iterating all these data, count, etc., attributes of Response.
If the attribute you are iterating happens to be the data attribute, it will start loop 2 and it will iterate the results getting the output you expect.
But loop 1 will also iterate other Reponse items outside of the data attribute.
Let's see, for example, what happens when loop 1 enters the meta attribute.
meta is a dictionary that looks like this:
meta={'result_count': 80, 'next_token': '676f9b7bumw8i3jbm4nnifamw2ejjaktp8kjym6akdak9'}
When you enter loop 2 with the meta attribute, it will start iterating the keys (not the values, because that's how dicts work in Python) so the value of user in loop 2 will be either result_count or next_token. And it's then when you are getting your error saying you are trying to access id on a str.
What you should be doing is iterating the response.data in loop 1 instead and that will also allow removing the need of a second loop:
for response in tweepy.Paginator(client.get_liking_users, id=tweetid, max_results=100, limit=10):
for user in response.data:
print(user.id)
Edit: grammar and style
I am trying to use rasgo.get.dataset(fqtn='vw_orders_main') but I am getting an error.
APIError: Dataset with fqtn 'vw_orders_main' does not exist or this API key does not have access.
When using rasgo.get.dataset(), you can either:
pass in a dataset_id
rasgo.get.dataset(123)
pass in a fully qualified table name (fqtn)
rasgo.get.dataset(fqtn="DB.SCHEMA.TABLE")
pass in a resource_key
rasgo.get.dataset(resource_key='mykey')
From the appearance of the string you are using, I believe that is a resource key.
If you are using a variable called vw_orders_main to hold the FQTN string, then try it without the single quotes.
Examples:
vw_orders_main = "DB.SCHEMA.TABLE"
rasgo.get.dataset(fqtn=vw_orders_main)
or
rasgo.get.dataset(fqtn="DB.SCHEMA.TABLE")
or, if what you meant was resource_key,
rasgo.get.dataset(resource_key='vw_orders_main')
A resource_key is randomly assigned when a dataset is published, unless you specify the string yourself (like it appears that you did). It provides you the ability to tie multiple datasets as 1, thus allowing “versions”.
Resource Links: get dataset, publish dataset
Basically I want to get the conversation_id if the Tweet is a reply to another Tweet. So I can get the list of replies to each other to analyze.
My code:
class Listener(StreamingClient):
def on_response(self, response):
print(response)
listener = Listener(auth['bearer_token'])
listener.sample(expansions=['in_reply_to_user_id'], tweet_fields=['conversation_id'])
When using this, I only get the user_id to which it is replying, but I cannot get any type of conversation_id.
I have a slight feeling I am missing something essential.
From the relevant FAQ section about this in Tweepy's documentation:
If you are simply printing the objects and looking at that output, the string representations of API v2 models/objects only include the default fields that are guaranteed to exist.
The objects themselves still include the relevant data, which you can access as attributes or by subscription.
I am using simple_salesforce and get all records of a custom object called "SER__Condition__c". I know for a fact that that is the name because I got a list of table names from our administrator.
"api" is an instance of "simple_salesforce.Salesforce".
This is the command I'm executing:
pprint(api.query('SELECT Id FROM SER__Condition__c'))
Which returns this error:
File "path\to\lib\simple_salesforce\api.py", line 698, in _exception_handler
raise exc_cls(result.url, result.status_code, name, response_content)
simple_salesforce.api.SalesforceMalformedRequest: Malformed request https://xxx.salesforce.com/services/data/v29.0/query/?q=SELECT+Id+FROM+SER__Condition__c. Response content: [{'message': "\nSELECT Id FROM SER__Condition__c\n
^\nERROR at Row:1:Column:16\nsObject type 'SER__Condition__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.", 'errorCode': 'INVALID_TYPE'}]
Using the exact same command for a default object returns all the records as expected:
pprint(api.query('SELECT Id FROM Account'))
The same also holds true for these two:
api.Account.get('xxxxxxxxxxxxxxxxxx')
api.SER__Condition__c.get('xxxxxxxxxxxxxxxx')
It probably is a permissions issue. Make sure the SER__Condition__c object is visible to the user you are running the query as.
I am 90% sure the issue is with the name of the object. Per Salesforce, the naming convention for a custom object cannot include two consecutive underscores. From the Salesforce error message on object creation: "Error: The Object Name field can only contain underscores and alphanumeric characters. It must be unique, begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores."
If you change "SER__Condition__c" to "SER_Condition__c" (a single underscore between "SER" and "Contition") it should fix the issue.
Yes, I resolved this issue by editing the user profile's custom object permissions. It looks like it defaults to none.
Instead of using get_by_id() method for getting the id of a specific entry and print the content of this entry from the google datastore, i am trying to get the name of the url and print the content. For example:
print all the content that have this specific name(may have more than one rows of content with this name)
print the content of the specific id
i am using get_by_id(long(id)) to get the id in the second part of my example, and its working. I am trying to use get_by_key_name(name) but it does not working. any ideas on that? thank you.
sorry, but since i couldn't leave a comment, i am editing my question. Basically, since now i can get all the name of animals from my datastore and i have made them clickable using an html code in template file. In the datastore, there are entries with the same name of animal more than one times (e.g. name= duck, content= water and name=duck, content=lake). Now, when i am clicking into every name of animals(i have use the DINSTINCT in my gql query to print redundant elements(e.g. duck) only one time).Since the name=duck has two contents, when i am clicking on the name of the duck i want to see both of the contents. My problem is if i am using get_by_id(long(id)) i get the unique id of every element. But this will not print me both of the content of the name=duck because every entry has a unique id. But i want all the content of the entries with the same name. I am trying the following but it does not working.
msg = MODEL.Animals.get_by_key_name(name)
self.response.write("%s" % msg.content)
With get_by_id() you can get entity only if you know this ID. This operations named "Small operations" in quota and they are cheaper than datastore reads, but to get list of entities filtered by indexed property - you should use filters.
query = MODEL.Animals.query()
query = query.filter(MODEL.Animals.name == 'duck')
ducks = query.fetch(limit=100) # limit number of returned animals
for duck in ducks:
self.response.write('%s - %s' % (duck.name, duck.content))
By default, all string properties are indexed, so you will be able to do such requests.