-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument_update.py
executable file
·47 lines (39 loc) · 1.35 KB
/
document_update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python3
import pandas as pd
import sqlalchemy
from sqlalchemy import create_engine
import requests
import json
docId = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
title = 'title'
subTitle = 'sub title'
unit = '€'
timeslice = 'M'
reportdate = '2016-01-01'
username = '[email protected]'
password = 'password'
header = {'content-type': 'application/json'}
url = 'https://portal.densio.com/api/documents/{0}/update/'.format(docId)
engine = create_engine('mysql+mysqlconnector://username:password@host/database')
sqlQuery = sqlalchemy.text("""select month, dimension, values
from table
where month = :month
group by month
order by month;""")
dataFrame = pd.read_sql(sqlQuery, engine, params={'month': '2016-01-01'})
dataPivot = dataFrame.pivot(index='dimension', columns='month', values='values')
dataCsv = dataPivot.to_csv(sep=',', encoding='utf-8')
data = {}
data['username'] = username
data['password'] = password
data['csv'] = {}
data['csv']['title'] = title
data['csv']['subtitle'] = subTitle
data['csv']['unit'] = unit
data['csv']['timeslice'] = timeslice
data['csv']['reportdate'] = reportdate
data['csv']['content'] = dataCsv
dataApi = json.dumps(data)
d = requests.post(url, data=dataApi, headers=header, verify=False)
print(d.status_code)
print(d.json())