Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load data from bonbast.com #1

Merged
merged 5 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
*.log

cookie.txt
120 changes: 120 additions & 0 deletions currency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package main

import (
"strconv"
"strings"
)

var (
Currencies map[string][2]string = map[string][2]string{
"usd": {"USD", "US Dollar"},
"eur": {"EUR", "Euro"},
"gbp": {"GBP", "British Pound"},
"chf": {"CHF", "Swiss Franc"},
"cad": {"CAD", "Canadian Dollar"},
"aud": {"AUD", "Australian Dollar"},
"sek": {"SEK", "Swedish Krona"},
"nok": {"NOK", "Norwegian Krone"},
"rub": {"RUB", "Russian Ruble"},
"thb": {"THB", "Thai Baht"},
"sgd": {"SGD", "Singapore Dollar"},
"hkd": {"HKD", "Hong Kong Dollar"},
"azn": {"AZN", "Azerbaijani Manat"},
"amd": {"AMD", "Armenian Dram"},
"dkk": {"DKK", "Danish Krone"},
"aed": {"AED", "UAE Dirham"},
"jpy": {"JPY", "Japanese Yen"},
"try": {"TRY", "Turkish Lira"},
"cny": {"CNY", "Chinese Yuan"},
"sar": {"SAR", "KSA Rial"},
"inr": {"INR", "Indian Rupee"},
"myr": {"MYR", "Ringgit"},
"afn": {"AFN", "Afghan Afghani"},
"kwd": {"KWD", "Kuwaiti Dinar"},
"iqd": {"IQD", "Iraqi Dinar"},
"bhd": {"BHD", "Bahraini Dinar"},
"omr": {"OMR", "Omani Rial"},
"qar": {"QAR", "Qatari Rial"},
}

Coins map[string]string = map[string]string{
"azadi1": "Azadi",
"azadi1_2": "Azadi 1/2",
"azadi1_4": "Azadi 1/4",
"emami": "Emami",
"azadi1g": "Gerami",
}
)

type Currency struct {
Code string
Name string
Sell int
Buy int
}

type Coin struct {
Name string
Sell int
Buy int
}

type Gold struct {
Name string
Price int
}

func GetCurrency(name string) (*Currency, error) {
currencies := GetCurrencies()

sell, err := strconv.Atoi(currencies[name+"1"])
if err != nil {
return nil, err
}

buy, err := strconv.Atoi(currencies[name+"2"])
if err != nil {
return nil, err
}

return &Currency{
Code: Currencies[name][0],
Name: Currencies[name][1],
Sell: sell,
Buy: buy,
}, nil
}

func GetCoin(name string) (*Coin, error) {
currencies := GetCurrencies()

sell, err := strconv.Atoi(currencies[name])
if err != nil {
return nil, err
}

buy, err := strconv.Atoi(currencies[name+"_2"])
if err != nil {
return nil, err
}

return &Coin{
Name: Coins[name],
Sell: sell,
Buy: buy,
}, nil
}

func GetGold(name string) (*Gold, error) {
currencies := GetCurrencies()

price, err := strconv.Atoi(currencies[name])
if err != nil {
return nil, err
}

return &Gold{
Name: strings.ToTitle(name),
Price: price,
}, nil
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/itsjoniur/currency

go 1.19

require github.com/andelf/go-curl v0.0.0-20200630032108-fd49ff24ed97 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/andelf/go-curl v0.0.0-20200630032108-fd49ff24ed97 h1:Nyfs+rh56aORy2tGMI9GCYEqTfePwL1v47qOzebfv/o=
github.com/andelf/go-curl v0.0.0-20200630032108-fd49ff24ed97/go.mod h1:WO1d2m1QDzkoPcgn9lgHVMi7qQR5j3jxYjIIvMTHpC0=
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"log"
)

func main() {
code := "azadi1"
c, err := GetCoin(code)
if err != nil {
log.Panicln(err)
}

fmt.Printf("%#v\n", c)
}
123 changes: 123 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"encoding/json"
"fmt"
"log"
"regexp"

curl "github.com/andelf/go-curl"
)

