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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
| # #######################################
# #######################################
from datetime import datetime, timedelta, timezone
from tzlocal import get_localzone
from typing import Dict
#from collections import defaultdict
import math, random
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
#import requests
import json
from aiohttp import ClientSession
import asyncio
# #######################################
# # via eklok API
# #######################################
BASE_URL = "https://eklok.nl/api"
def darkenRGB(color,factor):
R=hex(int(int(color[1:3],16)/factor))[2:]
G=hex(int(int(color[3:5],16)/factor))[2:]
B=hex(int(int(color[5:7],16)/factor))[2:]
return '#'+R.rjust(2,'0')+G.rjust(2,'0')+B.rjust(2,'0')
def _headers() -> Dict[str, str]:
return {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Cookie": "policyaccepted=true",
}
async def _get_observations(_session: ClientSession):
body = {}
async with _session.get(
f"{BASE_URL}/pricedetail", headers=_headers(), raise_for_status=True
) as response:
return await response.json()
async def main():
async with ClientSession() as session:
data= await _get_observations(session)
await session.close()
return data
local_tz = get_localzone()
#real data
data=json.loads(data.replace("'","\"").replace("False","\"False\"").replace("True","\"True\""))
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
data=loop.run_until_complete(main())
datenow=datetime.now().astimezone(local_tz)
datetoday=datetime(datenow.year,datenow.month,datenow.day,0,0,0).astimezone(local_tz)
items=dict()
storedata=False
firstdate,prev=datenow,datenow
currentcolor,currenttime='',''
for item in data['data']:
item['date']=datetime.strptime(item['date'],'%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=timezone.utc)
if item['date']>=firstdate+timedelta(hours=12):
break
if storedata or item['date']>=datenow+timedelta(hours=-1):
storedata=True
else:
firstdate=item['date']
if datenow>prev and datenow <= item['date']:
currentcolor=item['color']
diff=item['date']-datetoday
currenttime_linear=(diff.days+diff.seconds/60/60/24)*24
currenttime_polar=(15/12-1.99*(diff.days+diff.seconds/60/60/24))*2*math.pi
prev=item['date']
if storedata:
for ii in item:
if ii not in items:
items[ii]=[]
if ii=='color':
items[ii+'_hist']=[]
if ii=='date':
items[ii+'_linear']=[]
items[ii+'_polar']=[]
items[ii].append(item[ii])
if ii=='range':
items[ii][-1]+=8*random.random()
if ii=='color':
d=item['date']-datenow
factor=1-2*(d.days*24+d.seconds/60/60)
factor=1 if factor<=1 else max(1,factor**1.5)
darkenedcolor=darkenRGB(item['color'],factor)
items[ii+'_hist'].append(darkenedcolor)
if ii=='date':
diff=item['date']-datetoday
items[ii+'_linear'].append((diff.days+diff.seconds/60/60/24)*24)
items[ii+'_polar'].append((3/12-1.99*(diff.days+diff.seconds/60/60/24))*2*math.pi)
plt.style.use('dark_background')
fig, ax = plt.subplots()
ax.set_yticks([])
xstart=math.floor(items['date_linear'][0])
ax.set_xlim(xstart,xstart+12)
ax.set_xticks([x for x in range(xstart,xstart+12+1)])
ax.set_xticklabels([x%24 for x in range(xstart,xstart+12+1)])
ax.bar(items['date_linear'], [abs(r)+30 for r in items['range']],color=items['color_hist'],width=12/len(items['range']))
ax.bar([currenttime_linear], [100+100+50],color='#FFFFFF',width=12/len(items['range']))
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.set_rticks([])
ax.set_xticks([r*2*math.pi/12 for r in range(12)])
ax.set_xticklabels(['.' if r%3 else str((3-r)%12+12*(r==3)) for r in range(12)])
ax.grid(False)
ax.bar(items['date_polar'], [abs(r)+30+400 for r in items['range']],color=items['color_hist'],width=12/len(items['range']))
ax.bar(items['date_polar'], [400 for r in items['range']],color='#000000',width=12/len(items['range']))
ax.bar(items['date_polar'], [380 for r in items['range']],color=currentcolor,width=12/len(items['range']))
ax.bar(items['date_polar'], [100 for r in items['range']],color='#000000',width=12/len(items['range']))
ax.bar([currenttime_polar], [100+100+50],color='#FFFFFF',width=12/len(items['range']))
ax.bar([currenttime_polar], [100+100+100],color='#FFFFFF',width=12/len(items['range'])/1.3)
ax.bar([currenttime_polar], [100+100+150],color='#FFFFFF',width=12/len(items['range'])/1.5)
ax.bar([currenttime_polar], [100+100+200],color='#FFFFFF',width=12/len(items['range'])/2)
ax.bar([currenttime_polar], [100+100+300],color='#FFFFFF',width=12/len(items['range'])/3)
ax.bar([currenttime_polar], [100+100+400],color='#FFFFFF',width=12/len(items['range'])/4)
plt.show() |