-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoppingcurrencies.ahk
313 lines (300 loc) · 13.5 KB
/
shoppingcurrencies.ahk
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
SetWorkingDir A_WorkingDir
#Include "<select toolbox>"
DetectHiddenWindows 0
utcdif := DateDiff(A_Now, A_NowUTC, "H")
; moddate := dateadd(FileGetTime(A_ScriptName, "M"),-utcdif,"h")
moddate := 20240517000000
moddate := dateadd(moddate, -utcdif, "h")
repoupdate := 0
A_TrayMenu.add("Converter", (*) => convgui.show())
A_TrayMenu.add("Settings", (*) => settingsgui.show())
A_TrayMenu.default := "Converter"
A_TrayMenu.ClickCount := 1
convgui := Gui()
convgui.SetFont("s18")
convgui.AddText(, "From Currency")
convgui.AddDropDownList("vfromCur ys x250 w120").OnEvent("Change", calculateresult)
convgui.AddEdit("vfromVal xm w350").OnEvent("Change", calculateresult)
convgui.AddText("xm voverheadtext section", "Overhead Mode")
convgui.AddDropDownList("vOverhead yp x250 w120 choose1", ["Convert", "Shipping", "Traveler", "T+T"])
.OnEvent("Change", calculateresult)
convgui.AddComboBox("vtoVal xm w350")
(statusbar := convgui.AddStatusBar('vStatus')).SetParts(20, 20, 240)
statusbar.SetIcon(A_WinDir "\System32\" "dsuiext.dll", 36)
statusbar.SetIcon(A_WinDir "\System32\" "comres.dll", 7, 3)
statusbar.SetIcon("lib\youtube.png", , 2)
statusbar.OnEvent("Click", (obj, info) =>
info = 1 ? (settingsgui.Show(), convgui.Hide()) :
info = 2 ? run("https://youtu.be/qP5hBoRbKWc") :
info = 3 ? editcurgui.Show() : "")
InitiateYml() {
global
SettingsYml := Yaml("settings.yml")[1]
StoresYml := Yaml("stores.yml")[1]
baseCurrency := SettingsYml["Base"], intCurrency := SettingsYml["INT"]
if !FileExist("currency.json") or !instr(FileGetTime("currency.json", "M"), A_Year A_mon A_DD)
or InStr(FileRead("currency.json"), baseCurrency)
try {
Download("http://www.floatrates.com/daily/" baseCurrency ".json", "currency.json")
repoupdate := regexreplace(Yaml(
httprequest('https://api.github.com/repos/enaaab460/ShoppingCurrenciesAHK/branches/master2'))
['commit']['commit']['author']['date'], "\D")
}
catch
MsgBox("Failed to connect to online sources`nExiting Program", , "Icon!"), ExitApp()
curfile := FileRead('currency.json')
for i, match in RegExMatchAll(curfile, '([\d.]+)e-(\d)')
curfile := StrReplace(curfile, match[0], Format('{:.13f}', (match[1]) / (10 ** match[2])))
currencyjson := Yaml(curfile)
baseCurrency = intCurrency ? currencyjson[Strlower(intCurrency)] := Map("inverseRate", 1) : ""
bankusd := round(currencyjson[Strlower(intCurrency)]["inverseRate"], 2)
; altusd := SettingsYml["Alt_$"] > 0 ? SettingsYml["Alt_$"] : 0
if ( SettingsYml["Alt_$"] = "auto" ) {
try{
sitecom := gethtmlcomobj(httprequest("https://www.realegp.com/"))
altusd := number( sitecom.querySelector(".market-cell .fixed-num").innerText )
} Catch {
MsgBox "Can't use server's price, Alt $ used as 0"
altusd := 0
}
}else{
altusd := SettingsYml["Alt_$"]
}
bankrate := altusd ? 0 : SettingsYml["BankRate_%"]
intTax := (1 + (abs(SettingsYml["IntFees_%"]) / 100)) ** (SettingsYml["IntFees_%"] >= 0 ? 1 : -1)
travelerD := instr(SettingsYml["Traveler_$"], "a") ? StrReplace(SettingsYml["Traveler_$"], "a") * altusd : SettingsYml["Traveler_$"] * bankusd
; shippingD := SettingsYml["Shipping"] * (InStr(SettingsYml["Shipping"], "$") ? strreplace(bankusd, "$") : convrate)
altfactor := altusd ? altusd / bankusd : 1
bankmax := SettingsYml["BankMax"]
if InStr(bankmax, "$")
bankmax := StrReplace(bankmax, "$") * bankusd
local lastcur := convgui["fromCur"].Text
currencylist := strsplit(SettingsYml["Regions"], ",")
convgui["fromCur"].Delete(), convgui["fromCur"].Add(currencylist)
try convgui["fromCur"].Choose(lastcur != "" ? lastcur : intCurrency)
catch
convgui["fromCur"].Choose(intCurrency)
}
calculateresult(*) {
global toValArray := []
if convgui["fromCur"].Text = baseCurrency {
toCur := intCurrency
if altusd {
convrate := 1 / altusd
statusbar.SetText("Alt Rate",4)
} else {
convrate := 1 / bankusd
statusbar.SetText("",4)
}
maxLeft := bankmax
} else {
toCur := baseCurrency
convrate := currencyjson[StrLower(convgui["fromCur"].Text)]["inverseRate"] * altfactor
maxLeft := (bankmax / convrate) / intTax - settingsyml["IntFees"]
if SettingsYml["ShipNow"] and convgui["Overhead"].Text = "Shipping"
maxLeft -= settingsyml["Shipping"]
statusbar.SetText(altusd ? "Alt Rate" : floor(maxLeft), 4)
}
statusbar.SetText("1 " convgui["fromCur"].Text " = " round(convrate, 2) " " toCur, 3)
convgui["toVal"].Delete()
left := RegExReplace(convgui["fromVal"].Text, "[^\d.,]")
if !left
return
if toCur = baseCurrency {
left := left * convrate
intfees := left * (intTax - 1) + SettingsYml["IntFees"] * convrate
right := left + intfees
bankfees := right * (bankrate / 100)
switch convgui["Overhead"].Text {
case "Traveler", "T+T": transport := travelerD
case "Shipping":
transport := SettingsYml["Shipping"]
if InStr(SettingsYml["Shipping"], "$")
transport *= strreplace(bankusd,"$")
else
transport *= convrate
bankfees += transport * SettingsYml["BankRate_%"] / 100
default: transport := 0
}
localfees := SettingsYml["LocalFees"]
if convgui["Overhead"].Text != "Traveler"
localfees += right * SettingsYml["LocalFees_%"] / 100
right += localfees + bankfees + transport
if left > maxLeft * convrate and !altusd
MsgBox ("Price exceeds maximum exchange allowed by bank , change to altrate"), , 48
} else {
left := left * convrate
bankfees:=0, right := left
switch convgui["Overhead"].Text {
case "Traveler","T+T":
transport := travelerD * convrate
right -= transport
case "Shipping":
transport := SettingsYml['Shipping']
bankfees := transport * bankrate / 100
default: transport := 0
}
localfees := SettingsYml['LocalFees'] * convrate
if convgui["Overhead"].Text != "Traveler"
localfees += (1- (1/(1 + SettingsYml['LocalFees_%'] / 100))) * right
right -= localfees
bankfees += right * (bankrate / 100)
intfees := SettingsYml['IntFees']
intfees += (1 - (1/(1 + SettingsYml['IntFees_%'] / 100))) * right
right -= intfees + bankfees + transport
}
toValArray.push Format("{} {}-{}", toCur, ThousandsSep(round(left)), ThousandsSep(round(right)))
if intfees > 0
toValArray.push Format("International Fees: {}", ThousandsSep(Round(intfees)))
if localfees > 0
toValArray.push Format("Local Fees: {}", ThousandsSep(Round(localfees)))
if bankfees > 0
toValArray.push Format("Bank Fees: {}", ThousandsSep(Round(bankfees)))
switch convgui["Overhead"].Text {
case "Traveler","T+T":
toValArray.push Format("Traveler Fees: {}", ThousandsSep(Round(transport)))
case "Shipping":
toValArray.push Format("Shipping Fees: {}{}", ThousandsSep(Round(transport)),
SettingsYml["ShipNow"] and toCur != baseCurrency ? " (included)" : "")
}
convgui["toVal"].add(toValArray)
convgui["toVal"].choose(1)
}
InitiateYml()
calculateresult()
convgui.Show()
settingsgui := Gui()
settingsgui.SetFont("S18")
settingsnames := ["Currencies", "Base", "INT", "Regions", "Conversion", "BankRate_%", "BankMax", "Alt_$", "Overhead", "Traveler_$", "Shipping", "IntFees_%","IntFees", "LocalFees_%", "LocalFees"]
for editbox in settingsnames {
settingsgui.AddText("xs y" (A_Index - 1) * 38, editbox), inputformat := Format("x200 yp h36 w150 v{}", editbox)
; editbox = "BankMax" ? settingsgui.AddPicture("x+20 w20 h20 icon95", A_WinDir "\System32\imageres.dll").OnEvent("Click", (*) => run(SettingsYml["BankInfo"])) : ""
editbox = "Shipping" ? settingsgui.AddCheckbox("vShipNow y" (A_Index - 1) * 38 " x+5 " (SettingsYml["ShipNow"] ? "Checked" : ""), "Now") : ""
settingsgui.Add(InStr("Currencies,Conversion,Overhead", Editbox) ? "Link" : "Edit", inputformat, SettingsYml[editbox])
}
settingsgui.AddButton("y+10 xm+120", "Save").OnEvent("Click", Saveset)
settingsgui.OnEvent("Close", (*) => convgui.Show())
settingsstatus := settingsgui.AddStatusBar()
settingsstatus.SetParts(20, 20)
settingsstatus.OnEvent("Click", (obj, info) => info = 2 ? Run("notepad.exe stores.yml") : run("https://github.com/enaaab460/ShoppingCurrenciesAHK"))
settingsstatus.SetIcon("lib\github.png", , 1), settingsstatus.SetIcon("lib\store.png", , 2)
settingsstatus.SetText(repoupdate > moddate ? "Update available" : "Version:" FormatTime(moddate, 'shortdate'), 3)
Saveset(*) {
global SettingsYml
for key, value in SettingsYml
SettingsYml[key] := settingsgui[key].Text ? settingsgui[key].Text : 0
SettingsYml["ShipNow"] := settingsgui["ShipNow"].Value
FileOverwrite(Yaml(SettingsYml, 2), "settings.yml")
InitiateYml()
calculateresult()
convgui.Show()
settingsgui.Hide()
}
editcurgui := Gui()
editcurgui.SetFont("S14")
editcurgui.AddRadio("vCur Checked", baseCurrency)
editcurgui.AddRadio("x+50", convgui["fromCur"].Text)
editcurgui.AddEdit("y+5 xm w200 vCurValue")
editcurgui.AddButton("y+5 xm+60", "Save").OnEvent("Click", editcur)
editcur(*) {
input := editcurgui.Submit(1)
(input.cur = 1) ? (input.curvalue := 1 / input.curvalue) : ""
currencyjson[strlower(convgui["fromCur"].Text)]["inverseRate"] := input.curvalue
calculateresult()
}
#HotIf WinActive("ahk_exe chrome.exe")
f7:: {
static currencysymbols := Yaml("Common-Currency.json")
A_Clipboard := cn := StrReplace(copynow(, 0.2), "`r`n")
if RegExMatch(cn, "[^\d., ]+", ®ex)
for cur in currencylist
if regex[0] = "$"
convgui["fromCur"].choose("USD")
else if regex[0] = currencysymbols[cur]["symbol"] or regex[0] = currencysymbols[cur]["symbol_native"]
match := convgui["fromCur"].choose(cur)
cn := RegExReplace(cn, ",(\d{2})\D+", ".$1")
convgui["fromVal"].Text := RegExReplace(cn, "[^\d,.]"), calculateresult()
convgui.Show()
KeyWait(ThisHotkey, "t0.3")
}
f8::
chromeprice(*) {
WinActivate "ahk_exe chrome.exe"
; chrome := UIA_Chrome("ahk_exe chrome.exe")
chrome := UIA_Chrome()
chrome.ElementExist({ Name: "Toggle device toolbar", T: "Button" }) ? Send("F12{Sleep 300}") : ""
currentlink := chrome.GetCurrentURL()
for currency, stores in StoresYml
for store, query in stores {
RegExMatch(currentlink, store, ®ex)
if regex {
matchQ := query
MatchC := currency
break 2
}
}
failedmsg("Store not supported", !isset(MatchC))
convgui["fromCur"].Choose(MatchC)
jscmd := Format('
( ltrim join
matchedItems = document.querySelectorAll("{}");
try {matchedItems.forEach(x => x.querySelector("select").remove());} catch {};
jsreturn = Array.from(matchedItems)
.map(x => x.innerText.replace(/[a-zA-Z\n\s]/g,''))
.join("##");void(0);
)', MatchQ)
chrome.JSExecute(jscmd)
jsreturn := chrome.JSReturnThroughClipboard("jsreturn")
results := ""
for value in StrSplit(jsreturn, "##"){
convgui["fromVal"].Text := value
calculateresult()
for i in toValArray
results .= i "||"
results .= "##"
}
chrome.JSExecute(Format("
( ltrim join
ahkreturn = "{}".split("##");
ahklen = ahkreturn.length - 1;
for (i=0;i<ahklen;i++){
ahkselect = document.createElement("select");
ahkoptions = ahkreturn[i].split("||");
ahksellen = ahkoptions.length - 1;
for (j=0;j<ahksellen;j++){
option = document.createElement("option");
option.innerText = ahkoptions[j];
ahkselect.appendChild(option);
}
matchedItems[i].appendChild(ahkselect);
}
)",results))
}
f9:: {
WinActivate "ahk_exe chrome.exe"
chrome := UIA_Chrome("A")
RegExMatch(chrome.GetCurrentURL(), "U)https?:\/\/(?:www\.)?(?<host>[\w.]+)\/", &urlregex)
send "^C"
sleep 400
chrome.WaitElement({ Name: "Toggle device toolbar", T: "Button" })
ToolTip "Choose Price"
KeyWait "LButton", "D"
ToolTip
sleep 50
targetspan := chrome.ElementFromPathExist({T:30,CN:"Chrome_RenderWidgetHostHWND", i:-1}, {T:26}, {T:25}, {T:26, i:2}, {T:8}, {T:7, i:-1}, {T:5}).Name
failedmsg "Failed to find cssselector", !IsSet(targetspan)
ToolTip targetspan
cur := StrUpper(mySelectInput("ComboBox", currencylist, , "Currency of store"))
store := okinputbox("Confirm that the url is correct and specific to region", , , strreplace(urlregex["host"], "www."))
regionyml := Yaml("stores.yml")[1]
regionyml.has(cur) ? "" : regionyml[cur] := Map()
if regionyml[cur].has(store) and InStr(regionyml[cur][store], targetspan)
MsgBox(Format("{} is already a part of {}'s css selectors`nDo you want to replace the whole css?", targetspan, store), , 4) = "Yes" ? regionyml[cur][store] := targetspan : ""
else regionyml[cur][store] := targetspan
FileOverwrite(Yaml(regionyml, 3), "stores.yml")
InitiateYml()
ToolTip
WinActivate "ahk_exe chrome.exe"
send "{F12}{Sleep 500}"
chromeprice()
}