生成
(identify string)
| 219 | |
| 220 | // 生成 |
| 221 | func RenderCoverByBookIdentify(identify string) (err error) { |
| 222 | var args []string |
| 223 | shotJS := "cover.js" |
| 224 | if _, err = os.Stat(shotJS); err != nil { |
| 225 | err = ioutil.WriteFile(shotJS, []byte(screenshotCoverJS), os.ModePerm) |
| 226 | } |
| 227 | if err != nil { |
| 228 | return |
| 229 | } |
| 230 | |
| 231 | //使用chromium-browser |
| 232 | // chromium-browser --headless --disable-gpu --screenshot --no-sandbox --window-size=320,480 http://www.bookstack.cn |
| 233 | link := "http://localhost:" + beego.AppConfig.DefaultString("httpport", "8181") + "/local-render-cover?id=" + identify |
| 234 | name := "node" |
| 235 | if ok := beego.AppConfig.DefaultBool("puppeteer", false); ok { |
| 236 | args = []string{shotJS, "--identify", identify, "--url", link} |
| 237 | } else { |
| 238 | err = errors.New("请安装并启用puppeteer") |
| 239 | return |
| 240 | } |
| 241 | cmd := exec.Command(name, args...) |
| 242 | beego.Info("render cover by book id:", cmd.Args) |
| 243 | // 超过30秒,杀掉进程,避免长期占用 |
| 244 | time.AfterFunc(httpTimeout, func() { |
| 245 | if cmd.Process != nil && cmd.Process.Pid != 0 { |
| 246 | cmd.Process.Kill() |
| 247 | } |
| 248 | }) |
| 249 | if err = cmd.Run(); err != nil { |
| 250 | beego.Error(err) |
| 251 | } |
| 252 | return |
| 253 | } |
| 254 | |
| 255 | // 使用chrome采集网页HTML |
| 256 | func CrawlByChrome(urlStr string, bookIdentify string) (cont string, err error) { |
no test coverage detected