-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (91 loc) · 1.98 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
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
111
112
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)
}
type Comic struct {
Id int
Title string
DownloadId int
ComicSite ComicSite
}
func (comic *Comic) LoadMeta() error {
var err error
var mainPageHtml string
comicMainPageUrl := comic.ComicSite.GetComicMainPageUrl(comic.Id)
mainPageHtml, err = comic.getComicMainPageHtml(comicMainPageUrl)
if err != nil {
return err
}
comic.Title, err = comic.findTitle(mainPageHtml)
if err != nil {
return err
}
comic.DownloadId, err = comic.findDownloadId(mainPageHtml)
if err != nil {
return err
}
return nil
}
func (_ Comic) getComicMainPageHtml(url string) (string, error) {
res, err := http.Get(url)
if err != nil {
return "", err
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(data), nil
}
func (_ Comic) findTitle(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 (_ Comic) findDownloadId(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://*****"}
comic := &Comic{ComicSite: comicSite, Id: 282526}
err := comic.LoadMeta()
check(err)
fmt.Printf("%+v\n", comic)
}