JLindvig

Flag Dage DK - Update with icon

May 7th, 2020
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.44 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # TOOLS
  4. # I have a bunch of tools in a separate file
  5. # Essentially you will only need this function:
  6. #
  7. # _send_data
  8. # $1 = query
  9. # $2 = URL
  10. # _send_data() {
  11. #   curl -X POST \
  12. #   -H "Accept: application/json" \
  13. #   -H "Authorization: Bearer $APITOKEN" \
  14. #   -H "Content-Type: application/json" \
  15. #   -d "$1" \
  16. #   $2
  17. # }
  18. #
  19. # Load the tools
  20. source /config/shell/tools.sh
  21.  
  22. # CONST
  23. # I stored my shell-secrets in "/config/shell_secrets.txt"
  24. # These are the needed secrets:
  25. #
  26. # APITOKEN="YOUR TOKEN"
  27. # API_STATES_PATH="api/states"
  28. # BASE_URL="http://YOUR_HA_IP:8123/"
  29. # TEMP_PATH="temp"
  30. #
  31. # Load the secrets
  32. source /config/shell_secrets.txt
  33.  
  34. # Have we provided a "Days in advance"?
  35. # No, then 0
  36. if [ -z "$1" ]; then
  37.   DAYS_IN_ADVANCE=0
  38. else
  39.   DAYS_IN_ADVANCE=$1
  40. fi
  41.  
  42. # Initial cleanup
  43. rm -f $TEMP_PATH/tmp_file $TEMP_PATH/combined_file
  44.  
  45. # Fecth the HTML page
  46. curl https://designflag.dk/om-flag/flagdage/ -o $TEMP_PATH/tmp_file
  47.  
  48. # Extract the Year
  49. YEAR=$(grep "<h1>Officielle flagdage 2020</h1>" $TEMP_PATH/tmp_file | cut -d' ' -f3 | cut -d'<' -f1)
  50.  
  51. # Extract the data - combine the 2 columns on 1 line - store in a file
  52. grep '<td style="text-align: left;" valign="top" width="102">\|<td style="text-align: left;" valign="top" width="550">' $TEMP_PATH/tmp_file | cut -d'>' -f2 | cut -d'<' -f1 | awk 'NF' | sed '{N; s/\n/|/}' > $TEMP_PATH/combined_file
  53.  
  54. # Prepare the placeholders
  55. STATE=-1
  56. QUERY=""
  57. ATTR="\"events\": [ "
  58. CNT=0
  59. NOW=$(date +"%s")
  60. while read line; do
  61.  
  62.   # Extract the day
  63.   DAY=$(echo "$line" | cut -d'.' -f1)
  64.  
  65.   # Extract the month and convert it to number
  66.   MONTH=$(case $(echo "$line" | cut -d' ' -f2 | cut -d'|' -f1) in
  67.       januar)     echo 1;;
  68.       februar)    echo 2;;
  69.       marts)      echo 3;;
  70.       april)      echo 4;;
  71.       maj)        echo 5;;
  72.       juni)       echo 6;;
  73.       juli)       echo 7;;
  74.       august)     echo 8;;
  75.       september)  echo 9;;
  76.       oktober)    echo 10;;
  77.       november)   echo 11;;
  78.       december)   echo 12;;
  79.   esac)
  80.  
  81.   # Calculate the timestamp
  82.   TIMESTAMP=$(date -d "$YEAR-$MONTH-$DAY" +"%s")
  83.  
  84.   # Format date in proper manner
  85.   DATE=$(date -d "$YEAR-$MONTH-$DAY" +"%d-%m-%Y")
  86.  
  87.   # Extract the description of the event
  88.   EVENT=$(echo "$line" | cut -d'|' -f2)
  89.  
  90.   # Check events until we have found the first event in the future
  91.   if [[ $STATE -lt 0 ]]; then
  92.  
  93.     # Calculate days to the next event
  94.     NEW_STATE=$(( ($TIMESTAMP - $NOW)/(60*60*24) ))
  95.  
  96.     # Is the new event today or in the future
  97.     if [[ $NEW_STATE -ge 0 ]]; then
  98.  
  99.       # Set the state of the next event
  100.       STATE=$NEW_STATE
  101.      
  102.       # Prepare the main part of the query
  103.       # ( substract days in advsnce )
  104.       QUERY="{ \"state\": \"$(($NEW_STATE - $DAYS_IN_ADVANCE))\", \"attributes\": { \"next_date\": \"$DATE\", \"next_event\": \"$EVENT\", \"next_timestamp\": \"$TIMESTAMP\", \"days_in_advance\": \"$DAYS_IN_ADVANCE\", \"icon\": \"mdi:flag\""
  105.     fi
  106.   fi
  107.  
  108.   # Load all events
  109.   # Have been here before....? Add ","
  110.   CNT=$((CNT+1))
  111.   if [[ $CNT -gt 1 ]]; then
  112.     ATTR="$ATTR, "
  113.   fi
  114.  
  115.   # Append the data of the event
  116.   ATTR="$ATTR{ \"date\": \"$DATE\", \"event\": \"$EVENT\", \"timestamp\": \"$TIMESTAMP\" }"
  117.  
  118. done < $TEMP_PATH/combined_file
  119.  
  120. # Finish the query
  121. QUERY="$QUERY, $ATTR ] } }"
  122.  
  123. # Send the query to the API
  124. _send_data $QUERY $BASE_URL$API_STATES_PATH/sensor.flagday_dk
  125.  
  126. # Cleanup on exit
  127. rm -f $TEMP_PATH/tmp_file $TEMP_PATH/combined_file
Add Comment
Please, Sign In to add comment