Add prices for lower quantities (500-999L)
Don't forget to run the migratedb command
This commit is contained in:
58
fioul.py
58
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user