-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.go
More file actions
69 lines (47 loc) · 1.16 KB
/
Copy pathservice.go
File metadata and controls
69 lines (47 loc) · 1.16 KB
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
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/PuerkitoBio/goquery"
)
func getComicURL() string {
return scrapeWebPage()
}
func scrapeWebPage() string {
randDate := randomDate()
url := fmt.Sprintf("https://www.gocomics.com/garfield/%s", randDate)
var comicURL string
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalf("status code error: %d %s", resp.StatusCode, resp.Status)
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}
doc.Find(".item-comic-image").Each(func(i int, s *goquery.Selection) {
imageNode := s.Find(".img-fluid")
str, exists := imageNode.Attr("src")
if exists {
comicURL = fmt.Sprintf("%s.png", str)
}
})
return comicURL
}
func randomDate() string {
minDate := time.Date(1978, 6, 19, 0, 0, 0, 0, time.UTC).Unix()
maxDate := time.Now().Unix()
delta := maxDate - minDate
sec := rand.Int63n(delta) + minDate
utcDate := time.Unix(sec, 0)
randomDate := utcDate.UTC().Format("2006/01/02")
formattedDateString := fmt.Sprintf("%s", randomDate)
return formattedDateString
}