-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (63 loc) · 1.38 KB
/
main.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
)
func check(e error) {
if e != nil {
panic(e)
}
}
type ComicSite struct {
MainPageUrl string
}
func (cs ComicSite) GetComicMainPageUrl(comicId int) string {
return fmt.Sprintf("%s/cn/s/%d/", cs.MainPageUrl, comicId)
}
func getTitle(html string) (string, error) {
titleR, err := regexp.Compile(`<title>(.+?)</title>`)
if err != nil {
return "", err
}
titleMatches := titleR.FindStringSubmatch(html)
if titleMatches == nil {
err := errors.New("comic title not found")
return "", err
}
title := titleMatches[1]
return title, nil
}
func getDownloadId(html string) (int, error) {
downloadR, err := regexp.Compile(`cn/(\d+)/1.(jpg|png)`)
if err != nil {
return 0, err
}
downloadMatches := downloadR.FindStringSubmatch(html)
if downloadMatches == nil {
err := errors.New("download id not found")
return 0, err
}
downloadId, err := strconv.Atoi(downloadMatches[1])
if err != nil {
return 0, err
}
return downloadId, nil
}
func main() {
comicSite := ComicSite{MainPageUrl: "https://*****"}
res, err := http.Get(comicSite.GetComicMainPageUrl(282526))
check(err)
data, err := ioutil.ReadAll(res.Body)
check(err)
dataStr := string(data)
title, err := getTitle(dataStr)
check(err)
fmt.Println(title)
downloadId, err := getDownloadId(dataStr)
check(err)
fmt.Println(downloadId)
}