Wednesday, December 9, 2009

[How-to] dump yahoo weather city location codes for US

Here is the bash script to dump the location codes from weather.yahooapis.com

getcityloccode.sh Select all


#/bin/bash
for a in AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY AS GU MP PR VI FM MH PW AA AE AP CZ PI TT CM ; do
countblank=0
for h in 0 1 2 3 4 5 6 7 8 9 ; do
for i in 0 1 2 3 4 5 6 7 8 9 ; do
for j in 0 1 2 3 4 5 6 7 8 9 ; do
for k in 0 1 2 3 4 5 6 7 8 9 ; do
locname=`curl -s -L "http://weather.yahooapis.com/forecastrss?p=US${a}${h}${i}${j}${k}&u=f" | grep "Yahoo! Weather for" | sed -e "s/\(^.*Yahoo! Weather for \)\(.*, ${a}\)\(.*\)/\2/"`
if [ "${locname}" != "" ] ; then
  echo US${a}${h}${i}${j}${k} $locname
  countblank=0
else
  countblank=$[$countblank+1]
  if [ $countblank -gt 5 ] ; then
    break 4
  fi
fi
done
done
done
done
done


and run it with
./getcityloccode.sh > cityloccodes.txt &

The results are

USAL0001 Abbeville, AL
USAL0002 Abernant, AL
USAL0003 Adamsville, AL
USAL0004 Addison, AL
USAL0005 Adger, AL
USAL0006 Akron, AL
USAL0007 Alabaster, AL
USAL0008 Alberta, AL
USAL0009 Albertville, AL
USAL0010 Alexander City, AL
...






1 comment:

Darren said...

awesome! just what i was looking for. thanks