-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingfetcher.go
99 lines (93 loc) · 2.3 KB
/
pingfetcher.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
package main
import (
"context"
"net/http"
"path"
"strconv"
"time"
"github.com/asendia/salmonping/db"
)
func fetchListings(ctx context.Context, queries *db.Queries) error {
listings, err := queries.SelectListings(ctx, db.SelectListingsParams{
EnablePing: []bool{true},
Names: []string{},
Platforms: []string{},
Statuses: []string{},
})
if err != nil {
return err
}
for _, ol := range listings {
if !ol.EnablePing {
logJson(map[string]interface{}{
"level": "info",
"message": "Skipping a listing because ping is disabled",
"listing": ol.Name,
"url": ol.Url,
})
continue
}
logJson(map[string]interface{}{
"level": "info",
"message": "Scraping a listing",
"listing": ol.Name,
"url": ol.Url,
})
var status string
var header http.Header
var code int
var body []byte
var err error
if ol.Platform == "gofood" {
status, header, code, body, err = getGofoodStatus(ol.Url)
} else if ol.Platform == "grabfood" {
status, header, code, body, err = getGrabfoodStatus(ol.Url)
} else {
logJson(map[string]interface{}{
"level": "error",
"message": "Unsupported url",
"listing": ol.Name,
"url": ol.Url,
})
continue
}
if err != nil {
logJson(map[string]interface{}{
"level": "error",
"message": "Error scraping a listing",
"listing": ol.Name,
"error": err.Error(),
})
continue
}
logJson(map[string]interface{}{
"header": header,
"code": code,
"level": "info",
"listing": ol.Name,
"status": status,
})
if status == "unknown" {
// Store the body in Cloud Storage
// Create objectname with this format dump/2023/12/25/grabfood_21_05.html
now := time.Now()
objectName := path.Join("dump", strconv.Itoa(now.Year()), strconv.Itoa(int(now.Month())), strconv.Itoa(now.Day()), ol.Platform+"_"+strconv.Itoa(now.Hour())+"_"+strconv.Itoa(now.Minute())+".html")
writeToCloudStorage("salmonping", objectName, body)
}
// Log to database
_, err = queries.InsertPing(ctx, db.InsertPingParams{
OnlineListingID: ol.ID,
Status: status,
})
if err != nil {
logJson(map[string]interface{}{
"level": "error",
"message": "Error inserting a ping",
"listing": ol.Name,
"error": err.Error(),
})
continue
}
}
return nil
}