Skip to content

Export PVPC data

Export PVPC data #3198

Workflow file for this run

name: "Export PVPC data"
on:
workflow_dispatch:
inputs:
push:
description: "Push data?"
type: boolean
required: true
default: false
telegram:
description: "Publish data on Telegram?"
type: boolean
required: true
default: false
day:
description: "Day to get (today/tomorrow, or date with format YYYY-MM-DD)"
required: true
default: "today"
schedule:
- cron: "0 20,21,22 * * *" # 20:00 UTC = 21:00h CET = 22:00 CEST
jobs:
export:
name: "Export PVPC data"
runs-on: ubuntu-latest
env:
# Relative paths to directories, without prefixes nor suffixes.
# Current directory is repository root after clone.
PVPC_LIB_PATH: "python-pvpc"
PVPC_OUTPUT_BASE_PATH: "data"
PVPC_OUTPUT_PCB_PATH: "data/pcb/{year}/{month}/{day}.json"
PVPC_OUTPUT_CM_PATH: "data/cm/{year}/{month}/{day}.json"
steps:
# Setup local
- name: "Checkout"
uses: actions/checkout@v3
with:
fetch-depth: 1
- name: "Checkout python-pvpc"
uses: actions/checkout@v3
with:
repository: David-Lor/python-pvpc
path: ${{ env.PVPC_LIB_PATH }}
fetch-depth: 1
- name: "Setup Python"
uses: actions/setup-python@v3
with:
python-version: "3.9"
architecture: "x64"
- name: "Hash Python requirements file"
working-directory: ${{ env.PVPC_LIB_PATH }}
run: |-
checksum=$(printf $(sha256sum requirements.txt))
echo "PVPC_LIB_REQUIREMENTS_CHECKSUM=$checksum" >> $GITHUB_ENV
- name: "Cache Python packages"
uses: actions/cache@v2
with:
key: "cache-python-exportdata-${{ env.PVPC_LIB_REQUIREMENTS_CHECKSUM }}"
path: |
~/.cache/pip
~/.local/lib/python3.8/site-packages
- name: "Install Python requirements"
working-directory: ${{ env.PVPC_LIB_PATH }}
run: |
pip install -r requirements.txt
- name: "Set push flag"
if: ${{ github.event_name == 'schedule' || github.event.inputs.push == 'true' }}
run: |
echo "flagPush=true" >> $GITHUB_ENV
- name: "Set day to fetch"
# If input.day not defined, set "tomorrow" by default
run: |
day="${{ github.event.inputs.day }}"
test "$day" || day="tomorrow"
echo "PVPC_DATE=$day" >> $GITHUB_ENV
- name: "Extend Python path"
run: |
newpath="$PYTHONPATH:$(pwd)/${{ env.PVPC_LIB_PATH }}"
echo "PYTHONPATH=$newpath" >> $GITHUB_ENV
# Export data
- name: "Export data"
run: |
python scripts/export_pvpc.py
- name: "List changes"
run: |
echo "Output PVPC data for Peninsula/Canarias/Baleares:"
cat "${{ env.PVPC_OUTPUT_PCB_PATH_FORMATTED }}"
echo ""
echo "Output PVPC for Ceuta/Melilla:"
cat "${{ env.PVPC_OUTPUT_CM_PATH_FORMATTED }}"
# Commit & Push data
- name: "Commit exported data"
if: ${{ env.flagPush == 'true' }}
id: commit
uses: EndBug/add-and-commit@v7
with:
add: "${{ env.PVPC_OUTPUT_BASE_PATH }}/*"
push: true
message: |
Export PVPC data
PVPC data for ${{ env.PVPC_DATE_FORMATTED }}
# Publish data on Telegram channels
- name: "Set telegram flag"
if: ${{ (github.event_name == 'schedule' && steps.commit.outputs.committed == 'true') || github.event.inputs.telegram == 'true' }}
run: |
echo "flagTelegram=true" >> $GITHUB_ENV
- name: "Publish data on Telegram channel (pcb)"
if: ${{ env.flagTelegram == 'true' }}
env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_CHATID: ${{ secrets.TELEGRAM_CHATID_CHANNEL_PCB }}
DATA_PATH: ${{ env.PVPC_OUTPUT_PCB_PATH_FORMATTED }}
run: |
python scripts/upload_telegram.py
healthcheck_ok:
name: "Push Healthcheck (OK)"
needs: [ export ]
if: ${{ success() }}
runs-on: ubuntu-latest
steps:
- name: "Call healthcheck webhook"
run: curl -silent "${{ secrets.HEALTHCHECK_URL }}?status=up&msg=OK"
healthcheck_ko:
name: "Push Healthcheck (KO)"
needs: [ export ]
if: ${{ failure() }}
runs-on: ubuntu-latest
steps:
- name: "Call healthcheck webhook"
run: curl -silent "${{ secrets.HEALTHCHECK_URL }}?status=down&msg=Failed"