func GetCurrencies() map[string]string {
urlPage := "https://bonbast.com/"
urlJson := "https://bonbast.com/json"

html := loadHtmlPage(urlPage)
key, err := getPostJsonKeyFromHtml(html)
if err != nil {
log.Panicln(err)
}

res := loadJsonData(urlJson, key)
return res
}

func loadHtmlPage(url string) string {
var result string
ret := curl.EasyInit()
defer ret.Cleanup()

fn := func(buf []byte, userdata any) bool {
result = string(buf)
return true
}

ret.Setopt(curl.OPT_URL, url)
ret.Setopt(curl.OPT_TRANSFERTEXT, 1)
ret.Setopt(curl.OPT_SSL_VERIFYPEER, false)
ret.Setopt(curl.OPT_SSL_VERIFYHOST, false)
ret.Setopt(curl.OPT_FOLLOWLOCATION, true)
ret.Setopt(curl.OPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36")
ret.Setopt(curl.OPT_COOKIEJAR, "cookie.txt")
ret.Setopt(curl.OPT_COOKIEFILE, "cookie.txt")
ret.Setopt(curl.OPT_REFERER, "https://bonbast.com")
ret.Setopt(curl.OPT_ENCODING, "gzip, deflate, br")
ret.Setopt(curl.OPT_HTTPHEADER, []string{
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding: gzip, deflate, br",
"Accept-Language: en-US,en;q=0.9",
"Cache-Control: max-age=0",
"Connection: keep-alive",
"Host: bonbast.com",
"Upgrade-Insecure-Requests: 1",
"User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
})
ret.Setopt(curl.OPT_WRITEFUNCTION, fn)

ret.Perform()

return result

}

func loadJsonData(url string, key string) map[string]string {
var jsonData string
ret := curl.EasyInit()
defer ret.Cleanup()

fn := func(buf []byte, userdata any) bool {
jsonData = string(buf)
return true
}

ret.Setopt(curl.OPT_URL, url)
ret.Setopt(curl.OPT_TRANSFERTEXT, 1)
ret.Setopt(curl.OPT_SSL_VERIFYPEER, false)
ret.Setopt(curl.OPT_SSL_VERIFYHOST, false)
ret.Setopt(curl.OPT_FOLLOWLOCATION, true)
ret.Setopt(curl.OPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36")
ret.Setopt(curl.OPT_COOKIEJAR, "cookie.txt")
ret.Setopt(curl.OPT_COOKIEFILE, "cookie.txt")
ret.Setopt(curl.OPT_REFERER, "https://bonbast.com")
ret.Setopt(curl.OPT_ENCODING, "gzip, deflate, br")
ret.Setopt(curl.OPT_HTTPHEADER, []string{"Accept: application/json, text/javascript, */*; q=0.01",
"Accept-Encoding: gzip, deflate, br",
"Accept-Language: en-US,en;q=0.9,fa;q=0.8,it;q=0.7",
"Cache-Control: max-age=0",
"Connection: keep-alive",
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
"Host: bonbast.com",
"Origin: https://bonbast.com",
"Referer: https://bonbast.com/",
"Sec-Ch-Ua: \"Chromium\";v=\"104\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"104\"",
"Sec-Ch-Ua-Mobile: ?0",
"Sec-Ch-Ua-Platform: \"Windows\"",
"Sec-Fetch-Dest: empty",
"Sec-Fetch-Mode: cors",
"Sec-Fetch-Site: same-origin",
"User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
"X-Requested-With: XMLHttpRequest",
})
ret.Setopt(curl.OPT_POST, 1)
ret.Setopt(curl.OPT_POSTFIELDS, fmt.Sprintf("data=%s&webdriver=false", key))
ret.Setopt(curl.OPT_WRITEFUNCTION, fn)

ret.Perform()
result := map[string]string{}
json.Unmarshal([]byte(jsonData), &result)

return result
}

func getPostJsonKeyFromHtml(jd string) (string, error) {
r, err := regexp.Compile(`\$.post\(\'\/json\',{ data:\"(?P<data>.*)\"`)
if err != nil {
return "", err
}

key := r.FindStringSubmatch(jd)
result := key[1]

return result, nil
}