Skip to content

Instantly share code, notes, and snippets.

@andrewshulgin
Last active January 21, 2017 12:03
Show Gist options
  • Save andrewshulgin/843f0382ab7cd6ef6169 to your computer and use it in GitHub Desktop.
Save andrewshulgin/843f0382ab7cd6ef6169 to your computer and use it in GitHub Desktop.
Script for querying remaining places for UZ
#!/usr/bin/env sh
# Script for querying remaining places from UZ
# Requires curl, grep, jq and nodejs
LANG="en"
DESCRIPTION="Script for querying remaining places from UZ"
USAGE="Usage: $(basename "${0}") <start_station> <end_station> [MM.DD.YYYY]"
DEPENDENCIES="Please make sure that curl, grep, jq and Node.JS are installed"
CURL=$(command -v "curl")
GREP=$(command -v "grep")
JQ=$(command -v "jq")
NODE=$(command -v "node")
SCRIPT=""
if [ "${1}" == "-h" -o "${1}" == "--help" -o -z "${1}" -o -z "${2}" ]; then
echo $DESCRIPTION
echo $USAGE
exit 1
fi
if [ -z "${3}" ]; then
DATE=$(date +"%m.%d.%Y")
else
DATE="${3}"
fi
if [ -z $CURL ] || [ -z $GREP ] || [ -z $JQ ] || [ -z $NODE ]; then
echo $DEPENDENCIES
exit 2
fi
INIT_RESPONSE=$(${CURL} -s -D - -o - 'http://booking.uz.gov.ua/')
function get_cookie {
echo $(echo "${INIT_RESPONSE}" | ${GREP} --colour=never -Pzo "_gv_sessid=.*;")
}
function get_token {
local js=$(echo "${INIT_RESPONSE}" | ${GREP} --colour=never -Pzo "\\$\\$\_=.*~\[\];.*\"\"\)\(\)\)\(\);")
local js_mod=$(echo "localStorage={setItem:function(key, value){if(key==='gv-token')console.info(value)}};${js}")
SCRIPT=$js_mod
echo $(echo "${js_mod}" | ${NODE})
}
function get_station_id {
echo $(${CURL} -s -o - "http://booking.uz.gov.ua/${LANG}/purchase/station/${1}/" | ${JQ} .value[0].station_id)
}
DATA="station_id_from=$(get_station_id $1)&station_id_till=$(get_station_id $2)&date_dep=${DATE}&time_dep=00:00&time_dep_till=24:00"
echo $DATA
${CURL} -s -o - "http://booking.uz.gov.ua/${LANG}/purchase/search/" \
-H "GV-Token: $(get_token)" \
-H "Cookie: $(get_cookie)" \
-H "GV-Ajax: 1" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Referer: http://booking.uz.gov.ua/${LANG}/" \
--data ${DATA} | ${JQ} .value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment