forked from Logan1x/Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighcharts_loader.py
More file actions
56 lines (40 loc) 路 1.52 KB
/
Copy pathhighcharts_loader.py
File metadata and controls
56 lines (40 loc) 路 1.52 KB
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
48
49
50
51
52
53
54
55
56
import json
from base64 import b64encode
import requests
class EmptyParams(Exception):
pass
class ToManyParams(Exception):
pass
class Options:
data = {}
def __init__(self, *, from_file=None, from_dict=None):
if not from_file and not from_dict:
raise EmptyParams('You should pass "from_file" or "from_dict" param.')
if from_file and from_dict:
raise ToManyParams('You should pass only one param: "from_file" or "from_dict".')
self.data = json.loads(open(from_file).read()) if from_file else from_dict
class ChartLoader:
raw_chart_data = None
url = 'http://export.highcharts.com/'
image_type = None
def __init__(self, options: Options, image_type='image/png'):
self.image_type = image_type
response = requests.post(self.url, data={
'type': image_type,
'options': json.dumps(options.data)
})
if response.status_code == requests.codes.ok:
self.raw_chart_data = response.content
else:
response.raise_for_status()
def _decoded_chart(self):
return b64encode(self.raw_chart_data).decode()
def get_data_image(self):
return 'data:image/{0};charset=utf-8;base64,{1}'.format(self.image_type,
self._decoded_chart())
def get_raw_data(self):
return self.raw_chart_data
def save_to_file(self, path):
f = open(path, 'wb+')
f.write(self.raw_chart_data)
f.close()