ranczo-energy-price-scrapers/app.py
2025-09-02 18:06:09 +02:00

84 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import logging
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
import EnergyPriceScraperFactory
from logging_setup import setup_logging
from utils.time_helpers import WARSAW_TZ
from app_impl import hourly_tick
from logging_utils import function_logger
setup_logging(logging.DEBUG)
sched = BlockingScheduler(timezone=WARSAW_TZ)
def run_pstryk():
log = function_logger()
log.info("Running PSTRYK")
try:
conn = EnergyPriceScraperFactory.setup_db()
pstryk_scraper = EnergyPriceScraperFactory.create("Pstryk", conn=conn)
# 11:30 and hourly safety check
hourly_tick(pstryk_scraper)
except Exception as e:
log.error(f"PSTRYK throw an exception: {e}")
def run_PSE_RCE():
log = function_logger()
log.info("Running PSE-RCE")
try:
conn = EnergyPriceScraperFactory.setup_db()
scraper = EnergyPriceScraperFactory.create("PSE_RCE", conn=conn)
hourly_tick(scraper)
except Exception as e:
log.error(f"PSE-RCE throw an exception: {e}")
# Daily triggers at exact local times (DST-safe)
sched.add_job(run_pstryk, 'cron', hour='*', minute='30')
sched.add_job(run_PSE_RCE, 'cron', hour='*', minute='30')
sched.start()
# if __name__ == "__main__":
# setup_logging(logging.DEBUG)
#
# conn = EnergyPriceScraperFactory.setup_db()
# # scraper = EnergyPriceScraperFactory.create("InstratRDN_CSV", conn=conn, path=path)
# # scraper = EnergyPriceScraperFactory.create("PSE_RCE", conn=conn)
# scraper = EnergyPriceScraperFactory.create("Pstryk", conn=conn)
# distribution_cost = DistributionCostFactory.create("TauronG13")
# energy_price = DynamicPricesProvider.DynamicPricesProvider(PROVIDER="instrat", conn=conn, KIND="fixing I", SIDE="buy")
#
# start = datetime(2025, 6, 27, 0, 0) # lokalny czas PL (jeśli naive funkcja sama doda WAW)
#
# rows = []
# for h in range(24*3):
# ts = start + timedelta(hours=h)
# dist = distribution_cost.rate(ts)
# price = energy_price.rate(ts)
# pstryk_fee = 0.08
#
# rows.append({
# "ts": ts,
# "Dystrybucja (net)": dist,
# "Energia (net)": price,
# "Pstryk (net)" : pstryk_fee,
# "Podatki": (distribution_cost.rate(ts) + price + pstryk_fee) * 1.23 - (distribution_cost.rate(ts) + price + pstryk_fee),
# })
#
# fig, ax = plot_stacked_with_negatives(
# rows,
# title="Składowe ceny 1 kWh",
# order=["Dystrybucja (net)", "Energia (net)", "Podatki"] # opcjonalnie
# )
#
# plt.show()