Meteo Vitosha #29170
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Meteo Vitosha | |
#on: push | |
on: | |
schedule: | |
- cron: '*/5 * * * *' | |
# - cron: '*/15 * * * *' | |
# - cron: '*/35 * * * *' | |
jobs: | |
download: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
ref: pages | |
- shell: bash | |
run: | | |
changes="false" | |
response="$(curl --dns-servers '8.8.8.8,1.1.1.1,8.8.4.4,1.0.0.1' 'https://weather.bg/index.php?koiFail=tekushti&lng=0')" | |
echo "Meteo response is: <<<${response}>>>" | |
basefolder="data/meteo/vitosha" | |
# measurement | |
measurement_newdata="$(echo "${response}" | grep -Po '<tr><td>Черни връх</td>.*?</tr>')" | |
echo "Measurement data: ${measurement_newdata}" | |
measurement_targetfile="${basefolder}/measurement/$(date +%Y).html" | |
if [ -f "${measurement_targetfile}" ]; then | |
measurement_olddata="$(tail -n1 "${measurement_targetfile}" || true)" | |
else | |
measurement_olddata="$(tail -n1 "${basefolder}/measurement/$(( $(date +%Y) - 1 )).html" || true)" | |
fi | |
echo "Measurement old data: ${measurement_olddata}" | |
if [ "${measurement_newdata}" != "${measurement_olddata}" ]; then | |
changes="true" | |
mkdir -p "$(dirname "${measurement_targetfile}")" | |
echo "${measurement_newdata}" >> "${measurement_targetfile}" | |
fi | |
# comfort | |
comfort_newdata="$(echo "${response}" | tr -d '\n' | grep -Po "<tr> <td>Черни връх</td>.*?</tr>" | grep -P "<td.+<td.+<td.+<td.+<td.+<td.+<td.+<td.+" || true)" | |
echo "Comfort data: ${comfort_newdata}" | |
if [ "${comfort_newdata}" != "" ]; then | |
comfort_date="$(echo "${response}" | grep 'Индекс на комфорт в синоптичните ни станции на' | sed -n 's/.*Индекс на комфорт в синоптичните ни станции на \([^.]*\)\.\([^.]*\)\.\([^.]*\) в \([^ ]*\).*/\3\2\1 \4/p')" | |
if [ "$(($(date +%s)-$(date +%s -d "${comfort_date}") > -(20 * 60 * 60)))" == "0" ]; then | |
# too far in the future | |
# every night at 11p.m. the operator creates a dublicated record e.g. on 08.01 there will be one extra record 09.01 23:00 (in the future) that is exactly the same as the one at 08.01 23:00 | |
# so we are removing one day | |
comfort_date="$(date +"%Y%m%d %H" -d @"$(( $(date +%s -d "${comfort_date}") - (24 * 60 * 60) ))" )" | |
fi | |
comfort_newdata="$(echo "${comfort_newdata}" | sed -n "s|<td>Черни връх</td>|<td>Черни връх</td>$(date +'<td>%d.%m.%Y</td><td>%H</td>' -d "${comfort_date}")|p")" | |
comfort_targetfile="${basefolder}/comfort/$(date +%Y).html" | |
if [ -f "${comfort_targetfile}" ]; then | |
comfort_olddata="$(tail -n1 "${comfort_targetfile}" || true)" | |
else | |
comfort_olddata="$(tail -n1 "${basefolder}/comfort/$(( $(date +%Y) - 1 )).html" || true)" | |
fi | |
if [ "${comfort_newdata}" != "${comfort_olddata}" ]; then | |
changes="true" | |
mkdir -p "$(dirname "${comfort_targetfile}")" | |
echo "${comfort_newdata}" >> "${comfort_targetfile}" | |
fi | |
fi | |
# ground temperature | |
ground_temperature_newdata="$(echo "${response}" | tr -d '\n' | grep -Po "<tr> <td>Черни връх</td>.*?</tr>" | grep -P "<td.+<td.+<td.+<td.+<td.+<td.+" | grep -vP "<td.+<td.+<td.+<td.+<td.+<td.+<td.+<td.+" || true)" | |
echo "Ground temperature: ${ground_temperature_newdata}" | |
if [ "${ground_temperature_newdata}" != "" ]; then | |
ground_temperature_date="$(echo "${response}" | grep 'Температура на земната повърхност в синоптичните ни станции на' | sed -n 's/.*Температура на земната повърхност в синоптичните ни станции на \([^.]*\)\.\([^.]*\)\.\([^.]*\) в \([^ ]*\).*/\3\2\1 \4/p')" | |
if [ "$(($(date +%s)-$(date +%s -d "${ground_temperature_date}") > -(20 * 60 * 60)))" == "0" ]; then | |
# too far in the future | |
# every night at 11p.m. the operator creates a dublicated record e.g. on 08.01 there will be one extra record 09.01 23:00 (in the future) that is exactly the same as the one at 08.01 23:00 | |
# so we are removing one day | |
ground_temperature_date="$(date +"%Y%m%d %H" -d @"$(( $(date +%s -d "${ground_temperature_date}") - (24 * 60 * 60) ))" )" | |
fi | |
ground_temperature_newdata="$(echo "${ground_temperature_newdata}" | sed -n "s|<td>Черни връх</td>|<td>Черни връх</td>$(date +'<td>%d.%m.%Y</td><td>%H</td>' -d "${ground_temperature_date}")|p")" | |
ground_temperature_targetfile="${basefolder}/ground_temperature/$(date +%Y).html" | |
if [ -f "${ground_temperature_targetfile}" ]; then | |
ground_temperature_olddata="$(tail -n1 "${ground_temperature_targetfile}" || true)" | |
else | |
ground_temperature_olddata="$(tail -n1 "${basefolder}/ground_temperature/$(( $(date +%Y) - 1 )).html" || true)" | |
fi | |
if [ "${ground_temperature_newdata}" != "${ground_temperature_olddata}" ]; then | |
changes="true" | |
mkdir -p "$(dirname "${ground_temperature_targetfile}")" | |
echo "${ground_temperature_newdata}" >> "${ground_temperature_targetfile}" | |
fi | |
fi | |
# snow | |
snow_response="$(curl --dns-servers '8.8.8.8,1.1.1.1,8.8.4.4,1.0.0.1' 'https://hydro.bg/bg/t1.php?ime=&gr=data/&gn=sniag')" | |
echo "Snow response is: <<<${snow_response}>>>" | |
snow_date="$(echo "${snow_response}" | grep "Ежедневни данни за снежната покривка" | sed -nE 's/.*\s([0-9][0-9]?\.[0-9][0-9]?\.[0-9][0-9][0-9][0-9]).*/\1/p' || true)" | |
echo "Snow date: ${snow_date}" | |
if [ "${snow_date}" != "" ]; then | |
snow_info="$(echo "${snow_response}" | grep -A 2 "Черни връх" | tail -n 1 | sed -nE 's!.*>([0-9]*)\|([^|]+)\|[^0-9]*([0-9]*)[^0-9]*.*!<td>\1</td><td>\2</td><td>\3</td>!p')" | |
if [ "${snow_info}" != "" ]; then | |
snow_newdata="<tr><td>${snow_date}</td>${snow_info}</tr>" | |
snow_targetfile="${basefolder}/snow/$(date +%Y).html" | |
if [ -f "${snow_targetfile}" ]; then | |
snow_olddata="$(tail -n1 "${snow_targetfile}" || true)" | |
else | |
snow_olddata="$(tail -n1 "${basefolder}/snow/$(( $(date +%Y) - 1 )).html" || true)" | |
fi | |
if [ "${snow_newdata}" != "${snow_olddata}" ]; then | |
changes="true" | |
mkdir -p "$(dirname "${snow_targetfile}")" | |
echo "${snow_newdata}" >> "${snow_targetfile}" | |
fi | |
fi | |
fi | |
echo "Changes: ${changes} at $(date +"%Y-%m-%dT%H:%M:%S%z")" | |
# commit if new data is available | |
if [ "${changes}" == "true" ]; then | |
echo "Updating" | |
git config --global user.name 'Meteo Vitosha workflow' | |
git config --global user.email '[email protected]' | |
echo "Adding ${basefolder}" | |
git add "${basefolder}" | |
echo "Commiting" | |
git commit -m "Report at $(date +"%Y-%m-%dT%H:%M:%S%z")" | |
echo "Adding remote" | |
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY | |
echo "Push..." | |
git push | |
echo "Ready" | |
fi | |
echo "End" | |