-
Notifications
You must be signed in to change notification settings - Fork 13
/
al_api_test.py
102 lines (90 loc) · 2.39 KB
/
al_api_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import requests
import json
import time
import os
import asyncio
import aiohttp
start = time.time()
session = requests.session()
http_timeout = 2
async def fx_for(symbol_to):
async with aiohttp.ClientSession() as async_session:
response = await async_session.get(
"https://www.alphavantage.co/query",
timeout=http_timeout,
params={
'function': 'CURRENCY_EXCHANGE_RATE',
'from_currency': 'USD',
'to_currency': symbol_to,
'apikey': ''
}
)
api_result = await response.json()
return api_result
symbol_list = ["KRW",
"EUR",
"CNY",
"JPY",
"XDR",
"MNT"]
loop = asyncio.get_event_loop()
futures = [fx_for(symbol_lists) for symbol_lists in symbol_list]
api_result = loop.run_until_complete(asyncio.gather(*futures))
result_real_fx = {
"USDUSD": 1.0,
"USDKRW": 1.0,
"USDEUR": 1.0,
"USDCNY": 1.0,
"USDJPY": 1.0,
"USDSDR": 1.0,
"USDMNT": 1.0
}
list_number = 0
for symbol in symbol_list:
if symbol == "XDR":
symbol = "SDR"
result_real_fx["USD"+symbol] = float(
api_result[list_number]["Realtime Currency Exchange Rate"]["5. Exchange Rate"])
list_number = list_number +1
print("time :", time.time() - start)
print(result_real_fx)
async def fx_for_free(symbol_to):
async with aiohttp.ClientSession() as async_session:
response = await async_session.get(
"https://api.exchangerate.host/latest",
timeout=http_timeout,
params={
'base': 'USD',
'symbols': symbol_to
}
)
api_result = await response.json()
return api_result
symbol_list = ["KRW",
"EUR",
"CNY",
"JPY",
"XDR",
"MNT"]
loop = asyncio.get_event_loop()
futures = [fx_for(symbol_lists) for symbol_lists in symbol_list]
api_result = loop.run_until_complete(asyncio.gather(*futures))
result_real_fx = {
"USDUSD": 1.0,
"USDKRW": 1.0,
"USDEUR": 1.0,
"USDCNY": 1.0,
"USDJPY": 1.0,
"USDSDR": 1.0,
"USDMNT": 1.0
}
list_number = 0
for symbol in symbol_list:
fx_symbol="USD"+symbol
if symbol == "XDR":
fx_symbol = "USDSDR"
result_real_fx[fx_symbol] = float(
api_result[list_number]["rates"][symbol])
list_number = list_number +1
print("time :", time.time() - start)
print(result_real_fx)