How to pass a variable into a curl converted python command - python

The data option is converted from curl to python below:
data = '{"query": "query {publisherNetworkReport( sellerMemberId: \\"5241\\", startDate: \\"2022-12-01\\", endDate:\\"2022-12-02\\",dimensions:[YMD,PUBLISHER_NAME,DOMAIN],metrics: [IMPRESSIONS,CLICKS,REVENUE] ) { rows {dimensions {name value} metrics { ... on MetricLong { name long: value } ... on MetricDecimal {name decimal: value}}}nextCursor totalRows}}","variables":{}}'
How can I make the sellerMemberId, startDate and endDate as variables

Related

Create dictionary key and value pair from nested JSON values

I am attempting to create a dictionary by looping through a weather JSON file.
I want the key to be date and the value to be temperature from the nested "hours" in the JSON.
combined_temperature_hour = {date, temperature}
As I loop through the JSON file I can get the individual items but I can't figure out how to grab the two values and put them together in a (key, value) pair.
combined_temperature_hour = {}
for d in days:
for hours in d["hours"]:
hourly = hours.get('datetimeEpoch')
dia_y_hora = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(hourly))
complete_date.append(dia_y_hora)
h_temp = hours.get('temp')
hourly_temperature.append(h_temp)
#combined
combined = {complete_date, hourly_temperature}
print(combined)
JSON
"days":[
{
"datetime":"2021-01-01",
"datetimeEpoch":1609484400,
"tempmax":36.9,
"tempmin":32.4,
"temp":34.7,
"hours":[
{
"datetime":"00:00:00",
"datetimeEpoch":1609484400,
"temp":32.9,
},
{
"datetime":"01:00:00",
"datetimeEpoch":1609488000,
"temp":33.2,
},
{
"datetime":"02:00:00",
"datetimeEpoch":1609491600,
"temp":33.2,
}
]
}
This is what I would like the dictionary to look like after the loop
combined_temperature_hour = {`2021-01-24 05:00:00`: '25.1', `2021-01-24 06:00:00`: '26.2'}
I have tried grabbing the list for temperature and hour and combining into a dictionary them after the loop but I noticed that I am missing values and they are probably incorrect pairs.
Read your json as a dictionary first into a variable a
(I changed some of the mismatching brackets in your json)
a = {"days":[
{
"datetime":"2021-01-01",
"datetimeEpoch":1609484400,
"tempmax":36.9,
"tempmin":32.4,
"temp":34.7,
"hours":[
{
"datetime":"00:00:00",
"datetimeEpoch":1609484400,
"temp":32.9,
},
{
"datetime":"01:00:00",
"datetimeEpoch":1609488000,
"temp":33.2,
},
{
"datetime":"02:00:00",
"datetimeEpoch":1609491600,
"temp":33.2,
}
]
}
]
}
Then you can have the expected output from the following code:
combined_temperature_hour = {}
for date in a['days']:
for hour in date['hours']:
combined_temperature_hour[date['datetime']+' '+hour['datetime']] = hour['temp']

Filter data by day range in Elasticsearch using Python DSL

I wrote this below method to filter data for last 8 days
def method_one(query) -> Query:
gte = (datetime.datetime.now() - datetime.timedelta(days=query)).date()
lt = (datetime.datetime.now()).date()
print(gte, lt)
return Q(MultiMatch(
query=filter("range", {"lastModifiedDate": {"gte": gte, "lt": lt}}
),
fields=['lastModifiedDate']
))
I want to filter data based on the lastModifiedDate field by forming an Elasticsearch Query object in Python.
For example if I give /lastModifiedDate=8 (Rest API Call), it should return data by filtering for the last 8 days.
You don't need the datetime module to construct date queries in Elasticsearch -- you can use built-in date math:
from json import dumps
from elasticsearch_dsl.search import Search
from elasticsearch_dsl.query import Q, MultiMatch
def date_range_query(num_of_days):
if not isinstance(num_of_days, int):
raise Exception(
'expected numeric & positive `num_of_days`, got `%s`' % str(num_of_days))
return Q(
"range",
lastModifiedDate={
"gte": "now-%dd" % num_of_days,
"lt": "now"
}
)
try:
q = date_range_query(8)
print(dumps(q.to_dict(), indent=2))
except Exception as e:
print(e)
which'd print
{
"range": {
"lastModifiedDate": {
"gte": "now-8d",
"lt": "now"
}
}
}
Alternatively, if you insisted on using datetime.date objects, you'd need to stringify the dates first. Now, when you do that with str(...), you're in fact calling .__str()__ which then calls .isoformat() and returns a string formatted as YYYY-MM-DD.
Now, your lastModifiedDate field's mapping may have a different format. As such, it's good practice to declare the format of your range query:
gte = (datetime.datetime.now() - datetime.timedelta(days=num_of_days)).date()
lt = (datetime.datetime.now()).date()
return Q(
"range",
lastModifiedDate={
"gte": str(gte),
"lt": str(lt),
"format": "yyyy-MM-dd" # keep in mind that the format in ES conforms to Java syntax, not python
}
)
which'd produce a similar query but with concrete, spelled-out dates:
{
"range": {
"lastModifiedDate": {
"gte": "2021-02-26",
"lt": "2021-03-06",
"format": "yyyy-MM-dd"
}
}
}

Python, JSON: Form new JSON dict having id & timestamp & retrieve the id based on the latest timestamp value from the new JSON dictionary

JSON output:
"machines": {
"machines-xyz-123": {
"vm": {
"id": "compute1234",
"createTimestamp": "2020-11-15T14:44:02.272908941Z",
"status": "running"
}
"machines-tyu-567": {
"vm": {
"id": "compute23456",
"createTimestamp": "2020-11-15T18:18:22.412021105Z",
"status": "running"
}
}
}
Machine ID changes dynamically each time, I am able to get the VM ID and timestamp of each machine with below code:
machine_list = json_output[machines]
key_list = list(machines.keys())
count = len(machine_list)
for i in range (count):
id = json_output['machines'][key_list[i]]['vm']['id']
date = json_output['machines'][key_list[i]]['vm']['creationTimestamp']
I need to do 2 operations from the JSON output further:
1.) Need to form a new JSON dictionary with 'id' value as key and
'timestamp' value as value of the id key
2.) Need to retrieve the id based on the latest timestamp value from
the JSON dictionary
Any help will be appreciated.
not sure i understand your question
import json
my_dict = {}
my_dict[id] = timestamp
my_json = json.dumps(my_dict) #to create a json and save it as a file
out_file = open(my_path,"w")
out_file.write(my_json)
out_file.close()
#to compare between dates see https://stackoverflow.com/questions/8142364/how-to-compare-two-dates
max_date = 0
max_id = 0
for id in my_dict:
if my_dict[id]>max_date: # use date compare method
max_date = my_dict[id]
max_id = id

