Files
fioul/fioul.py

53 lines
1.2 KiB
Python

#! /usr/bin/env python3
# from bottle import hook, request, route, run, static_file, view
from datetime import date
from models import Price, IntegrityError
import requests
import json
query = """query {
getProductGroups(category: 1, zipcode: 1184) {
name
default_price_tax
}
}
"""
def get_price():
response = requests.post(
url="https://www.bretagne-multi-energies.fr/graphql",
json={"query": query},
headers={"content-type": "application/json"},
)
json_data = json.loads(response.text)
products = json_data["data"]["getProductGroups"]
product = next(
filter(lambda x: x["name"] == "Fioul Domestique Standard", products), None
)
return product["default_price_tax"]
def store_current_price():
today = date.today()
# Get price returns a float (price per L)
# We store an int (price per m^3)
price = int(get_price() * 1000)
try:
Price.create(date=today, price=price)
except IntegrityError:
print("Price for today already in database, not writing")
return price
def main():
print(get_price())
store_current_price()
if __name__ == "__main__":
main()