-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproof-of-concept.sh
executable file
·74 lines (56 loc) · 2.47 KB
/
proof-of-concept.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# This is a prototype to use for working out the method of retrieving data
# from the State of California PeMS website.
#
# Copyright Brian High (https://github.com/brianhigh) and Surakshya Dhakal
# License: GNU GPL v3 http://www.gnu.org/licenses/gpl.txt
# --------------------------------------------------------------------------
# Configuration
# --------------------------------------------------------------------------
# Data folder configuration - where the data files are to be stored
DATA='data'
# Session configuration - variables used to set up the HTTP session
USERNAME='[email protected]'
PASSWORD='s3kr!t'
BASEURL='http://pems.dot.ca.gov'
USERAGENT='Mozilla/5.0' # https://en.wikipedia.org/wiki/User_agent
COOKIES='cookies.txt' # https://en.wikipedia.org/wiki/HTTP_cookie
# Page configuration - query specification for type of report page
FORM='1'
NODE='Freeway'
CONTENT='detector_health'
EXPORT='text'
# Combine variables into a "page" (PG) string
PG="report_form=${FORM}&dnode=${NODE}&content=${CONTENT}&export=${EXPORT}"
# Lanes configuration - specific freeway and direction to query
FWY='1'
DIR='N'
# Combine variables into a "lanes" (LN) string
LN="fwy=${FWY}&dir=${DIR}"
# Start date configuration - date for (beginning of) query (date or range)
MONTH='02'
DAY='05'
YEAR='2016'
# Combine variables into a "start date" (SDATE) string
SDATE="${MONTH}%2F${DAY}%2F${YEAR}"
# There is no end date in this sample query.
# --------------------------------------------------------------------------
# Remove old cookie file
rm -f "$COOKIES"
# Create data directory
mkdir -p "$DATA"
# Visit home page to get cookie
curl -o "${DATA}/freeways-and-forms.html" \
"$BASEURL" -c "$COOKIES" -A "$USERAGENT" \
--data "redirect=&username=${USERNAME}&password=${PASSWORD}&login=Login"
# Find "freeway" choices in HTML select option tags and write to CSV file.
echo '"fwy","dir","name"' > "${DATA}/freeways.csv" # Write the header first.
perl -wnl -e \
's/.*\/\?dnode=Freeway\&.*fwy=(.*)\&.*=(.*)">(.*)<.*/$1,$2,$3/g and print' \
"${DATA}/freeways-and-forms.html" >> "${DATA}/freeways.csv"
# Calculate the s_time_id (start time ID) as integer (unix epoch timestamp)
UDATE=$(date -u -d "${YEAR}-${MONTH}-${DAY}" "+%s")
# Get the TSV file for the detector_health for chosen freeway and date
curl -o "${DATA}/${NODE}-${CONTENT}-${FWY}-${DIR}-${YEAR}${MONTH}${DAY}.tsv" \
-b "$COOKIES" -A "$USERAGENT" \
"${BASEURL}/?${PG}&${LN}&s_time_id=${UDATE}&s_time_id_f=${SDATE}"