Extract Json Data with Python - python

My Jason Data looks like this:
{
"componentId": "SD1:1100047938",
"componentType": "Device",
"name": "WR50MS15-7938 (WR 33)",
"product": "SB 5000TL",
"productTagId": 9037,
"pvPower": 886,
"serial": "1100047938",
"specWhOutToday": 3.0909803921568626,
"specWhOutYesterday": 2.924313725490196,
"state": 307,
"totWhOutToday": 15764,
"totWhOutYesterday": 14914
}
How could i only extract:
"state" to a separate file
"pv-power" to a sperate file ?
Thanks !

import requests
import json
import urllib3
import sys
import requests
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
fehler = '"state": 35'
urltoken = "https://172.16.63.100/api/v1/token"
urldaten = "https://172.16.63.100/api/v1/overview/Plant:1/devices?todayDate=2022-10-17T12%3A53%3A16.746Z"
urlaktuell = "https://172.16.63.100/api/v1/widgets/gauge/power?componentId=Plant%3A1&type=PvProduction"
data = {
"grant_type": "password",
"username": "...",
"password": "...",
}
response = requests.post(urltoken, data, verify=False)
#print(response.json())
data = json.loads(response.text)
with open('/usr/lib/nagios/plugins/check_DataManager/token.txt', 'w') as f:
data = json.dump(data, f, indent = 2)
with open('/usr/lib/nagios/plugins/check_DataManager/token.txt') as json_file:
data1 = json.load(json_file)
token = data1["access_token"]
payload={}
headers = {'Authorization': 'Bearer ' + token }
response = requests.request("GET", urldaten, headers=headers, data=payload, verify=False)
data = json.loads(response.text)
with open('/usr/lib/nagios/plugins/check_DataManager/info.txt', 'w') as f:
data = json.dump(data, f, indent = 2)
if fehler in open('/usr/lib/nagios/plugins/check_DataManager/info.txt').read():
print("Mindestens ein Wechselrichter hat einen Fehler!")
exit(1)
else:
print("Alle Wechselrichter Online!")
exit(0)
payload={}
headers = {'Authorization': 'Bearer ' + token }
response = requests.request("GET", urlaktuell, headers=headers, data=payload, verify=False)
aktuell = json.loads(response.text)
daten = aktuell["value"]
print("Aktuelle Leistung:", 0.001*daten , "KWh")
I now managed to do all i wanted like this :)
Well at first it ignores all Certificate Warnings i have been getting from my SMA Monitoring Device.
Then it gathers the Bearer Access token an stores it into an .txt file
After this is sends a request for json data to urldaten and urlaktuell. This Data is then stored in info.txt and ertrag.txt :) In this Files it checks if there is a faulty Inverter stored :)

url = "https://172.16.63.100/api/v1/overview/Plant:1/devices?todayDate=2022-10-17T12%3A53%3A16.746Z"
payload={}
headers = {
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NjYwMTU0MzMsInN1YiI6Ik1iYWNobCIsInVpZCI6Ijc1YmNkNTM2LTFhOTYtNDQ4My05MjQxLWZkNjY5Zjk3M2Y5OCIsImV4cCI6MTY2NjAxOTAzM30.bMMAsD8iPrAXDp7fbnwYL3Y8lj4Ktok3tU9NHZkYq8s'
}
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
#print(response.text)
data = json.loads(response.text)
with open('data.json', 'w') as f:
data = json.dump(data, f, indent = 2)
This is my Code for gathering the JSON Data.
I need to exctract the above mentioned Values.

After you run data = json.loads(response.text) , the json is loaded into your data variable as a python dictionary.
So state = data.state and pvPower = data.pvPower should give the info you're after.
I'm not exactly sure what you want to do with that information, as far as extracting to a separate file. But, json.dump() does output data to a json file.

Related

python dynamic value in api payload