How to get json data by name using python

I'm kinda new JSON and python and i wish to use the keys and values of JSON to compare it.
I'm getting the JSON from a webpage using requests lib.
Recently, I've done this:
import requests;
URL = 'https://.../update.php';
PARAMS = { 'platform':'0', 'authcode':'code', 'app':'60' };
request = requests.get( url=URL, params=PARAMS );
data = request.json( );
I used this loop to get the keys and values from that json:
for key, value in data.items( ):
print( key, value );
it return JSON part like this:
rescode 0
desc success
config {
"app":"14",
"os":"2",
"version":"6458",
"lang":"zh-CN",
"minimum":"5",
"versionName":"3.16.0-6458",
"md5":"",
"explain":"",
"DiffUpddate":[ ]
}
But in Firefox using pretty print i get different result look like this:
{
"rescode": 0,
"desc": "success",
"config": "{
\n\t\"app\":\"14\",
\n\t\"os\":\"2\",
\n\t\"version\":\"6458\",
\n\t\"lang\":\"zh-CN\",
\n\t\"minimum\":\"5\",
\n\t\"versionName\":\"3.16.0-6458\",
\n\t\"md5\":\"\",
\n\t\"explain\":\"\",
\n\t\"DiffUpddate\":[\n\t\t\n\t]\n
}"
}
What I'm planing to do is:
if data['config']['version'] == '6458':
print('TRUE!');
But everytime i get this error:
TypeError: string indices must be integers
You need to parse the config
json.loads(data['config'])['version']
Or edit the PHP to return an associative array rather than a string for the config object

Sqlite3 Db to Json, used for Highcharts?

I'm currently working with a database and i would like to display its values on a webpage, using highcharts.
Here is what i use to fetch the data in the web app :
#app.route("/data.json")
def data():
connection = sqlite3.connect("/home/pi/database/Main_Database.db")
cursor = connection.cursor()
cursor.execute("SELECT epochTime, data_x from table")
results = cursor.fetchall()
return json.dumps(results)
Then i currently get this value by doing this in my html:
$.getJSON('http://192.168.1.xx/data.json', function (data) {
// Create the chart
$('#container').highcharts('StockChart', {
rangeSelector : {
selected : 1
},
title : {
text : 'title'
},
series : [{
name : 'Value',
data : data,
tooltip: {
valueDecimals: 2
}, .......
This works if i want to display only one data array.
If i want to display more than one array, then it looks like each arrays must be preceded by its name respecting a certain parsing (i checked on the data sample used by highcharts).
Example:
data1:[(epochTime, 200),(epochTime,400)];data2:[(epochTime, 2),(epochTime,4)]
I have some trouble to json.dumps two arrays from two different tables for example. I tried to use this following command : json.dumps({data1:results}).
But the results is still not readable.
Do you have any advice ? Or example/templates of webapp/highcharts using sqlite?
Thanks a lot !
I think this should work:
In the controller:
Fetch 2 results and put them in a dictionary.
#app.route("/data.json")
def data():
connection = sqlite3.connect("/home/pi/database/Main_Database.db")
cursor = connection.cursor()
cursor.execute("SELECT epochTime, data_x from table")
results1 = cursor.fetchall()
cursor.execute("SELECT epochTime, data_x from table2")
results2 = cursor.fetchall()
return json.dumps({'result1': results1,
'result2': results2})
On the page:
$.getJSON('http://192.168.1.xx/data.json', function (data) {
// Create the chart
$('#container').highcharts('StockChart', {
rangeSelector : {
selected : 1
},
title : {
text : 'title'
},
series : [{
name : 'Value1',
data : data.result1,//read result1
tooltip: {
valueDecimals: 2
},
{
name : 'Value2',
data : data.result2,//read result2
tooltip: {
valueDecimals: 2
}, .......

Categories