User:R. Hillgentleman/currencyCIA1.py

出自維基百科,自由嘅百科全書
Jump to navigation Jump to search
#currencyCIA.py
#TO GET CURRENCY INFORMATION FROM CIA WORLD FACT BOOK
#https://www.cia.gov/library/publications/the-world-factbook/geos/ch.html
#AT THE MOMENT, EXCHANGE RATE ONLY
# TO MATCH: <div align="right">Exchange rates:</div> -NOT NEEDED
# THEN MATCH: (yuan) per US dollar - 7.97 (2006)  -WHAT WE DO NOW
import urllib
import re
###SET REGEX ################################
exword=re.compile(r'(?i)exchange rate')
dollarword=re.compile(r'(?i)(?<=per us dollar - ).+\(2006\)')


#LIST OF COUNTRIES
countryList=['ch','cn','uz', 'uk','ja','ca']


def exchangeRate(countrycode):  

  ###OPEN FACTBOOK PAGE AT READ ALL THE CRAP####
  thePage='https://www.cia.gov/library/publications/the-world-factbook/geos/'+countrycode+'.html'
  noPage=False
  try:
    x=urllib.urlopen(thePage)
  except:
    print('No factbook for'+countrycode)
    noPage=True
  finally:
    print('...')
  if noPage: return('No data')  #NO SUCH PAGE! NEXT COUNTRY

  #PAGE OBJECT CREATED, NOW READ
  ciacrap = x.read()

  #NOW SEEK THE EXCHANGE RATE
  l = dollarword.findall(ciacrap)
  for i in l:
    exchangeRateString= i  #THERE SHOULD BE ONLY ONE ANYWAY
    print countrycode , exchangeRateString
    return exchangeRateString


for country in countryList:
 print(country+':'+ exchangeRate(country) +'\n')




"""
#####OLD CRAP###############
#LOOK FOR "EXCHANGE RATE"
n=0
wherefound=0
for line in x:
 l[n]=line
 junk, count= exword.subn('',line)
 if count >0:
   print line
   wherefound = n
   print('%d'%wherefound)
 n=n+1
print('Found at %d'+%wherefound)
"""