skysurf_hf

fuel.info.sh

Oct 5th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.51 KB | None | 0 0
  1. #!/bin/bash
  2. set -e
  3. set -u
  4. set -o pipefail
  5.  
  6. # Change this values acoording to instructions to match your Home-assistant
  7. PROTOCOL="https"
  8. HOST_IP_OR_NAME="skysurf.duckdns.org"
  9. PORT_NUMBER="8123"
  10. HAPASSWORD="123"
  11. SHOWFUELSHOPLOCATION="YES" \
  12. # Set this to something else like "NO" to remove from friendly names
  13.  
  14. # Don't change anything bellow this line.
  15. # You break it, you fix it.
  16.  
  17. usage () {
  18.  
  19. cat <<EOF
  20.  
  21.   Options
  22.   -------
  23.   h - This help text
  24.   d - Debugging for non-admin
  25.   f - Get the fuel prices and publish them to your home-assistant
  26.  
  27. Usage:
  28. Get fuel prices > $(basename $0) -f "nppostocombustivel nppostocombustivel nppostocombustivel"
  29. Debug script > $(basename $0) -d "nppostocombustivel nppostocombustivel nppostocombustivel"
  30.  
  31. (Enter a nppostocombustivel for each Fuel shop you want to get prices from, separeted by spaces)
  32.  
  33. This utility gets the results of a search in a website so you can scrappe the values  add to your home-assistant as Sensors.
  34. This script should be run from a server every day. find out how to added it to a sheduller like crontab.
  35.  
  36. HOW-TO GET THE nppostocombustivel
  37. ---------------------------------
  38. First of all, lets find out the identifier for the fuel shop you want to control
  39. 1. Open Firefox or Chrome developer tools (Usually pressing F12 key).
  40. 2. Select the network tab from the development tools so you can see the requests.  
  41. 3. Go to http://www.precoscombustiveis.dgeg.pt/
  42. 4. Find the fuel shop you want info from.
  43. 5. Click on the shop so you can see the info
  44. 6. In the developer tools, on the network tab, select only the XHR traffic.
  45. 7. Click on the POST to the infoPostoCB.aspx
  46. 8. On the details for the request, read the parameters sent (Params).
  47. 9. Get the value from nppostocombustivel.
  48.  
  49. HOW-TO configure communication to your Home-assistant
  50. -----------------------------------------------------
  51. Now, lets configure the bellow variables for your Home-assistant and fuel shop
  52. --> PROTOCOL="http" <-- Enter either "http" or "https", depending where your Home-sistant is listening
  53. --> HOST_IP_OR_NAME="localhost" <-- Usually "localhost" is fine. If you are running this script from another computer other than the one running Home-assistant, enter either a named address like "myserver.myhouse" or an internet protocol number (IP) like "192.168.1.20"
  54. --> PORT_NUMBER="8123" <-- This is the port number your Home-assistant is listening.
  55. --> HAPASSOWRD="SomePassword" <-- You shouldnt leave you Home-assistant running without a password. Enter yours here. Its the same password you entered for api_password: at configuration.yaml
  56. EOF
  57.  
  58. exit 0
  59. }
  60.  
  61.  
  62. getFuelPrices () {
  63.     #DEBUG
  64.     # echo $FUELSHOPS
  65.     for FUELSHOP in $FUELSHOPS;
  66.     do
  67.     # Read local gas and diesel prices
  68.     #DEBUG
  69.     # echo $FUELSHOP
  70.     curl -s "http://www.precoscombustiveis.dgeg.pt/include/infoPostoCB.aspx" \
  71.     -H "Host:www.precoscombustiveis.dgeg.pt" \
  72.     -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:59.0) Gecko/20100101 Firefox/59.0" \
  73.     -H "Accept: */*" -H "Accept-Language: en-US,en;q=0.5" --compressed \
  74.     -H "Referer: http://www.precoscombustiveis.dgeg.pt/pagina.aspx?screenwidth=1280&mlkid=gfc3xf55hmk5dz55d0vqq0mm&menucb=1" \
  75.     -H "Content-Type: application/x-www-form-urlencoded;" \
  76.     -H "Cookie:ASP.NET_SessionId=gfc3xf55hmk5dz55d0vqq0mm; mlkid=gfc3xf55hmk5dz55d0vqq0mm; Origem=MQ2; StartTime=MzksNDI2MjM4Nw2; Saida=SW5mb3JtYcOnw6NvIFPDrXRpbyBQcmXDp29zIENvbWJ1c3TDrXZlaXM1; ASPSESSIONIDAQSABRRD=EIBICCIDNJBNFDLPPNJGNEGA" \
  77.     -H "DNT: 1" -H "Connection: keep-alive" \
  78.     -H "Pragma: no-cache" \
  79.     -H "Cache-Control: no-cache" --data "tipo=popup&nppostocombustivel=$FUELSHOP" > .data 2>&1
  80.  
  81.     cat .data | sed 's/||/\\\n/g' | sed 's/<[^>]*>/|/g' | sed 's/\€/EUR/g' | sed 's/||/\n/g' | sed 's/\ |//g' | sed 's/|G/G/g' | sed 's/\ EUR//g'  | sed 's/\á/a/g' | sed 's/\ó/o/g' | sed 's/\ \-\ //g' > .information
  82.  
  83.     FUELSHOPLOCATION=$(cat .data | sed 's/.*infoTitulo\"><h1>//g' | sed 's/<\/h1>.*//g' | sed 's/<br>.*//g')
  84.     #DEBUG
  85.     #echo $FUELSHOPLOCATION
  86.       # Clean the html out of this to get the values for each fuel price.
  87.       for LABEL in 'Gasoleo simples' 'Gasoleo\|' 'Gasoleo especial' 'Gásoleo colorido' 'Gasolina simples 95' \
  88.         'Gasolina 95' 'Gasolina especial 95' 'Gasolina 98' 'Gasolina especial 98' 'GPL Auto' \
  89.         'GNC (gas natural comprimido) - EUR/m3' 'GNC (gas natural comprimido) - EUR/kg' \
  90.         'GNL (gas natural liquefeito) - EUR/kg';
  91.       do
  92.         SEDLABEL=$(echo $LABEL | sed 's/\ /\\ /g' | sed 's/\//\\\//g')
  93.         FUELPRICE=$(awk -F"|" '/'"$SEDLABEL"'/{print $2}' .information)
  94.         sensorType=$(echo "$LABEL" | sed 's/\ /_/g' | sed 's~[^[:alnum:]|_]\+~~g')
  95.  
  96.         if [ $SHOWFUELSHOPLOCATION = "YES" ]
  97.         then
  98.           FRIENDLYNAME="$LABEL ($FUELSHOPLOCATION)"
  99.         else
  100.           FRIENDLYNAME="$LABEL"
  101.         fi
  102.  
  103.         SENSOR="$FUELSHOP"
  104.         SENSOR+="_"
  105.         SENSOR+="$sensorType"
  106.  
  107.             if ! [ -z "$FUELPRICE" ];
  108.             then
  109.                 # DEBUG
  110.                 #echo "$LABEL $FUELPRICE"
  111.                 # Add to home-assistant
  112.                 curl -s -X POST -H "x-ha-access: $HAPASSWORD" \
  113.                 -H "Content-Type: application/json" \
  114.                 -d '{"state": "'$FUELPRICE'", "attributes": {"unit_of_measurement": "€", "icon": "mdi:gas-station", "friendly_name":"'"$FRIENDLYNAME"'"}}' \
  115.                 $PROTOCOL://$HOST_IP_OR_NAME/api/states/sensor.fuel_$SENSOR >/dev/null 2>&1
  116.             fi
  117.       done
  118.   done
  119.   exit 0
  120. }
  121.  
  122. debugme () {
  123.     #DEBUG
  124.     # set -x
  125.     SHOWFUELSHOPLOCATION="YES"
  126.     echo $FUELSHOPS > debugme.txt
  127.     for FUELSHOP in $FUELSHOPS;
  128.     do
  129.     # Read local gas and diesel prices
  130.     #DEBUG
  131.     # echo $FUELSHOP
  132.     curl -s "http://www.precoscombustiveis.dgeg.pt/include/infoPostoCB.aspx" \
  133.     -H "Host:www.precoscombustiveis.dgeg.pt" \
  134.     -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:59.0) Gecko/20100101 Firefox/59.0" \
  135.     -H "Accept: */*" -H "Accept-Language: en-US,en;q=0.5" --compressed \
  136.     -H "Referer: http://www.precoscombustiveis.dgeg.pt/pagina.aspx?screenwidth=1280&mlkid=gfc3xf55hmk5dz55d0vqq0mm&menucb=1" \
  137.     -H "Content-Type: application/x-www-form-urlencoded;" \
  138.     -H "Cookie:ASP.NET_SessionId=gfc3xf55hmk5dz55d0vqq0mm; mlkid=gfc3xf55hmk5dz55d0vqq0mm; Origem=MQ2; StartTime=MzksNDI2MjM4Nw2; Saida=SW5mb3JtYcOnw6NvIFPDrXRpbyBQcmXDp29zIENvbWJ1c3TDrXZlaXM1; ASPSESSIONIDAQSABRRD=EIBICCIDNJBNFDLPPNJGNEGA" \
  139.     -H "DNT: 1" -H "Connection: keep-alive" \
  140.     -H "Pragma: no-cache" \
  141.     -H "Cache-Control: no-cache" --data "tipo=popup&nppostocombustivel=$FUELSHOP" > .data 2>&1
  142.  
  143.     cat .data >> debugme.txt
  144.  
  145.     echo "==============" >> debugme.txt
  146.  
  147.     cat .data | sed 's/||/\\\n/g' | sed 's/<[^>]*>/|/g' | sed 's/\€/EUR/g' | sed 's/||/\n/g' | sed 's/\ |//g' | sed 's/|G/G/g' | sed 's/\ EUR//g'  | sed 's/\á/a/g' | sed 's/\ó/o/g' | sed 's/\ \-\ //g' > .information
  148.  
  149.     cat .information >> debugme.txt
  150.  
  151.     echo "==============" >> debugme.txt
  152.  
  153.     FUELSHOPLOCATION=$(cat .data | sed 's/.*infoTitulo\"><h1>//g' | sed 's/<\/h1>.*//g' | sed 's/<br>.*//g')
  154.     #DEBUG
  155.     echo $FUELSHOPLOCATION >> debugme.txt
  156.       # Clean the html out of this to get the values for each fuel price.
  157.       for LABEL in 'Gasoleo simples' 'Gasoleo\|' 'Gasoleo especial' 'Gásoleo colorido' 'Gasolina simples 95' \
  158.         'Gasolina 95' 'Gasolina especial 95' 'Gasolina 98' 'Gasolina especial 98' 'GPL Auto' \
  159.         'GNC (gas natural comprimido) - EUR/m3' 'GNC (gas natural comprimido) - EUR/kg' \
  160.         'GNL (gas natural liquefeito) - EUR/kg';
  161.       do
  162.         SEDLABEL=$(echo $LABEL | sed 's/\ /\\ /g' | sed 's/\//\\\//g')
  163.         FUELPRICE=$(awk -F"|" '/'"$SEDLABEL"'/{print $2}' .information)
  164.         sensorType=$(echo "$LABEL" | sed 's/\ /_/g' | sed 's~[^[:alnum:]|_]\+~~g')
  165.  
  166.         if [ $SHOWFUELSHOPLOCATION = "YES" ]
  167.         then
  168.           FRIENDLYNAME="$LABEL ($FUELSHOPLOCATION)"
  169.         else
  170.           FRIENDLYNAME="$LABEL"
  171.         fi
  172.  
  173.         SENSOR="$FUELSHOP"
  174.         SENSOR+="_"
  175.         SENSOR+="$sensorType"
  176.  
  177.             if ! [ -z "$FUELPRICE" ];
  178.             then
  179.                 # DEBUG
  180.                 echo "$LABEL $FUELPRICE" >> debugme.txt
  181.             fi
  182.       done
  183.   done
  184.   exit 0
  185. }
  186.  
  187. while getopts 'dh:f' OPTION; do
  188.   case "$OPTION" in
  189.     f)
  190.       FUELSHOPS="$2"
  191.       getFuelPrices
  192.       ;;
  193.     d)
  194.       FUELSHOPS="$2"
  195.       debugme
  196.       ;;
  197.     h)
  198.       usage
  199.       ;;
  200.  
  201.     ?)
  202.       usage
  203.       exit 1
  204.       ;;
  205.   esac
  206. done
  207. shift "$(($OPTIND -1))"
Advertisement
Add Comment
Please, Sign In to add comment