-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusd_khr.py
137 lines (123 loc) · 4.32 KB
/
usd_khr.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
# coding=UTF-8
import traceback
import locale
import requests
import utils
from bs4 import BeautifulSoup
BANK_INFOS = [
{"SWIFT": "NCAMKHPP",
"NAME": "NATIONAL BANK OF CAMBODIA",
"URL": "https://www.nbc.org.kh/english/economic_research/exchange_rate.php",
"XPATH": "//form[@id='fm-ex']/table/tbody/tr[2]/td/font",
"ENABLED": True,
"IMPLEMENTATION": "get_nbc"},
{"SWIFT": "",
"NAME": "AMK CAMBODIA",
"URL": "https://www.amkcambodia.com/foreign-exchange.html",
"XPATH": "//div[@id='content_body']/table/tbody/tr[2]/td[2]",
"ENABLED": True,
"IMPLEMENTATION": "get_amk"},
{"SWIFT": "ACLBKHPP",
"NAME": "ACLEDA BANK PLC.",
"URL": "https://www.acledabank.com.kh/kh/eng/ps_cmforeignexchange",
"XPATH": "//form[@name='frmdEx']/table/tbody/tr[2]/td[2]",
"ENABLED": True,
"IMPLEMENTATION": "get_acleda"},
{"SWIFT": "CADIKHPP",
"NAME": "CANADIA BANK PLC",
"URL": "https://www.canadiabank.com.kh/en/exchange_rate.aspx",
"XPATH": "//div[@id='menu']/table/tbody/tr/td[2]/div[2]/table/tbody/tr[2]/td[2]",
"ENABLED": True,
"IMPLEMENTATION": "get_canadia"},
{"SWIFT": "IDBCKHPP",
"NAME": "BANK FOR INVESTMENT AND DEVELOPMENT OF CAMBODIA PLC",
"URL": "https://www.bidc.com.kh/#tygia",
"XPATH": "//table[@id='tbl_100']/tbody/tr[8]/td[2]",
"ENABLED": True,
"IMPLEMENTATION": "get_idbc"}
]
def get_nbc(url):
try:
r = requests.get(url)
r.encoding = 'utf-8'
soup = BeautifulSoup(r.text, "html.parser")
table = soup.find(id="fm-ex")
rateKHRUSD = table.find_all("td")[1].text.split(":")[1].strip()
return rateKHRUSD.split(" ")[0]
except:
traceback.print_exc()
return 0
def get_amk(url):
try:
r = requests.get(url)
r.encoding = 'utf-8'
soup = BeautifulSoup(r.text, "html.parser")
table = soup.table
rateKHRUSD = table.find_all("tr")[1].find_all("td")[1].text
return rateKHRUSD.split(" ")[1]
except:
traceback.print_exc()
return 0
def get_acleda(url):
try:
r = requests.get(url)
r.encoding = 'utf-8'
soup = BeautifulSoup(r.text, "html.parser")
table = soup.table
tds = table.find_all("td")
for idx, elem in enumerate(tds):
if elem.text == "US Dollar (USD)":
next_elem = tds[idx+1]
if "KHR" in next_elem.text:
rateKHRUSD = next_elem.text.split(" ")[1]
return rateKHRUSD
except:
traceback.print_exc()
return 0
def get_canadia(url):
try:
r = requests.get(url)
r.encoding = 'utf-8'
soup = BeautifulSoup(r.text, "html.parser")
span = soup.find_all("span", id="lblKH_Buy")[0]
rateKHRUSD = span.text
return rateKHRUSD
except:
traceback.print_exc()
return 0
def get_idbc(url):
try:
r = requests.get(url)
r.encoding = 'utf-8'
soup = BeautifulSoup(r.text, "html.parser")
table = soup.find_all("table", id="tbl_100")[0]
tds = table.find_all("td")
for idx, td in enumerate(tds):
if td.text == "KHR/USD":
next_td = tds[idx+1]
rateKHRUSD = next_td.text
return rateKHRUSD
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")
print('url : {} / name : {}'.format(url, name))
fxrate = globals()[implementation](url)
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()