-
Notifications
You must be signed in to change notification settings - Fork 1
/
usd_thb.py
170 lines (154 loc) · 6.01 KB
/
usd_thb.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
# 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
import traceback
import locale
import requests
import utils
from bs4 import BeautifulSoup
BANK_INFOS = [{"SWIFT": "BOTHTHBK",
"NAME": "BANK OF THAILAND",
"URL": "https://www.bot.or.th/english/_layouts/application/exchangerate/exchangerate.aspx",
"XPATH": "//table[@id='ctl00_PlaceHolderMain_dgAvg']/tbody/tr[4]/td[5]",
"ENABLED": False,
"IMPLEMENTATION": "get_bot"},
{"SWIFT": "BKKBTHBK",
"NAME": "BANGKOK BANK PUBLIC COMPANY LIMITED",
"URL": "https://www.bangkokbank.com/en/Personal/Other-Services/View-Rates/Foreign-Exchange-Rates",
"XPATH": "//table[@class='table-primary table-foreign-exchange-rates blue']",
"ENABLED": False,
"IMPLEMENTATION": "get_bkb"},
{"SWIFT": "KASITHBK",
"NAME": "KASIKORNBANK PUBLIC COMPANY LIMITED",
"URL": "https://www.kasikornbank.com/en/rate/Pages/Foreign-Exchange.aspx",
"XPATH": "//table[@id='table-exchangerate']/tbody/tr[5]/td[4]",
"ENABLED": False,
"IMPLEMENTATION": "get_kasikorn"},
{"SWIFT": "AYUDTHBK",
"NAME": "BANK OF AYUDHYA PUBLIC COMPANY LIMITED",
"URL": "https://www.krungsri.com/bank/en/Other/ExchangeRate/Todayrates.html",
"XPATH": "//div[@id='tab_content_1']/div/div[3]/table/tbody/tr/td[8]",
"ENABLED": False,
"IMPLEMENTATION": "get_krungsri"},
{"SWIFT": "SICOTHBK",
"NAME": "SIAM COMMERCIAL BANK PCL., THE",
"URL": "http://www.scb.co.th/en/personal-banking/foreign-exchange-rates.html#fxrate",
"XPATH": "//*[@id='fxrate']/div/div[2]/div/table/tbody/tr[5]/td[4]",
"ENABLED": True,
"IMPLEMENTATION": "get_scb"}
]
def get_bot(url, bankInfo=None):
try:
r = requests.get(url)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "html.parser")
table = soup.find("table", id='ctl00_PlaceHolderMain_dgAvg')
tds = table.find_all("td")
for idx, elem in enumerate(tds):
if elem.text.startswith("USD"):
target = tds[idx+2]
rateUSDTHB = target.text
return rateUSDTHB
except:
traceback.print_exc()
return 0
def get_bkb(url, bankInfo=None):
try:
driver = utils.create_chromedriver()
xpath = bankInfo["XPATH"]
name = bankInfo["NAME"]
if not xpath:
print("[WARNING] Cannot find FxRate from {}".format(name))
return 0
utils.get_with_retry(driver, url)
def get_text(dr):
elem = dr.find_element(By.XPATH, xpath)
return elem is not None
WebDriverWait(driver, 10, 0.5).until(get_text)
xpath2 = xpath + '/tbody/tr[3]/td[5]'
elem = driver.find_element_by_xpath(xpath2)
print(elem)
fxrate = elem.text
fxrate = fxrate.replace(",", "")
driver.quit()
return fxrate
except:
traceback.print_exc()
return 0
def get_kasikorn(url, bankInfo=None):
try:
r = requests.get(url)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "html.parser")
divs = soup.find_all("div", {'class': 'itemsRate'})
for div in divs:
if div.attrs.get("data-sname", "") == "USD 50-100":
fxrate = div.attrs.get("data-buytelex", "0")
fxrate = fxrate.replace(",", "")
return fxrate
except:
traceback.print_exc()
return 0
def get_krungsri(url, bankInfo=None):
try:
r = requests.get(url)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "html.parser")
span = soup.find("span", id="p_lt_ctl19_ksP_p_lt_ctl05_ExchangeRate_tableCurrent")
trs = span.find_all("tr")
for tr in trs:
tds = tr.find_all('td')
for idx, td in enumerate(tds):
if td.text.strip() == "USD":
next_td = tds[idx+1]
if int(next_td.text) == 1:
target_td = tds[idx+5]
fxrate = target_td.text
fxrate = fxrate.replace(",", "")
return fxrate
except:
traceback.print_exc()
return 0
def get_scb(url, bankInfo=None):
try:
driver = utils.create_chromedriver()
xpath = bankInfo["XPATH"]
name = bankInfo["NAME"]
if not xpath:
print("[WARNING] Cannot find FxRate from {}".format(name))
return 0
utils.get_with_retry(driver, url)
def get_text(dr):
elem = dr.find_element(By.XPATH, xpath)
return elem is not None
WebDriverWait(driver, 10, 0.5).until(get_text)
elem = driver.find_element_by_xpath(xpath)
fxrate = elem.text
fxrate = fxrate.replace(",", "")
driver.quit()
return fxrate
except:
traceback.print_exc()
return 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")
name = bankInfo.get("NAME")
fxrate = globals()[implementation](url, bankInfo)
if not fxrate:
fxrate = "0"
print("Partner : {} => CANNOT get price now ... please check !".format(name))
else:
fxrate = locale.atof(fxrate.replace(",", ""))
print("Partner : {} / FxRate : {}".format(name, fxrate))
result[name] = fxrate
return result
pass
def get_current_forex_price():
return get_impl()