summaryrefslogtreecommitdiffstats
path: root/god/meinsack/index
blob: 7046d78cb38a00223b2646121c1e3b0d81e5aa24 (plain)
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
#! /bin/sh
#
# //meinsack
#
# export plz
# export str
# export anr
# export ort
#
set -euf
cd $(dirname $(readlink -f $0))/.. # cd //

plz=${plz+"&plz=$plz"}
str=${str+"&str=$str"}
anr=${anr+"&anr=$anr"}

url="http://www.sita-deutschland.de/sita/ywbase_Abfallkalender_Stuttgart.nsf/frmSelect?ReadForm$plz$str$anr"

# echo $url

html="$(curl -fsS "$url" | iconv -f latin1 -t utf8)"

if echo "$html" | fgrep -q 'Ihre Eingabe war nicht eindeutig'; then
  {
    echo "Error: multiple locations found"
    echo "$html" |
    query/jquery/index 'table.tab:nth-child(2n) .bordertop > .text' |
    sed '
      s/[[:space:]]\+/ /g
      s/'"`printf '\xc2\xa0'`"'//g;# kill unicode non-breaking space
    ' |
    while read plz &&
          read str &&
          read ort &&
          read bes1 &&
          read bes2 &&
          read anr; do
      str=$(echo "$str"|sed 's/<[^>]*>//g')
      echo "export anr=$anr if you meant \"$ort\""
    done | sort | uniq
  } >&2
  exit 2
elif test $(echo "$html" | grep -c 'Abfallkalender für') -gt 1 &&
     test -z "${ort-}"; then
  {
    echo "Error: multiple locations found"
    echo "$html" |
    query/jquery/index '.tab span.text,.subheadline' |
    sed -n '
      s/[[:space:]]\+/ /g
      s/'"`printf '\xc2\xa0'`"'//g;# kill unicode non-breaking space
      s/^Abfallkalender.*[^0-9]\([0-9]\+\)[^(]*(\(.*\))/export ort=\2/p
    ' | sort | uniq
  } >&2
  exit 3
else
  result="$(echo "$html" |
  query/jquery/index '.tab span.text,.subheadline' | {
    if test -n "${ort-}"; then
      sed -n "
        /($ort)/,/^Abfallkalender/{p;b}
        /($ort)/,\${p;b}
      "
    else
      cat
    fi
  } | {
    grep -v Abfallkalender |
    sed '
      s/'"`printf '\xc2\xa0'`"'//g;# kill unicode non-breaking space
      s/^[A-Z][a-z]\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)$/\3-\2-\1/
      s/^[0-9].*/  "&T00:00:00.000Z"/
      $!s/$/,/
      1s/^/[\n/
      $s/$/\n]/
    '
  })"
  if ! echo "$result" | grep .; then
    {
      echo "Error: your query had no effect"
      echo "url=\"$url\""
    } >&2
    exit 4
  fi
fi