-
Notifications
You must be signed in to change notification settings - Fork 0
/
bonbast.ts
152 lines (141 loc) · 4.13 KB
/
bonbast.ts
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
import { DOMParser } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
const BASE_URL = "https://bonbast.com";
const headers = new Headers({
Host: "bonbast.com",
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0",
Accept: "application/json, text/javascript, */*; q=0.01",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With": "XMLHttpRequest",
"Content-Length": "68",
Origin: "https://bonbast.com",
Connection: "keep-alive",
Referer: "https://bonbast.com/",
Cookie: "cookieconsent_status=true; st_bb=0",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
TE: "trailers",
});
const CURRENCIES = {
usd: "US Dollar",
eur: "Euro",
gbp: "British Pound",
chf: "Swiss Franc",
cad: "Canadian Dollar",
aud: "Australian Dollar",
sek: "Swedish Krona",
nok: "Norwegian Krone",
rub: "Russian Ruble",
thb: "Thai Baht",
sgd: "Singapore Dollar",
hkd: "Hong Kong Dollar",
azn: "Azerbaijani Manat",
amd: "10 Armenian Dram",
dkk: "Danish Krone",
aed: "UAE Dirham",
jpy: "10 Japanese Yen",
try: "Turkish Lira",
cny: "Chinese Yuan",
sar: "Saudi Riyal",
inr: "Indian Rupee",
myr: "Malaysian Ringgit",
afn: "Afghan Afghani",
kwd: "Kuwaiti Dinar",
iqd: "100 Iraqi Dinar",
bhd: "Bahraini Dinar",
omr: "Omani Rial",
qar: "Qatari Rial",
} as const;
const COINS = {
azadi1: "Azadi",
emami1: "Emami",
azadi1_2: "½ Azadi",
azadi1_4: "¼ Azadi",
azadi1g: "Gerami",
} as const;
type CurrencyType = keyof typeof CURRENCIES;
type CoinType = keyof typeof COINS;
const SELL = 1;
const BUY = 2;
async function getToken(): Promise<string | undefined> {
try {
const res = await fetch(BASE_URL, { headers });
const html = await res.text();
const document = new DOMParser().parseFromString(
html,
"text/html",
)?.textContent;
const extraction = document?.match(/param\s*[=:]\s*\"(.+)\"/);
return extraction![1];
} catch (error) {
console.error(error);
}
}
export type CurrencyListType = {
currency: (typeof CURRENCIES)[CurrencyType];
buy: string;
sell: string;
};
export type CoinListType = {
coin: (typeof COINS)[CoinType];
buy: string;
sell: string;
};
export async function getCurrenciesAndCoins(): Promise<
{ currencyList: CurrencyListType[]; coinList: CoinListType[] } | undefined
> {
try {
const token = await getToken();
if (token) {
const body = `param=${token}`;
const request = new Request(BASE_URL + "/json", {
headers,
method: "post",
body,
});
return fetch(request)
.then((response) => {
return response.json();
})
.then((data: Record<string, string>) => {
const currencyList: CurrencyListType[] = [];
const coinList: CoinListType[] = [];
for (const currency of Object.keys(CURRENCIES)) {
const sellValue = data[`${currency}${SELL}` as CurrencyType]
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
const buyValue = data[`${currency}${BUY}` as CurrencyType]
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
const currentCurrency = {
currency: CURRENCIES[currency as CurrencyType],
buy: buyValue,
sell: sellValue,
};
currencyList.push(currentCurrency);
}
for (const coin of Object.keys(COINS)) {
const sellValue = data[`${coin}`]
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
const buyValue = data[`${coin}${BUY}`]
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
const currentCoin = {
coin: COINS[coin as CoinType],
buy: buyValue,
sell: sellValue,
};
coinList.push(currentCoin);
}
return { currencyList, coinList };
});
}
} catch (error) {
console.error(error);
return undefined;
}
}