// /admin/crawl/article/new func CrawlArticleHandler(rw http.ResponseWriter, req *http.Request) { var data = make(map[string]interface{}) if req.PostFormValue("submit") == "1" { urls := strings.Split(req.PostFormValue("urls"), "\n") var errMsg string for _, articleUrl := range urls { _, err := service.ParseArticle(strings.TrimSpace(articleUrl), false) if err != nil { errMsg = err.Error() } } if errMsg != "" { data["ok"] = 0 data["error"] = errMsg } else { data["ok"] = 1 data["msg"] = "添加成功" } } else { // 设置内容模板 req.Form.Set(filter.CONTENT_TPL_KEY, "/template/admin/article/new.html") } filter.SetData(req, data) }
func AddArticleHandler(rw http.ResponseWriter, req *http.Request) { article, err := service.ParseArticle(req.FormValue("url"), false) if err != nil { fmt.Fprint(rw, err) return } fmt.Fprint(rw, article.Title) }
func parseArticleList(url, listselector, resultselector string, isAuto bool) (err error) { logger.Infoln("parse url:", url) var doc *goquery.Document if doc, err = goquery.NewDocument(url); err != nil { return } doc.Find(listselector).Each(func(i int, contentSelection *goquery.Selection) { aSelection := contentSelection.Find(resultselector) if isAuto { title := aSelection.Text() matched, err := regexp.MatchString(pattern, title) if err != nil { logger.Errorln(err) return } if !matched { return } } articleUrl, ok := aSelection.Attr("href") if ok { service.ParseArticle(articleUrl, isAuto) } }) return }