-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnordpool_cache.py
38 lines (29 loc) · 1.07 KB
/
nordpool_cache.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
#!/usr/bin/python
# coding: utf-8
import configparser
from urllib.request import urlopen
import json
import datetime
import pytz
import os
import codecs
main_base = os.path.dirname(__file__)
config_file = os.path.join(main_base, "config", "prod.cfg")
config = configparser.ConfigParser()
config.read_file(codecs.open(config_file, 'r', 'utf8'))
dir_path = config.get('Nordpool', 'cache_dir')
area = config.get('Nordpool', 'area')
currency = config.get('Nordpool', 'currency')
def save_price(date):
response = urlopen('https://www.nordpoolgroup.com/api/marketdata/page/10?currency={}&endDate={}'.format(currency, date))
data = json.loads(response.read())
with open('{}/{}.json'.format(dir_path, date), 'w') as outfile:
json.dump(data, outfile)
#Get todays prices
dt_today = datetime.date.today().strftime("%d-%m-%Y")
save_price(dt_today)
#We only have next day prices after 12:00
now = datetime.datetime.now(pytz.timezone('CET'))
if now.time() >= datetime.time(12,00):
dt_tomorrow = (datetime.date.today()+ datetime.timedelta(days=1)).strftime("%d-%m-%Y")
save_price(dt_tomorrow)