Skip to content

Instantly share code, notes, and snippets.

@ptantiku
Created March 29, 2014 18:15
Show Gist options
  • Save ptantiku/9859349 to your computer and use it in GitHub Desktop.
Save ptantiku/9859349 to your computer and use it in GitHub Desktop.
Pull temperature data from Thai Meteorological Department website, from $fromdate to $todate, filtering by name of the location.
#!/bin/bash
fromdate=$(date -d 2013-08-01 +"%Y%m%d")
todate=$(date +"%Y%m%d")
filter="สนามบินสุวรรณภูมิ"
for year in {2013..2014}
do
for month in {1..12}
do
for day in {1..31}
do
# if date possible
[ $(expr $year % 4) == 0 -a $day -gt 29 -a $month == 2 ] && continue
[ $(expr $year % 4) != 0 -a $day -gt 28 -a $month == 2 ] && continue
[ $day == 31 -a \( $month == 2 -o $month == 4 -o $month == 6 -o \
$month == 9 -o $month == 11 \) ] && continue
# if date in between from-to
monthstr=$(printf %02d $month)
daystr=$(printf %02d $day)
date=$year$monthstr$daystr
[ ! \( $date -ge $fromdate -a $date -le $todate \) ] && continue
#pulling data
curl -s -d "ddlDay=$day&ddlMonth=$year-$monthstr" http://www.tmd.go.th/climate/climate.php \
| iconv -f TIS620 -t UTF8 \
| grep -oe "\($filter\)[^R]*/TR" \
| sed -r -e 's%(</?T[RD][^>]*>?)+%,%g' -e 's/,$//' -e 's/ +,/,/g' \
| sed -r -e 's%<[^>]*>%%g' \
| sed -r -e 's/- ได้รับรายงานบางส่วน -/NULL,NULL,NULL/' \
| sed -r -e 's/- ยังไม่ได้รับรายงาน -/NULL,NULL,NULL,NULL,NULL,NULL,NULL/' \
| sed -r -e "s/^/$year-$monthstr-$daystr,/"
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment