# get cia factbook data

import urllib
import re
import sys

if len(sys.argv)==1:
    print 'please enter the country property to search for.'
    sys.exit(1)

keyw = ' '.join(sys.argv[1:]).lower()

wget = lambda url: urllib.urlopen( url ).read()

src = wget( 'http://www.cia.gov/cia/publications/factbook/index.html')

for chunk in src.split( '<option value="geos/')[1:]:
    code, chunk = chunk.split( '.html"   >',1)
    country, chunk = chunk.split( '</option', 1)
    data = wget( 'http://www.cia.gov/cia/publications/factbook/geos/%s.html' % code )
    p = data.lower().find( keyw)
    if p==-1: continue
    data = data[p:].split('<br>',1)[1].split('</td>',1)[0].strip()
    
    print "%s (%s): %s" % (country, code, data)
    



