Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PARSEHUB_API_TOKEN="DIT_PARSEHUB_TOKEN"
- PARSEHUB_LIBRARY_TOKEN="DIT_PARSEHUB_PROJECT"
- PARSEHUB_LIBRARY_URL="https://bibliotek.dk/"
- PARSEHUB_LIBRARY_CREDENTIALS="{\"mail\":\"din@mail.dk\", \"password\":\"KODE\"}"
- APITOKEN="TOKEN FRA HOME ASSISTANT"
- API_STATES_PATH="api/states"
- BASE_URL="http://DIN_HA_URL:8123/"
- TEMP_PATH="temp"
- # PUBLIC
- # START SCRAPER
- library_start_scrape() {
- _parsehub_start_scrape \
- $PARSEHUB_API_TOKEN \
- $PARSEHUB_LIBRARY_URL \
- $PARSEHUB_LIBRARY_TOKEN \
- $PARSEHUB_LIBRARY_CREDENTIALS
- }
- # GET DATA
- # Get the library loans and send it to HA
- # 1: Friendly_name for sensor in HA
- # 2: Entity_id for sensor
- library_get() {
- rm -f $TEMP_PATH/parsehub.json $TEMP_PATH/parsehub_edit.json
- curl -X GET "https://www.parsehub.com/api/v2/projects/$PARSEHUB_LIBRARY_TOKEN/last_ready_run/data?api_key=$PARSEHUB_API_TOKEN" | gunzip > $TEMP_PATH/parsehub.json
- sed 's/.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*/|\1/' $TEMP_PATH/parsehub.json | while read d; do if [[ ${d:0:1} == "|" ]]; then echo '"Due_In" :"'$(( ($(date -d ${d#?} +%s) - $(date +%s) ) / 86400 ))'",'; else echo $d; fi; done > $TEMP_PATH/parsehub_edit.json
- _send_data "{\"state\": "$(grep -m 1 "Due_In" $TEMP_PATH/parsehub_edit.json | cut -d':' -f2)"\"attributes\": $(cat $TEMP_PATH/parsehub_edit.json), \"friendly_name\": \"$1\"}}" $BASE_URL$API_STATES_PATH/sensor.library_$2
- }
- # PRIVATE
- # Start a Parsehub Scraper
- # 1: API_Key for Parsehub
- # 2: Start URL for scraper
- # 3: Project ID
- # 4: Start values aka login credentials
- _parsehub_start_scrape() {
- curl -X POST \
- -d "api_key=$1" \
- -d "start_url=$2" \
- -d "start_value_override=$4" \
- -d "send_email=0" \
- "https://www.parsehub.com/api/v2/projects/$3/run"
- }
- # SEND TO HOME ASSISTANT
- # $1 = query
- # $2 = URL
- _send_data() {
- curl -X POST \
- -H "Accept: application/json" \
- -H "Authorization: Bearer $APITOKEN" \
- -H "Content-Type: application/json" \
- -d "$1" \
- $2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement