Iterating through a class to retrieve json response - Synapse/Django - python

I have a django project and I am trying to integrate SynapsePay Api. I sent a request that returned a class as a response. I am trying to figure out how to cycle or parse the response to get to the json so that I can grab certain values from the response that was returned. I have looked everywhere and cant seem to find a solution or a way to get into a class objects and reach the json part of the return.
Here is the response that I am getting...
I want to grab the _id from both of the objects returned in the object response below. Does anyone know how I can do this??
[
<class 'synapse_pay_rest.models.nodes.ach_us_node.AchUsNode'>( {
'user':"<class 'synapse_pay_rest.models.users.user.User'>(id=...49c04e1)",
'json':{}
} )
]

It would help if you linked the library you are using, which I'm guessing is
https://github.com/SynapseFI/SynapseFI-Python
In this case, there really is no need to grab the JSON - the client library has already wrapped the returned JSON in easy to use objects. The class instances (not classes) it returns already have an id attribute which is what you want. You can also get the raw JSON from the json attribute (based on my reading of the source code.
I'd recommend installing IPython (pip install ipython) and using the repl to execute commands and play around with the response objects. You will quickly be able to use tab completion to find what attributes are available.

Related

Using Python - convert string into JSON only instead of - dict of dict?

I am creating a watcher for my Videos section in youtube which will keep monitoring the stats of latest video uploaded. I am not using Selenium because it will keep the browser engaged and interrupt. But using requests_html to load the /videos page and return me the stats like how many view are now and send me a message on Telegram app.
So when I did requests_html call i retrieve a small json which has all the videos & stats but the command json.load() actually coverts it into dict of dict. The intention is to use the result of json.load() as JSON so that I can perform json parsing.
Attached the snapshot of JSON structure and highlighted the desired keys
I couldnt find any better example that tell me how to convert it into just JSON instead of dict of dict. because there are multiple levels approx 20+ to retrieve the desired key value. I have seen very simple example that do the following, but it is not possible to write those many nodes in the below statement.
aKeyValue = dictName['parentnode']['childNode']
Secondly if it is just JSON then I believe jsonpath_ng and its parse methods can be used to retrieve a desired key with having to provide the complete path from root. If this is tnot the right way, please suggest any other Py Module.
the recursive functions didnt work properly. I tried may be 10+ different functions, none worked on the JSON. Finally I found jsonpath-ng to work after I read thru its entire documentation
It was pretty simple, I dont know why I didnt figure this out earlier.
json.loads(jsonString)
parse($..gridRenderer.items[0].gridVideoRenderer.viewCountText.simpleText)
But at the same time the statement below doesnt work. Although statement below wasnt required, but i just tried. It worked on jsonpath evaluator online but not in the python code.
$..gridRenderer.items[0].gridVideoRenderer.viewCountText[?(simpleText="5927 views")]
If someone could point out why the above statement wouldnt work in python code ?

Is there a way to get the response after saving data in Firestore?

I'm using python to add data in firestore and I would want to get a response from firestore so that I can know if the data was successfully added or not. Is there a way to do this using Python? Newbie here.
I've already searched from other resources but can't find anything.
According to the Firestore documentation in the code, the DocumentReference.set() method returns a:
google.cloud.firestore_v1.types.WriteResult: The write result corresponding to the committed document. A write result contains an update_time field.
I can't find any Python examples, but it should work similarly to the Node.js version here: https://cloud.google.com/nodejs/docs/reference/firestore/1.2.x/WriteResult

Parse JSON within Python

I just wanted to ask what can I do to solve this issue I have.
Essentially I am making a stock checker for sneakers from Adidas, I know the endpoint to obtain the stock but the JSON data given back to me whilst readable and contains what I need also contains a bunch of other information that is unnecessary to what I am trying to do.
Example of a link to an endpoint:
http://production.store.adidasgroup.demandware.net/s/adidas-GB/dw/shop/v16_9/products/(BZ0221)?client_id=c1f3632f-6d3a-43f4-9987-9de920731dcb&expand=availability,variations,prices
This is a link to the JSON containing the stock of the shoe, price and availability. However, if you try to open it you'll see that it responds a bunch of useless info such as the description of the shoe and the price which I do not need.
A github repository that I was using to try and get to grips with the requests I am trying to make is:
https://github.com/yzyio/adidas-stock-checker/blob/master/assets/index.js
I can get it to give me the JSON response I am just trying to strip what I don't need and keep what I do need which I am finding very difficult especially in python.
Many Thanks!
Since you've said you can get a JSON response from the server than the first think you need to do is tell python to load it as JSON.
import json
data = json.loads(response_from_server)
After doing this you can now access the values in your JSON object the way you would access them via a Python dict.
data["artist"]["id"]

Detecting NSFW submissions with praw

I'm using PRAW to scrape for some content from reddit. I can get info on a submission (praw.objects.Submission), but I don't see from the documentation how to tell if the post is flagged as NSFW or not. Is it possible to figure this out through PRAW or should I use another api wrapper?
You may figure it out through PRAW by retrieving a submission object, and then applying over_18 to the object itself (as #Kevin suggested).
Here's an example:
if submission.over_18:
...
else:
...
And for future references, by using "dir(object)," you'll be able to see both attributes and methods that pertain to the Reddit API (which you may use to test and see all properties that effect the given object being tested). You can ignore everything that starts with an underscore (most likely).
Or you can go straight to source where PRAW is getting its data. The variable names are not set by PRAW, they come from this JSON (linked above).

Imgur TagVote issue python

A beginner here, I am using the imgur python library to get tags related to an image. For this I am using the gallery_item_tags method as mentioned here.
However whenever i call the method it gives me an output as shown here.
I have followed the authorization procedure using the needed client id and client secret and i can run all methods not involving TagVotes array. How can i get the required information from this?
You're getting a list of TagVote instances. You probably want the name, you can access it like this:
for tag in tags:
print tag.name

Categories

Resources