forked from DubhAd/Home-AssistantConfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-email
61 lines (51 loc) · 2.13 KB
/
parse-email
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
#!/usr/bin/zsh
LOGFILE=/data/homeassistant/email.log
BINFILE=/data/homeassistant/bin
SENDER=$(echo ${1}|tr -d '/'| sed "s/@/-a-/")
case ${SENDER} in
*-a-greenboxday.co.uk )
TDIR=$(mktemp --directory --suffix=.pe)
MASTER=${TDIR}/email.file.${SENDER}
cat > ${MASTER}
cat ${MASTER} | sed "1,/^$/d" | sed ':a;N;$!ba;s/=\n//g' | sed 's/<[^>]*>//g' > ${MASTER}.body
WHAT=$(cat ${MASTER}.body | grep " Bin Collection " | sed -e "s/.*relates to the forthcoming scheduled //" -e "s/ Bin Collection on .*//")
WHEN=$(date --date="$(cat ${MASTER}.body | grep " Bin Collection " | sed -e "s/.* Bin Collection on //" -e "s/ for POST CODE.*//" | sed "s/\([0-9]\)[a-z][a-z] /\1 /")" +"%F %T")
cat << EOM >> ${BINFILE}.${WHAT:l}.json
{ "collection_date":"${WHEN}", "collection_type":"${WHAT} Bin Collection","timestamp":"$(date +"%F %T")" }
EOM
;;
*-a-COUNCIL.gov.uk )
TDIR=$(mktemp --directory --suffix=.pe)
MASTER=${TDIR}/email.file.${SENDER}
cat > ${MASTER}
cat ${MASTER} | sed "1,/^$/d" | sed ':a;N;$!ba;s/=\n//g' | sed 's/<[^>]*>//g' | csplit -f ${TDIR}/xx - '/^----/' '{*}' >/dev/null 2>&1
EMAIL_DATE=$(cat ${MASTER} | grep "X-Apparently-To" | sed "s/.*; //")
WHAT=$(cat ${TDIR}/xx02 | grep "POST CODE" | sed -e "s/.* POST CODE. Your //" -e "s/ .*//")
WHEN=$(cat ${TDIR}/xx02 | grep "POST CODE" | sed -e "s/.*will be collected //" -e "s/.\s*$//" -e "s/\([0-9]\)[snrt][tdh]/\1/")
case ${WHEN} in
tomorrow* ) WHEN="$(date --date="tomorrow ${EMAIL_DATE}" +"%F 00:00:00")"
;;
* ) WHEN="$(echo "${WHEN}" | sed -e "s/^.*\([0-9]\)/\1" -e "s/\.$//")"
# If this is December, and that's January, we need to add a year
if [[ $(date +%m) -eq 12 && $(date --date="${WHEN}" +%m) -eq 01 ]]; then
WHEN=$"(date --date="${WHEN} year" +"%F %T")"
fi
;;
esac
cat << EOM >> ${BINFILE}.${WHAT:l}.json
{ "collection_date":"${WHEN}", "collection_type":"${WHAT} Bin Collection","timestamp":"$(date +"%F %T")" }
EOM
;;
* ) TDIR=$(mktemp --directory --suffix=.pe)
MASTER=${TDIR}/email.file.${SENDER}
cat > ${MASTER}
cat << EOM >> ${LOGFILE}
==========
${SENDER}
-----
$(cat ${MASTER})
----------
EOM
;;
esac
rm -rf ${TDIR}