Hey i have an api request and i need the value in payload to by dynamic, i tried with f'' but it wont make it dynamic
will appreciate your help.
import requests
url = "https://www.stie.com/api/conversations/KJQ-CZHNR-985/fields"
valuetarget = "123"
payload = {'apikey': 'zbyc88srdi333d3dpq5lye48pgg1tfo1pnjvj65ld',
'customfields': '[{"code": "orderno", "value":"valuetarget"}]'}
files = [
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
You just need to load json and dump it to string and send it.
import json
valuetarget = "123"
payload = {
'apikey': 'zbyc88srdi333d3dpq5lye48pgg1tfo1pnjvj65ld',
'customfields':
json.dumps([{"code": "orderno", "value":valuetarget}])
}
You should escape the curly braces in the formatted string, like so:
f'[{{"code": "orderno", "value":"{valuetarget}"}}]'
But why not let requests format the string for you?
import requests
url = "https://www.stie.com/api/conversations/KJQ-CZHNR-985/fields"
valuetarget = "123"
# 'customfields' is now a list of dictionaries
payload = {'apikey': 'zbyc88srdi333d3dpq5lye48pgg1tfo1pnjvj65ld',
'customfields': [{"code": "orderno", "value": valuetarget}]}
files = [
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)

Can't pass base 64 to post request python

enter image description here
Hi i am trying to pass base64 image to post api request but getting error please help i am new to programming
import requests
import json, base64
final_list = []
def vehicle_damage(img):
url = "https://vehicle-damage-assessment.p.rapidapi.com/run"
payload = {
"draw_result": True,
"image": img
}
headers = {
"content-type": "application/json",
"X-RapidAPI-Host": "vehicle-damage-assessment.p.rapidapi.com",
"X-RapidAPI-Key": "bd31338435msha00c21a34e0a18dp10bb9cjsn067429eb078f"
}
response = requests.request("POST", url, json=payload, headers=headers)
res = response
json_data = json.loads(response.text)
anoted_url = (json_data['output_url'])
for item in (json_data['output']['elements']):
damage_cat = (item['damage_category'])
damage_loc = (item['damage_location'])
damage_per = (item['score'])
damage_per = damage_per*100
final_list.append(damage_cat)
final_list.append(damage_loc)
final_list.append(damage_per)
image = open("C:\\Users\\286303\\Pictures\\Car Damaged\\Damaged\\052a5e6f08d199f88379964c4e4d6196--car-body-repairs-audi-a.jpg", 'rb')
image_read = image.read()
image_64_encode = base64.b64decode(image_read)
print(image_64_encode)
vehicle_damage(image_64_encode)
you need to change it like this:
def vehicle_damage(img: bytes):
...
payload = {"draw_result": True, "image": str(img)}
...
make 'img' as bytes in function's arguments and make it string when put it in your payload dict.

Trying to retrieve data from the Anbima API

I'm trying to automate a process in which i have to download some brazilian fund quotes from Anbima (Brazil regulator). I have been able to work around the first steps to retrieve the access token but i don't know how to use the token in order to make requests. Here is the tutorial website https://developers.anbima.com.br/en/como-acessar-nossas-apis/.
I have tried a lot of thing but all i get from the request is 'Could not find a required APP in the request, identified by HEADER client_id.'
If someone could share some light. Thank you in advance.
import requests
import base64
import json
requests.get("https://api.anbima.com.br/feed/fundos/v1/fundos")
ClientID = '2Xy1ey11****'
ClientSecret = 'faStF1Hc****'
codeString = ClientID + ":" + ClientSecret
codeStringBytes = codeString.encode('ascii')
base64CodeBytes = base64.b64encode(codeStringBytes)
base64CodeString = base64CodeBytes.decode('ascii')
url = "https://api.anbima.com.br/oauth/access-token"
headers = {
'content-type': 'application/json'
,'authorization': f'Basic {base64CodeString}'
}
body = {
"grant_type": "client_credentials"
}
r = requests.post(url=url, data=json.dumps(body), headers=headers, allow_redirects=True)
jsonDict = r.json()
##################
urlFundos = "https://api-sandbox.anbima.com.br/feed/precos-indices/v1/titulos-publicos/mercado-secundario-TPF"
token = jsonDict['access_token']
headers2 = {
'content-type': 'application/json'
,'authorization': f'Bearer {token}'
}
r2 = requests.get(url=urlFundos, headers=headers2)
r2.status_code
r2.text
I was having the same problem, but today I could advance. I believe you need to adjust some parameters in the header.
Follows the piece of code I developed.
from bs4 import BeautifulSoup
import requests
PRODUCTION_URL = 'https://api.anbima.com.br'
SANDBOX_URL = 'https://api-sandbox.anbima.com.br'
API_URL = '/feed/fundos/v1/fundos/'
CODIGO_FUNDO = '594733'
PRODUCTION = False
if PRODUCTION:
URL = PRODUCTION_URL
else:
URL = SANDBOX_URL
URL = URL + API_URL + CODIGO_FUNDO
HEADER = {'access_token': 'your token',
'client_id' : 'your client ID'}
html = requests.get(URL, headers=HEADER).content
soup = BeautifulSoup(html, 'html.parser')
print(soup.prettify())
The sandbox API will return a dummy JSON. To access the production API you will need to request access (I'm trying to do this just now).
url = 'https://api.anbima.com.br/oauth/access-token'
http = 'https://api-sandbox.anbima.com.br/feed/precos-indices/v1/titulos-publicos/pu-intradiario'
client_id = "oLRa*******"
client_secret = "6U2nefG*****"
client_credentials = "oLRa*******:6U2nefG*****"
client_credentials = client_credentials.encode('ascii')
senhabytes = base64.b64encode(client_credentials)
senha = base64.b64decode(senhabytes)
print(senhabytes, senha)
body = {
"grant_type": "client_credentials"
}
headers = {
'content-type': 'application/json',
'Authorization': 'Basic b0xSYTJFSUlOMWR*********************'
}
request = requests.post(url, headers=headers, json=body, allow_redirects=True)
informacoes = request.json()
token = informacoes['access_token']
headers2 = {
"content-type": "application/json",
"client_id": f"{client_id}",
"access_token": f"{token}"
}
titulos = requests.get(http, headers=headers2)
titulos = fundos.json()
I used your code as a model, then I've made some changes. I've printed the encode client_id:client_secret and then I've copied and pasted in the headers.
I've changed the data for json.

Get bugs from bugzilla using python

I am trying to get bugs to a db from bugzilla rest API. My code is given below.
import requests
import json
URL = "https://bugzilla.mozilla.org/rest/"
API_KEY = "key"
headers = {"Content-type": "application/json"}
params = {
"username": "email",
"password": "password",
"apikey": API_KEY,
}
# r = requests.get(URL + 'login/', headers = headers, params = params)
# print(r)
resp = requests.post(URL + "bug/" , headers = headers, params = params)
if resp.status_code != 200:
print('error: ' + str(resp.status_code))
else:
print('Success')
print(resp)
When I try this I get Response 404.
Someone please direct me to the correct path.
After poking aroung https://resttesttest.com/ I found the answer. Bugzilla API can be authenticate just by API-KEY. So I removed username and password from params dict. It seems I have an error in concatenating the URL too. I just used "https://bugzilla.mozilla.org/rest/bug/35" to get the bug report on bug_id 35. Then json.load(resp.text) gave the json object of the bug report. Final code looks like this.
import requests
import json
URL = "https://bugzilla.mozilla.org/rest/bug/35"
API_KEY = "key"
headers = {"Content-type": "application/json"}
params = {
"api_key": API_KEY,
}
resp = requests.get(URL , headers = headers, params = params)
if resp.status_code != 200:
print('error: ' + str(resp.status_code))
else:
print('Success')
print(json.loads(resp.text))

Using Python to Post json contact list to Qualtrics API, error with Content-Type

I'm trying to import contacts into a contact list in Qualtrics. I am using python to do this.
Token = 'MyToken' #when running the code I put in my actual token and id
ContactsID = 'MyContactsID'
data = open('contacts.json', 'rb')
headers = {'X-API-TOKEN': Token, 'Content-Type':'application/json',}
r = requests.post('https://az1.qualtrics.com/API/v3/mailinglists/' + ContactsID +'/contactimports', headers=headers, data=data)
r.text
This code gives me the following error: '{"meta":{"httpStatus":"400 - Bad Request","error":{"errorMessage":"Invalid Content-Type. expected=multipart/form-data found=application/json","errorCode":"RP_0.1"},"requestId":null}}'
I changed the content type to multipart/form-data that it says it is expecting and received the response "413", which qualtrics explains means "The request body was too large. This can also happen in cases where a multipart/form-data request is malformed."
I have tested my json and verified that it is valid. Also, I don't know why the request body would be too large because it's only 13 contacts that I'm trying to import. Any ideas?
With the help of Qualtrics Support, I was eventually able to get the following code to work:
Token = 'MyToken' #when running the code I put in my actual token and id
ContactsID = 'MyContactsID'
url = "https://az1.qualtrics.com/API/v3/mailinglists/" + ContactsID + "/contactimports/"
headers = {
'content-type': "multipart/form-data; boundary=---BOUNDRY",
'x-api-token': "Token"
}
files = {'contacts': ('contacts', open('contacts.json', 'rb'), 'application/json')}
request = requests.post(url, headers=headers, files=files)
print(request.text)
Please note that if you want to use this code, you will need to change "az1" in the URL to your own Qualtrics datacenter ID.
You need to use files = .. for a multipart request:
Token = 'MyToken' #when running the code I put in my actual token and id
ContactsID = 'MyContactsID'
data = open('contacts.json', 'rb')
headers = {'X-API-TOKEN': Token}
r = requests.post('https://az1.qualtrics.com/API/v3/mailinglists/' + ContactsID +'/contactimports',files={"file":data}, headers=headers)
r.text
Once you do requests will take care of the rest:
In [36]: url = 'http://httpbin.org/post'
In [37]: headers = {'X-API-TOKEN': "123456789"}
In [38]: files = {'file': open('a.csv', 'rb')}
In [39]: r = requests.post(url, files=files, headers=headers)
In [40]: print r.text
{
"args": {},
"data": "",
"files": {
"file": "a,b,c\n1,2,3"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "152",
"Content-Type": "multipart/form-data; boundary=3830dbe5fa6141f69d3d85dee4ba6e78",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.10.0",
"X-Api-Token": "123456789"
},
"json": null,
"origin": "51.171.98.185",
"url": "http://httpbin.org/post"
}
In [41]: print(r.request.body)
--3830dbe5fa6141f69d3d85dee4ba6e78
Content-Disposition: form-data; name="file"; filename="a.csv"
a,b,c
1,2,3
--3830dbe5fa6141f69d3d85dee4ba6e78--
looking at the docs, you actually want something closer to:
Token = 'MyToken' #when running the code I put in my actual token and id
ContactsID = 'MyContactsID'
data = open('contacts.json', 'rb')
files = {'file': ('contact', data ,'application/json', {'X-API-TOKEN': Token})}
r = requests.post('https://az1.qualtrics.com/API/v3/mailinglists/' + ContactsID +'/contactimports',files=files)

Categories

Resources