-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
executable file
·135 lines (119 loc) · 4.01 KB
/
api.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
from __future__ import unicode_literals
import json
from datetime import date
import locale
import frappe
import random
import string
import requests
import pandas as pd
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
def random_string(string_length=8):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(string_length))
@frappe.whitelist()
def get_exchange_rate(**args):
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
exchange_date = args.get('exchange_date')
from_currency = args.get('from_currency')
to_currency = args.get('to_currency')
yyyymmdd = exchange_date[0:10]
print(yyyymmdd)
yyyy = exchange_date[0:4]
mm = exchange_date[5:7]
dd = exchange_date[8:10]
print(yyyy + mm + dd)
url1 = 'https://www.kebhana.com/cms/rate/wpfxd651_01i_01.do'
data = {
'ajax': 'true',
'curCd': from_currency,
'tmpInqStrDt': yyyymmdd,
'pbldDvCd': '1',
'pbldSqn': '',
'inqStrDt': yyyy + mm + dd,
'inqKindCd': '1',
'requestTarget': 'searchContentDiv'
}
res1 = requests.post(url1, data=data)
html = res1.content
soup = BeautifulSoup(html, 'lxml')
exchange_rate = 0.0
list_td = soup.find_all(name="td", attrs={"class": "txtAr"})
exchange_rate = list_td[8].text.strip()
if locale.atof(exchange_rate) > 0:
exchange_doc = frappe.new_doc('Currency Exchange')
exchange_doc.date = exchange_date
exchange_doc.from_currency = from_currency
exchange_doc.to_currency = to_currency
if from_currency == "JPY" or from_currency == "VND" or from_currency == "IDR":
exchange_doc.exchange_rate = locale.atof(exchange_rate)/100
else:
exchange_doc.exchange_rate = locale.atof(exchange_rate)
exchange_doc.for_buying = 1
exchange_doc.for_selling = 1
# create_exchange_rate(yyyymmdd, currency_cd, exchange_rate)
# for td_tag in list_td:
# print(td_tag.text.strip())
# exchange_rate = td_tag.text.strip()
print(args)
print(exchange_doc)
return exchange_doc
@frappe.whitelist()
def get_exchange_rate_all(**args):
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
exchange_date = args.get('exchange_date')
list_currency = ['USD', 'JPY', 'EUR', 'CNY', 'HKD', 'THB', 'TWD', 'PHP', 'SGD', 'AUD', 'VND', 'GBP', 'CAD', 'MYR',
'RUB', 'ZAR', 'NOK', 'NZD', 'DKK', 'MXN', 'MNT', 'BHD', 'BDT', 'BRL', 'BND', 'SAR', 'LKR', 'SEK',
'CHF', 'AED', 'DZD', 'OMR', 'JOD', 'ILS', 'EGP', 'INR', 'IDR', 'CZK', 'CLP', 'KZT', 'QAR', 'KES',
'COP', 'KWD', 'TZS', 'TRY', 'PKR', 'PLN', 'HUF']
yyyymmdd = exchange_date[0:10]
yyyy = exchange_date[0:4]
mm = exchange_date[5:7]
dd = exchange_date[8:10]
for currency_cd in list_currency:
url1 = 'https://www.kebhana.com/cms/rate/wpfxd651_01i_01.do'
data = {
'ajax': 'true',
'curCd': currency_cd,
'tmpInqStrDt': yyyymmdd,
'pbldDvCd': '1',
'pbldSqn': '',
'inqStrDt': yyyy + mm + dd,
'inqKindCd': '1',
'requestTarget': 'searchContentDiv'
}
res1 = requests.post(url1, data=data)
html = res1.content
soup = BeautifulSoup(html, 'lxml')
exchange_rate = 0.0
list_td = soup.find_all(name="td", attrs={"class": "txtAr"})
exchange_rate = list_td[8].text.strip()
if locale.atof(exchange_rate) > 0:
create_exchange_rate(yyyymmdd, currency_cd, exchange_rate)
# for td_tag in list_td:
# print(td_tag.text.strip())
# exchange_rate = td_tag.text.strip()
print(args)
return True
def create_exchange_rate(curr_date , currency_cd , mrate):
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
ex_exists = frappe.db.exists({
'doctype': 'Currency Exchange',
'date': curr_date,
'from_currency': currency_cd,
'to_currency': 'KRW'
})
if not ex_exists:
print("create " + curr_date + " " + currency_cd + " " + mrate)
exchange_doc = frappe.new_doc('Currency Exchange')
exchange_doc.date = curr_date
exchange_doc.from_currency = currency_cd
exchange_doc.to_currency = "KRW"
if currency_cd == "JPY" or currency_cd == "VND" or currency_cd == "IDR":
exchange_doc.exchange_rate = locale.atof(mrate)/100
else:
exchange_doc.exchange_rate = mrate
exchange_doc.for_buying = 1
exchange_doc.for_selling = 1
exchange_doc.insert()