| 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 | /^[\(\)]/ { |
| 54 | update_node() |
| 55 | next |
| 56 | } |
| 57 | |
| 58 | /^A/ && $2 == "CDATA" { |
| 59 | update_node_attributes() |
| 60 | next |
| 61 | } |
| 62 | |
| 63 | /^-/ { |
| 64 | XmlPayload = substr($0, 2, length($0)) |
| 65 | } |
| 66 | |
| 67 | XmlPath == "/html/body/div/div/div/div/div/div/table/tr/td" && XmlPayload == location { |
| 68 | found = 1; |
| 69 | next |
| 70 | } |
| 71 | |
| 72 | XmlPath == "/html/body/div/div/div/div/div/div/table/tr/td" && found { |
| 73 | print XmlPayload; |
| 74 | found = 0; |
| 75 | next; |
| 76 | } |
| 77 | |
| 78 | function path_to_string(path, depth, p, i) { |
| 79 | p = "" |
| 80 | for (i = 1; i <= depth; i++) { |
| 81 | p = p "/" path[i] |
| 82 | } |
| 83 | return p |
| 84 | } |
| 85 | |
| 86 | function update_node( paren, name, key, val, path, attr) { |
| 87 | paren = substr($1, 1, 1) |
| 88 | name = substr($1, 2, length($1) - 1) |
| 89 | if (paren == "(") { |
| 90 | _depth++ |
| 91 | _path[_depth] = name |
| 92 | XmlPath = path_to_string(_path, _depth) |
| 93 | for (key in _hxpipe_curr_attrs) { |
| 94 | val = _hxpipe_curr_attrs[key] |
| 95 | XmlAttr[XmlPath, key] = val |
| 96 | } |
| 97 | } else if (paren == ")") { |
| 98 | delete _hxpipe_curr_attrs |
| 99 | XmlPayload = "" |
| 100 | for (key in XmlAttr) { |
| 101 | split(key, k, SUBSEP) |
| 102 | path = k[1] |
| 103 | attr = k[2] |
| 104 | if (path == XmlPath) delete XmlAttr[key] |
| 105 | } |
| 106 | _depth-- |
| 107 | XmlPath = path_to_string(_path, _depth) |
| 108 | } else { |
| 109 | printf("ERROR in input line %d - not a parenthesis: \"%s\"\n", NR, paren) > "/dev/stderr" |
| 110 | exit 1 |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | function update_node_attributes( key, val, s) { |
| 115 | key = substr($1, 2, length($1)) |
| 116 | val = $0 |
| 117 | s = " +" |
| 118 | sub("^" $1 s $2 s, "", val) |
| 119 | _hxpipe_curr_attrs[key] = val |
| 120 | } |
| 121 | ' |