#!/usr/bin/python #!coding=utf8 import urllib2 from TextUtil import TextUtil from HttpUtil import HttpUtil class Currency: def __init__(self,cname,ccode,cusdrate): self.name=cname self.code=ccode self.usdrate=cusdrate def getName(self): return self.name def getCode(self): return self.code def getUsdRate(self): return self.cusdrate def toString(self): return self.name+"/t"+self.code+"/t"+self.usdrate class GetLatestCurrency: def __init__(self): self.currencylist=[] def __extractcurrency__(self): home_url="http://www.iccfx.com" h=urllib2.urlopen(home_url) home_data=h.read() #print home_data h.close() textUtil=TextUtil(home_data) self.cmap={} while textUtil.selectText('OPTION>','<'): value=textUtil.extractText() inf=value.find("(") ine=value.find(")",inf) name=value[:inf].strip() code=value[inf+1:ine].strip() self.cmap.update({code:name}) def __convertcurrency__(self): from_c="US Dollar ".replace(" ","+")+urllib2.quote("(")+"USD"+urllib2.quote(")") for key in self.cmap: key=self.cmap.get(key).replace(" ","+")+"+"+urllib2.quote("(")+key+urllib2.quote(")") if key=="US Dollar (USD)": continue headers={ "User-Agent":"Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110221 Ubuntu/10.10 (maverick) Firefox/3.6.14", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-us;en;q=0.5", "Accept-Encoding": "gzip,deflate", "Accept-Charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.7", "Content-Type":"application/x-www-form-urlencoded" } util=HttpUtil("www.iccfx.com","POST","/convert.php","showall=&to="+key+"&amount=1&from="+from_c,headers) data=util.fetch() textUtil=TextUtil(data) if textUtil.selectText('<B>1</B> US Dollar (USD) = <B>','</B>'): value=textUtil.extractText() cur=Currency(self.cmap.get(key),key,value) self.currencylist.append(cur) def downloadCurrencyFromWeb(self): self.__extractcurrency__() self.__convertcurrency__() def toString(self): _buffer="" for key,value in self.cmap.items(): _buffer+=key+"/t"+value+"/r/n" return _buffer def toStringWithUsdRate(self): _buffer="" for cur in self.currencylist: _buffer+=cur.getName()+"/t"+cur.getCode()+"/t"+str(cur.getUsdRate())+"/r/n" return _buffer def main(): g=GetLatestCurrency() g.downloadCurrencyFromWeb() print g.toStringWithUsdRate() #print "-----------------------" #print g.toString() if __name__=="__main__": main()