Relocate path to a variable
[covid-19-scrapers.git] / fetch-case-count-usa-ny
1 #! /bin/sh
2 #
3 # Dependencies:
4 # - curl
5 # - awk
6 # - hxpipe (packaged in html-xml-utils on Debian and Ubuntu)
7
8 VALID_LOCATIONS='
9 Albany
10 Broome
11 Delaware
12 Dutchess
13 Erie
14 Greene
15 Herkimer
16 Monroe
17 Montgomery
18 Nassau
19 Orange
20 Putnam
21 Rockland
22 Saratoga
23 Schenectady
24 Suffolk
25 Tioga
26 Tompkins
27 Ulster
28 Westchester
29 New York State (Outside of NYC)
30 New York City:
31 Total Positive Cases (Statewide)
32 '
33 DEFAULT_LOCATION='New York City:'
34
35 usage() {
36 printf "Usage: %s [LOCATION]\n" "$0"
37 printf '\n'
38 printf 'LOCATION ='
39 printf '%s\n' "$VALID_LOCATIONS"
40 printf "Default LOCATION:\n %s\n" "$DEFAULT_LOCATION"
41 exit 1
42 }
43
44 case "$1" in
45 '-h') usage;;
46 '' ) location="$DEFAULT_LOCATION";;
47 * ) location="$1";;
48 esac
49
50 curl 'https://health.ny.gov/diseases/communicable/coronavirus/' \
51 | hxpipe \
52 | awk -v location="$location" '
53 BEGIN {
54 target_path = "/html/body/div/div/div/div/div/div/table/tr/td"
55 }
56
57 /^[\(\)]/ {
58 update_node()
59 next
60 }
61
62 /^A/ && $2 == "CDATA" {
63 update_node_attributes()
64 next
65 }
66
67 /^-/ {
68 XmlPayload = substr($0, 2, length($0))
69 }
70
71 XmlPath == target_path && XmlPayload == location {
72 found = 1;
73 next
74 }
75
76 XmlPath == target_path && found {
77 print XmlPayload;
78 found = 0;
79 next;
80 }
81
82 function path_to_string(path, depth, p, i) {
83 p = ""
84 for (i = 1; i <= depth; i++) {
85 p = p "/" path[i]
86 }
87 return p
88 }
89
90 function update_node( paren, name, key, val, path, attr) {
91 paren = substr($1, 1, 1)
92 name = substr($1, 2, length($1) - 1)
93 if (paren == "(") {
94 _depth++
95 _path[_depth] = name
96 XmlPath = path_to_string(_path, _depth)
97 for (key in _hxpipe_curr_attrs) {
98 val = _hxpipe_curr_attrs[key]
99 XmlAttr[XmlPath, key] = val
100 }
101 } else if (paren == ")") {
102 delete _hxpipe_curr_attrs
103 XmlPayload = ""
104 for (key in XmlAttr) {
105 split(key, k, SUBSEP)
106 path = k[1]
107 attr = k[2]
108 if (path == XmlPath) delete XmlAttr[key]
109 }
110 _depth--
111 XmlPath = path_to_string(_path, _depth)
112 } else {
113 printf("ERROR in input line %d - not a parenthesis: \"%s\"\n", NR, paren) > "/dev/stderr"
114 exit 1
115 }
116 }
117
118 function update_node_attributes( key, val, s) {
119 key = substr($1, 2, length($1))
120 val = $0
121 s = " +"
122 sub("^" $1 s $2 s, "", val)
123 _hxpipe_curr_attrs[key] = val
124 }
125 '
This page took 0.047136 seconds and 4 git commands to generate.