-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.go
executable file
·166 lines (155 loc) · 5 KB
/
io.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Copyright 2017 by GoSpider author.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
*/
package src
import (
"fmt"
"github.com/hunterhug/GoSpider/util"
"regexp"
"strings"
"time"
)
func Input(say, defaults string) string {
fmt.Println(say)
var str string
fmt.Scanln(&str)
if strings.TrimSpace(str) == "" {
if strings.TrimSpace(defaults) != "" {
return defaults
} else {
fmt.Println("不能为空!")
return Input(say, defaults)
}
}
//fmt.Println("--" + str + "--")
return str
}
// 输出友好格式HTML,返回问题ID,回答ID,标题,作者,还有HTML
func OutputHtml(answer DataInfo) (qid, aid int, title, who, html string) {
answer.Content = strings.Replace(answer.Content, "src", "xx", -1)
if PublishToWeb {
answer.Content = strings.Replace(answer.Content, "data-original", "data-src", -1)
} else {
answer.Content = strings.Replace(answer.Content, "data-original", "src", -1)
}
b := `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>%s:%d</title>
<style>
body{
margin:20px 15%%
}
img {width:60%%;
display:block;
text-align:center}
.link{
margin:20px;
height:10px
}
</style>
%s
</head>
<body>
<div id="author">
%s
</div>
<div class="link">
###link###
</div>
<div>
跳页: <input type="number" id="page" min="1" max="500" value="3" style="width:100px">
<input type="submit" onclick="var a=document.getElementById('page').value;location.href=a+'.html' "></div>
<div id="answer">
<hr/>
正文:
%s
</div>
<div class="link">
###link###
</div>
<div>
跳页: <input type="number" id="page" min="1" max="500" value="3" style="width:100px">
<input type="submit" onclick="var a=document.getElementById('page').value;location.href=a+'.html' "></div>
</body>
</html>
`
sex := "男"
if answer.Author.Sex == 0 {
sex = "女"
}
purl := fmt.Sprintf(PeopleUrl, answer.Author.UrlToken)
qurl := fmt.Sprintf(QuestionUrl, answer.Question.Qid)
aurl := fmt.Sprintf(AnswerUrl, answer.Question.Qid, answer.Aid)
ct := time.Unix(int64(answer.CreateTime), 0).Format("2006-01-02 03:04:05 PM")
ut := time.Unix(int64(answer.UpdateTime), 0).Format("2006-01-02 03:04:05 PM")
about := fmt.Sprintf(`
名字:<a href="%s">%s</a> 性别:%s<br/>
<img data-src="%s" width="400" height="500" /><br/>
介绍:%s<br/>
<a href="%s">问题</a><br/>
<a href="%s">答案</a>新建于:%s,更新于%s
<br/>
`, purl, answer.Author.Name, sex, strings.Replace(answer.Author.Image, "{size}", "xll", -1), answer.Author.About, qurl, aurl, ct, ut)
if !PublishToWeb {
about = strings.Replace(about, "data-src", "src", -1)
}
JsScript := ""
if PublishToWeb {
JsScript = "<script type='application/ecmascript' async='' src='../" + JsName + "'></script>"
}
content := fmt.Sprintf(b, answer.Question.Title, answer.Aid, JsScript, about, answer.Content)
return answer.Question.Qid, answer.Aid, answer.Question.Title, answer.Author.UrlToken, content
}
// 遇到返回的JSON中有中文乱码,请转意
func JsonBack(body []byte) ([]byte, error) {
return util.JsonBack(body)
}
func OneOutputHtml(data string) string {
s := strings.Replace(data, "<script type='application/ecmascript' async='' src='../hotpic.js'></script>", "", -1)
s = strings.Replace(s, ` 跳页: <input type="number" id="page" min="1" max="500" value="3" style="width:100px">
<input type="submit" onclick="var a=document.getElementById('page').value;location.href=a+'.html' "></div>`, "", -1)
s = strings.Replace(s, "data-src", "src", -1)
s = strings.Replace(s, "###link###", "", -1)
r1, _ := regexp.Compile("<noscript>.*?</noscript>")
s = r1.ReplaceAllString(s, "")
r, err := regexp.Compile(`src="(.*?)"`)
if err != nil {
return s
} else {
bb := r.FindAllSubmatch([]byte(s), -1)
for _, v := range bb {
temp := string(v[1])
filetemp := strings.Replace(strings.Replace(util.ValidFileName(temp), "#", "_", -1), "jpg", "png", -1)
s = strings.Replace(s, temp, "./"+filetemp, -1) // 知乎图片默认后缀不知
}
}
return s
}
func BossOutputHtml(qid int, who string, aid int, data string) string {
r1, _ := regexp.Compile("<noscript>.*?</noscript>")
s := r1.ReplaceAllString(data, "")
r, err := regexp.Compile(`src="(.*?)"`)
if err != nil {
return s
} else {
bb := r.FindAllSubmatch([]byte(s), -1)
for _, v := range bb {
temp := string(v[1])
filetemp := strings.Replace(strings.Replace(util.ValidFileName(temp), "#", "_", -1), "jpg", "png", -1)
s = strings.Replace(s, temp, fmt.Sprintf("../%d/%s-%d/%s", qid, who, aid, filetemp), -1) // 知乎图片默认后缀不知
}
}
return s
}