def fetch_price(country_code='CA-AB', session=None):
"""Requests the last known power price of a given country
Arguments:
country_code (optional) -- used in case a parser is able to fetch multiple countries
session (optional) -- request session passed in order to re-use an existing session
Return:
A dictionary in the form:
{
'countryCode': 'FR',
'currency': EUR,
'datetime': '2017-01-01T00:00:00Z',
'price': 0.0,
'source': 'mysource.com'
}
"""
r = session or requests.session()
url = 'http://ets.aeso.ca/ets_web/ip/Market/Reports/SMPriceReportServlet?contentType=html/'
response = r.get(url)
df_prices = pd.read_html(response.text, match='Price', index_col=0, header=0)
prices = df_prices[1]
data = {}
for rowIndex, row in prices.iterrows():
price = row['Price ($)']
if (isfloat(price)):
hours = int(rowIndex.split(' ')[1]) - 1
data[rowIndex] = {
'datetime': arrow.get(rowIndex, 'MM/DD/YYYY').replace(hours=hours, tzinfo=ab_timezone).datetime,
'countryCode': country_code,
'currency': 'CAD',
'source': 'ets.aeso.ca',
'price': float(price),
}
return [data[k] for k in sorted(data.keys())]
评论列表
文章目录