-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusd_vnd.py
184 lines (169 loc) · 6.52 KB
/
usd_vnd.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# coding=UTF-8
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from pprint import pprint
from datetime import date, timedelta
from bs4 import BeautifulSoup
import os
import traceback
import locale
import requests
import utils
BANK_INFOS = [
{
"SWIFT": "STBVVNVXXXX",
"NAME": "State Bank of Viet Nam.",
"URL": "https://www.sbv.gov.vn/webcenter/portal/en/home/rm/er?centerWidth=80%25&leftWidth=20%25&rightWidth=0%25&showFooter=false&showHeader=false&_adf.ctrl-state=19izpkwweh_4&_afrLoop=8111483055743432#%40%3F_afrLoop%3D8111483055743432%26centerWidth%3D80%2525%26leftWidth%3D20%2525%26rightWidth%3D0%2525%26showFooter%3Dfalse%26showHeader%3Dfalse%26_adf.ctrl-state%3Dw3nxezlou_4",
"ENABLED": True,
"IMPLEMENTATION": "get_sbv"
},
{
"SWIFT": "HDBCVNVXXXX",
"NAME": "HO CHI MINH CITY DEVELOPMENT JOINT STOCK COMMERCIAL BANK.",
"URL": "https://www.hdbank.com.vn/?ArticleId=f7fb576a-fe3e-40ea-9a67-4b00a3f06742",
"ENABLED": True,
"IMPLEMENTATION": "get_hdb"
},
{
"SWIFT": "IDBCKHPPXXX",
"NAME": "BANK FOR INVESTMENT AND DEVELOPMENT OF CAMBODIA PLC.",
"URL": "https://www.bidc.com.kh/Default.aspx?po=2&p=85&nid=291#tygia",
"ENABLED": True,
"IMPLEMENTATION": "get_bidc"
},
{
"SWIFT": "ICBVVNVXXXX",
"NAME": "Vietnam Joint Stock Commercial Bank For Industry and Trade.",
"URL": "https://www.vietinbank.vn/web/home/en/doc/saving/exrate.html",
"ENABLED": True,
"IMPLEMENTATION": "get_vietin"
},
{
"SWIFT": "VBAAVNVX380",
"NAME": "Vietnam Bank for Agriculture and Rural Development.",
"URL": "http://agribank.com.vn/Layout/Pages/GetTyGia.aspx?date=13/7/2018&lang=2",
"ENABLED": False,
"IMPLEMENTATION": "get_agribank"
},
{
"SWIFT": "BFTVVNVXXXX",
"NAME": "JOINT STOCK COMMERCIAL BANK FOR FOREIGN TRADE OF VIETNAM.",
"URL": "https://www.vietcombank.com.vn/ExchangeRates/?lang=en",
"ENABLED": True,
"IMPLEMENTATION": "get_vietcom"
},
]
def get_sbv(url, bankInfo=None):
bankName = bankInfo["NAME"]
try:
r = requests.get(url)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "html.parser")
tds = soup.find_all("td")
for idx, td in enumerate(tds):
if td.text.strip().rstrip() == "United States Dollar":
next_td = tds[idx + 1]
rateVNDUSD = next_td.text
rateVNDUSD = locale.atof(rateVNDUSD.replace(",", ""))
return [(bankName, rateVNDUSD)]
except:
traceback.print_exc()
return [(bankName, 0)]
def get_hdb(url, bankInfo=None):
bankName = bankInfo["NAME"]
try:
driver = utils.create_chromedriver()
utils.get_with_retry(driver, url)
def get_text(dr):
elem = dr.find_element(By.CLASS_NAME, "borderBottomLeft")
return elem != None
WebDriverWait(driver, 10, 0.5).until(get_text)
content = driver.page_source
driver.quit()
soup = BeautifulSoup(content, "html.parser")
tds = soup.find_all("td")
for idx, td in enumerate(tds):
if td.text.strip().rstrip() == "USD(50,100)":
target_td = tds[idx + 2]
rateVNDUSD = target_td.text
rateVNDUSD = locale.atof(rateVNDUSD.replace(",", ""))
return [(bankName, rateVNDUSD)]
except:
traceback.print_exc()
return [(bankName, 0)]
def get_bidc(url, bankInfo=None):
bankName = bankInfo["NAME"]
try:
r = requests.get(url)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "html.parser")
tds = soup.find_all("td")
for idx, td in enumerate(tds):
if td.text.strip().rstrip() == "VND/USD":
target_td = tds[idx + 1]
rateVNDUSD = target_td.text
rateVNDUSD = locale.atof(rateVNDUSD.replace(",", ""))
return [(bankName, rateVNDUSD)]
except:
traceback.print_exc()
return [(bankName, 0)]
def get_vietin(url, bankInfo=None):
bankName = bankInfo["NAME"]
try:
r = requests.get(url)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "html.parser")
tds = soup.find_all("td")
for idx, td in enumerate(tds):
if td.text.strip().rstrip() == "USD":
target_td = tds[idx + 3]
rateVNDUSD = target_td.text
rateVNDUSD = locale.atof(rateVNDUSD.replace(",", ""))
return [(bankName, rateVNDUSD)]
except:
traceback.print_exc()
return [(bankName, 0)]
def get_agribank(url, bankInfo=None):
bankName = bankInfo["NAME"]
try:
pass
except:
traceback.print_exc()
return [(bankName, 0)]
def get_vietcom(url, bankInfo=None):
bankName = bankInfo["NAME"]
try:
r = requests.get(url)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "html.parser")
tds = soup.find_all("td")
for idx, td in enumerate(tds):
if td.text.strip().rstrip() == "US DOLLAR":
target_td = tds[idx + 2]
rateVNDUSD = target_td.text
rateVNDUSD = locale.atof(rateVNDUSD.replace(",", ""))
return [(bankName, rateVNDUSD)]
except:
traceback.print_exc()
return [(bankName, 0)]
def get_impl():
global BANK_INFOS
result = {}
for bankInfo in BANK_INFOS:
if bankInfo.get("ENABLED"):
implementation = bankInfo.get("IMPLEMENTATION")
url = bankInfo.get("URL")
rateInfo = globals()[implementation](url, bankInfo)
print(rateInfo)
for data in rateInfo:
name, fxrate = data
if fxrate == 0:
print("Partner : {} => CANNOT get price now ... please check !".format(name))
else:
print("Partner : {} / FxRate : {}".format(name, fxrate))
result[name] = fxrate
return result
pass
def get_current_forex_price():
return get_impl()