/*
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 := `
%s:%d
%s
%s
###link###
跳页:
正文:
%s
###link###
跳页:
`
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(`
名字:%s 性别:%s
介绍:%s 问题 答案新建于:%s,更新于%s
`, 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 = ""
}
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, "", "", -1)
s = strings.Replace(s, ` 跳页:
`, "", -1)
s = strings.Replace(s, "data-src", "src", -1)
s = strings.Replace(s, "###link###", "", -1)
r1, _ := regexp.Compile("")
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("")
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
}