-
Notifications
You must be signed in to change notification settings - Fork 1
/
origin.go
122 lines (105 loc) · 2.52 KB
/
origin.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
113
114
115
116
117
118
119
120
121
122
/**
* Copyright (C) 2021 CharlieYu4994
*
* This file is part of Blog-Pic-go.
*
* Blog-Pic-go is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Blog-Pic-go is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Blog-Pic-go. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/dlclark/regexp2"
)
type picture struct {
Date string `json:"enddate"`
BaseUrl string `json:"urlbase"`
}
func getBing(num int) ([]picture, bool) {
var url strings.Builder
var tmp []byte
url.Grow(192)
url.WriteString(bing)
url.WriteString("/HPImageArchive.aspx?format=js&n=")
url.WriteString(strconv.Itoa(num))
url.WriteString("&mkt=zh-CN")
for t := 0; t <= 5; t++ {
resp, err := http.Get(url.String())
if err != nil || resp.StatusCode != 200 {
continue
}
tmp, err = io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
continue
}
}
if tmp == nil {
return nil, false
}
var ret struct {
Images []picture
}
err := json.Unmarshal(tmp, &ret)
if err != nil {
return nil, false
}
return ret.Images, true
}
func getAPOD(num int) ([]picture, bool) {
var url strings.Builder
var ret []picture
var tmp []byte
date := time.Now().AddDate(0, 0, 1)
matcher := regexp2.MustCompile(`(?<=\<a href=\")(image\/\d{4}\/.*\.\w{3,4})(?=[\s\S]*<IMG)`, 0)
for i := 0; i < num; i++ {
date = date.AddDate(0, 0, -1)
url.Reset()
url.Grow(40)
url.WriteString(apod)
url.WriteString("ap")
url.WriteString(date.Format("060102"))
url.WriteString(".html")
for t := 0; t <= 5; t++ {
resp, err := http.Get(url.String())
if err != nil || resp.StatusCode != 200 {
continue
}
tmp, err = io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
continue
}
}
if tmp == nil {
continue
}
baseUrl, err := matcher.FindStringMatch(string(tmp))
if err != nil || baseUrl == nil {
continue
}
ret = append(ret, picture{
Date: date.Format("20060102"),
BaseUrl: string(baseUrl.String()),
})
}
if ret == nil {
return nil, false
}
return ret, true
}