def get_rate(self, time):
# Where USD is the base currency you want to use
# 我的需求是获取美元对印尼币的汇率,我存在了自己建立的一张表中,此方法效果是判断我调用的时候传入的时间,如果和表中的
一致,就直接用数据库表中的,如果不一致就调用api获取最新的汇率,保证一天只请求一次api,因为这个免费的api是需要收费
的,一个月只能请求1000次,
url = 'https://v3.exchangerate-api.com/bulk/fa192ccf2d553fe2b4005125/USD'
# Making our request
try:
sql = "SELECT rate, savetime from JD_rate"
self.cur_test.execute(sql)
result_a = self.cur_test.fetchone()[0]
rate = result_a[0]
savetime = result_a[1].encode("utf-8")[:11]
if savetime != time:
response = requests.get(url)
rate = response.json()['rates']['IDR']
sql1 = "UPDATE JD_rate set rate='%s', savetime='%s' WHERE id = 1" % (rate, time)
self.cur_test.execute(sql1)
self.coon_test.commit()
return rate
except Exception, ex:
traceback.print_exc()
print ex, "get_rate"
The Reliable Currency Conversion API 这是我找到的获取实时汇率的api地址,每小时更新