-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
110 lines (91 loc) · 2.33 KB
/
server.go
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
package main
import(
"github.com/labstack/echo"
"github.com/PuerkitoBio/goquery"
"strings"
"encoding/json"
"io/ioutil"
"time"
"net/http"
"log"
"fmt"
"os"
)
var SERVER_ADRESS = ":" + os.Getenv("PORT")
var INTERVAL time.Duration = 60 // interval new data should be fetched in seconds
var TARGET_URL = "https://coinmarketcap.com/all/views/all/"
func main() {
e := echo.New()
e.Static("/", "static")
go loopInterval(INTERVAL) // start the loop
type WelcomeMessage struct {
Success bool `json:"success"`
Message string `json:"message"`
}
e.GET("/", func(c echo.Context) error {
res := &WelcomeMessage{
Success:true,
Message: "Welcome to the crypto-prices api, /coins.json for the data",
}
return c.JSON(http.StatusOK, res)
})
e.Logger.Fatal(e.Start(SERVER_ADRESS))
}
func scrapeCoins() {
println("Fetching new coins...")
type coin struct {
Name string `json:"name"`
Ticker string `json:"ticker"`
Btc string `json:"btc"`
Price string `json:"price"`
Currency string `json:"currency"`
}
type responseStruct struct {
Success bool `json:"success"`
Timestamp int64 `json:"timestamp"`
AmountOfCoins int `json:"amount_of_coins"`
Coins []coin `json:"coins"`
}
var coins []coin
var response[] responseStruct
response = response;
doc, err := goquery.NewDocument(TARGET_URL)
if err != nil {
log.Fatal(err)
}
// Find the review items
doc.Find("tr").Each(func(i int, s *goquery.Selection) {
id := s.AttrOr("id", "")
if (id != "") {
var price= s.Find(".price")
var coinName = strings.TrimPrefix(id, "id-")
var ticker = s.Find(".currency-symbol").Text()
var btc = price.AttrOr("data-btc", "")
var price_usd = price.AttrOr("data-usd", "")
coins = append(coins, coin {
Name: coinName,
Ticker: ticker,
Btc: btc,
Price: price_usd,
Currency: "usd",
})
}
})
response = append(response, responseStruct{
Success:true,
Timestamp:time.Now().Unix(),
AmountOfCoins:len(coins),
Coins: coins,
})
//b, _ := json.MarshalIndent(response, "", " ")
b, _ := json.MarshalIndent(response, "", " ")
// writing json to file
_ = ioutil.WriteFile("static/coins.json", b, 0644)
println("Wrote " + fmt.Sprint(len(coins)) + " coins to the coins.json file")
}
func loopInterval(interval time.Duration) {
scrapeCoins()
for range time.Tick(time.Second * interval){
scrapeCoins()
}
}