Skip to content

Commit

Permalink
Merge pull request #28 from guerda/fix_slots
Browse files Browse the repository at this point in the history
Fix time slot determination
  • Loading branch information
guerda authored Jan 1, 2025
2 parents b8dd3c4 + 8fcc263 commit a82e4bd
Show file tree
Hide file tree
Showing 8 changed files with 1,078 additions and 14 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
python-version: ["3.11", "3.12", "3.13"]

steps:
- uses: actions/checkout@v4
Expand All @@ -31,3 +31,6 @@ jobs:
- name: Lint with ruff
run: pipenv run ruff check

- name: Test with pytest
run: pipenv run pytest
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ playwright = "*"

[dev-packages]
ruff = "*"
pytest = "*"

[requires]
python_version = "3.11"
35 changes: 34 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 28 additions & 12 deletions euemastobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@
mastodon = None


def get_time_slots():
api_url = "https://api.energy-charts.info/ren_share_forecast?country=de"
headers = {
"accept": "application/json",
"User-Agent": "Erneuerbare Energien Überschuss Mastobot",
}
r = requests.get(api_url, headers=headers)
r.raise_for_status()
logger.info("got the forecast data")
forecast = r.json()

def get_slots_from_forecast(forecast):
i = 0
filter_above_threshold = []
for row in forecast["ren_share"]:
Expand All @@ -30,22 +20,48 @@ def get_time_slots():
else:
filter_above_threshold.append(0)
i += 1

logging.debug("Above threshold: ")
logging.debug(filter_above_threshold)
start = 0
previous = 0
slots = []
i = 0
added = True
for time_slice in filter_above_threshold:
if time_slice != previous:
previous = time_slice
if time_slice == 1:
start = forecast["unix_seconds"][i]
added = False
elif time_slice == 0:
end = forecast["unix_seconds"][i - 1]
start_text = datetime.fromtimestamp(start).strftime("%H:%M")
end_text = datetime.fromtimestamp(end).strftime("%H:%M")
slots.append((start_text, end_text))
added = True
i += 1

# If the threshold was exceeded at the end, has it been added to the array yet?
if not added:
start_text = datetime.fromtimestamp(start).strftime("%H:%M")
end_text = "0:00"
slots.append((start_text, end_text))
return slots


def get_time_slots():
api_url = "https://api.energy-charts.info/ren_share_forecast?country=de"
headers = {
"accept": "application/json",
"User-Agent": "Erneuerbare Energien Überschuss Mastobot",
}
r = requests.get(api_url, headers=headers)
r.raise_for_status()
logger.info("got the forecast data")
forecast = r.json()

slots = get_slots_from_forecast(forecast)

return slots


Expand Down
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S
log_level = DEBUG
Loading

0 comments on commit a82e4bd

Please sign in to comment.