-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·90 lines (76 loc) · 1.86 KB
/
entrypoint.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
#
# Calls hobo-scrape.js to download data for each of the passed stations
#
# Syntax:
# entrypoint.sh <output directory or URL> <station id> ...
#
# Seconds to sleep between scrapes when in daemon mode
FETCH_DELAY=${FETCH_DELAY:-300}
# set to --silent to quiesce all messages from cURL
CURL_SILENT=${CURL_SILENT:-}
CURL_TIMEOUT=${CURLTIMEOUT:-2}
# Defaults
daemon_mode=false
data_dir=./data
usage() {
echo "entrypoint.sh [-d] [-p <url>] [-o <directory>] <station id>..."
exit 1
}
scrape() {
local data_dir=$1
shift
# Scrape the website and download the data.
# Do this for each station id given as parameters
for station_id in $*; do
echo "Launch virtual browser to fetch [${station_id}] to [${data_dir}]..."
casperjs hobo-scrape.js "${data_dir}" "${station_id}"
#rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi
done
}
post() {
local data_dir=$1
local post_url=$2
# If POST_URL was specified, upload all the .csv files there
for f in ${data_dir}/*.csv; do
if [ -f $f ]; then
echo "Post [$f] to [${post_url}]..."
curl ${CURL_SILENT} --connect-timeout ${CURL_TIMEOUT} --form file=@$f ${post_url} > /dev/null
#rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi
fi
done
}
# Parse command line options
while getopts ":hdp:o:" arg; do
case $arg in
h) usage
;;
d) daemon_mode=true
;;
p) post_url=${OPTARG}
;;
o) data_dir=${OPTARG}
;;
*) break
;;
esac
done
shift $((OPTIND-1))
# Verify station ids were passed on command line.
# Otherwise there's nothing to do.
if [ $# -le 0 ]; then
echo "No sensor stations specified."
usage
fi
while true; do
scrape "${data_dir}" $*
# Send the data to it's final destination
if [ ! -z ${post_url+x} ] ; then
post "${data_dir}" "${post_url}"
fi
if [ ${daemon_mode} == "false" ]; then
break;
fi
echo "Waiting for ${FETCH_DELAY} seconds before fetching again..."
sleep ${FETCH_DELAY}
done