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
|
||||
|
||||
|
||||
19
models.py
19
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))
|
||||
|
||||
@@ -9,22 +9,36 @@
|
||||
<body>
|
||||
<h1>Prix du m³ de fioul pour Languidic</h1>
|
||||
<table id="results" class="u-full-width">
|
||||
<tr>
|
||||
<th colspan="2"></th> <!-- Date -->
|
||||
<th>1000-1499 L</th>
|
||||
<th>500-999 L</th>
|
||||
</tr>
|
||||
% for x in results:
|
||||
<tr class="{{day_classes[x.date.weekday()]}}">
|
||||
<td>{{x.date.strftime('%A')}}</td>
|
||||
<td>{{x.date.strftime('%d %b %Y')}}</td>
|
||||
<td>{{x.price}}</td>
|
||||
</tr>
|
||||
<tr class="{{day_classes[x.date.weekday()]}}">
|
||||
<td>{{x.date.strftime('%A')}}</td>
|
||||
<td>{{x.date.strftime('%d %b %Y')}}</td>
|
||||
<td>{{x.price}}</td>
|
||||
<td>{{x.price500 if x.price500 is not None else "---"}}</td>
|
||||
</tr>
|
||||
% end
|
||||
</table>
|
||||
<div id="price-plot"></div>
|
||||
<script>
|
||||
var plotDiv = document.getElementById('price-plot');
|
||||
var plotData = [{
|
||||
x: {{ [str(d.date) for d in results] }},
|
||||
y: {{ [d.price for d in results] }},
|
||||
type: 'scatter',
|
||||
}];
|
||||
var plotData = [
|
||||
{
|
||||
x: {{ [str(d.date) for d in results] }},
|
||||
y: {{ [d.price for d in results] }},
|
||||
type: 'scatter',
|
||||
name: '1000-1499 L',
|
||||
},
|
||||
{
|
||||
x: {{ [str(d.date) for d in results if d.price500 is not None] }},
|
||||
y: {{ [d.price500 for d in results if d.price500 is not None] }},
|
||||
type: 'scatter',
|
||||
name: '500-999 L',
|
||||
}];
|
||||
var plotLayout = {
|
||||
margin: { t: 0 }
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user