diff --git a/fioul.py b/fioul.py index f0dafc0..1f892ab 100755 --- a/fioul.py +++ b/fioul.py @@ -11,6 +11,39 @@ from config import conf from models import IntegrityError, Price +def get_today_prices_from_internet(): + query = """query { + getProducts(group: 1, zipcode: 1184) { + tax { + value + } + pricesZone { + price + min_quantity + } + } + } + """ + 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)["data"] + json_data = json_data["getProducts"][0] + tax = float(json_data["tax"]["value"]) + zones = json_data["pricesZone"] + price_500 = next((x["price"] for x in zones if x["min_quantity"] == 500), None) + price_1000 = next((x["price"] for x in zones if x["min_quantity"] == 1000), None) + # apply tax + price_500 = price_500 * (100 + tax) / 100.0 + price_1000 = price_1000 * (100 + tax) / 100.0 + # Return an int, for db storage + price_500 = int(price_500 * 1000) + price_1000 = int(price_1000 * 1000) + return price_500, price_1000 + + def get_today_price_from_internet(): query = """query { getProductGroups(category: 1, zipcode: 1184) { @@ -37,16 +70,16 @@ def get_prices(n=10): return query -def store_current_price(): +def store_current_prices(): today = date.today() # Get price returns a float (price per L) # We store an int (price per m^3) - price = int(get_today_price_from_internet() * 1000) + price500, price1000 = get_today_prices_from_internet() try: - Price.create(date=today, price=price) + Price.create(date=today, price=price1000, price500=price500) except IntegrityError: print("Price for today already in database, not writing") - return price + return price500, price1000 # ---------------------------------------------------------------------- @@ -74,13 +107,19 @@ def argument(*name_or_flags, **kwargs): @subcommand() def update(args): - store_current_price() + store_current_prices() @subcommand([argument("n", default=42, nargs="?", type=int)]) def show(args): for x in get_prices(args.n): - print(x.date, x.price) + print(x.date, x.price, x.price500) + + +@subcommand() +def show_current(args): + current_price = get_today_prices_from_internet() + print(current_price) @subcommand() @@ -100,6 +139,13 @@ def initdb(args): init() +@subcommand() +def migratedb(args): + from models import migrate_001 + + migrate_001() + + # ---------------------------------------------------------------------- # Main diff --git a/models.py b/models.py index 125e15e..d180b25 100644 --- a/models.py +++ b/models.py @@ -35,8 +35,27 @@ class Price(BaseModel): id = AutoField() date = DateField(unique=True) price = IntegerField() + price500 = IntegerField() def init(): db.connect() db.create_tables([Price]) + + +def migrate_001(): + backend = conf["db"].get("backend", None) + if backend == "sqlite": + from playhouse.migrate import SqliteMigrator + + migrator = SqliteMigrator(db) + elif backend == "mysql": + from playhouse.migrate import MySQLMigrator + + migrator = MysqlMigrator(db) + + from playhouse.migrate import migrate + + price500_field = IntegerField(null=True) + with db.atomic(): + migrate(migrator.add_column("price", "price500", price500_field)) diff --git a/views/search_results.tpl b/views/search_results.tpl index b586308..58c20ff 100644 --- a/views/search_results.tpl +++ b/views/search_results.tpl @@ -9,22 +9,36 @@

Prix du m³ de fioul pour Languidic

+ + + + + % for x in results: - - - - - + + + + + + % end
1000-1499 L500-999 L
{{x.date.strftime('%A')}}{{x.date.strftime('%d %b %Y')}}{{x.price}}
{{x.date.strftime('%A')}}{{x.date.strftime('%d %b %Y')}}{{x.price}}{{x.price500 if x.price500 is not None else "---"}}