跳到主要内容

1 篇博文 含有标签「全球汇率」

查看所有标签

· 阅读需 1 分钟
forkp

Python 代码如下

import asyncio
import aiohttp
import json
import redis
from urllib.parse import urlparse, parse_qs

s = {}

def cache(db=0):
return redis.Redis(host='127.0.0.1', port=6379, decode_responses=True,db=db)

async def fetch(session, url):
async with session.get(url,ssl=False) as response:
parsed_url = urlparse(url)
query_params = parse_qs(parsed_url.query)
from_money_value = query_params.get('from_money', [''])[0]
k = await response.text()
kd = json.loads(k)
money_value = {"money1":find_key_value(kd, 'money1') or [],"money2":find_key_value(kd, 'money2') or [],"money2_num": find_key_value(kd, 'money2_num') or [],"money2_rev":find_key_value(kd, 'money2_rev') or []}
s[from_money_value] = money_value
money_value = json.dumps(money_value)
k_s = "laravel_database_money:{}".format(from_money_value)
cache(db=1).set(k_s,money_value)
return k

async def main():
url_cny_usd = 'https://sp0.baidu.com/5LMDcjW6BwF3otqbppnN2DJv/finance.pae.baidu.com/vapi/async/v1?from_money=CNY&to_money=USD&srcid=5293'
async with aiohttp.ClientSession() as session:
cny_usd_response = await fetch(session, url_cny_usd)
data = json.loads(cny_usd_response)
money_list = find_key_value(data, 'money_list') or []
currency_codes = [item[1].split(" ")[1] for item in money_list if isinstance(item, list) and len(item) > 0]
urls = [f'https://sp0.baidu.com/5LMDcjW6BwF3otqbppnN2DJv/finance.pae.baidu.com/vapi/async/v1?from_money={code}&to_money=USD&srcid=5293' for code in currency_codes]

tasks = [fetch(session, url) for url in urls]
responses = await asyncio.gather(*tasks)
k_s = "laravel_database_money_all"
cache(db=1).set(k_s,json.dumps(s))

def find_key_value(nested_item, key):
if isinstance(nested_item, dict):
if key in nested_item:
return nested_item[key]
for v in nested_item.values():
found_value = find_key_value(v, key)
if found_value is not None:
return found_value
elif isinstance(nested_item, list):
for item in nested_item:
found_value = find_key_value(item, key)
if found_value is not None:
return found_value
return None


asyncio.run(main())