当前位置: 首页>>代码示例>>Golang>>正文


Golang Post.CommentParseErrors方法代码示例

本文整理汇总了Golang中github.com/kimxilxyong/intogooglego/post.Post.CommentParseErrors方法的典型用法代码示例。如果您正苦于以下问题:Golang Post.CommentParseErrors方法的具体用法?Golang Post.CommentParseErrors怎么用?Golang Post.CommentParseErrors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/kimxilxyong/intogooglego/post.Post的用法示例。


在下文中一共展示了Post.CommentParseErrors方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ParseHtmlComments

func ParseHtmlComments(p *post.Post) (err error) {

	if p.WebPostId == "" {
		return errors.New(fmt.Sprintf("p.WebPostId is empty in post '%s'", p.String("PC: ")))
	}
	// Get comments from hackernews
	geturl := fmt.Sprintf("http://news.ycombinator.com/item?id=%s", p.WebPostId)
	// DEBUG
	//geturl := fmt.Sprintf("https://news.ycombinator.com/item?id=9751858")

	if DebugLevel > 2 {
		fmt.Printf("START GET COMMENTS FROM '%s'\n", geturl)
	}

	body, err := GetHtmlBody(geturl)
	if err != nil {
		return errors.New("GetHtmlBody: " + err.Error())
	}
	// Create a qoquery document to parse from an io.Reader
	doc, err := goquery.NewDocumentFromReader(body)
	if err != nil {
		return errors.New("Failed to parse HTML: " + err.Error())
	}
	// Find hackernews comments = elements with class "athing"
	thing := doc.Find(".athing")
	for iThing := range thing.Nodes {
		// use `singlecomment` as a selection of one single post
		singlecomment := thing.Eq(iThing)

		comment := post.NewComment()
		//p.Comments = append(p.Comments, &comment)

		comheads := singlecomment.Find(".comhead a")
		for i := range comheads.Nodes {

			comhead := comheads.Eq(i)
			t, _ := comhead.Html()
			s, exists := comhead.Attr("href")
			if exists {
				if strings.HasPrefix(s, "user?id") {
					comment.User = t
					continue
				}
				if strings.HasPrefix(s, "item?id") {
					if strings.Contains(t, "ago") {
						var commentDate time.Time
						commentDate, err = GetDateFromCreatedAgo(t)
						if err != nil {
							comment.Err = errors.New(fmt.Sprintf("Failed to convert to date: %s: %s\n", t, err.Error()))
							err = nil
							continue
						}
						comment.CommentDate = commentDate
						if len(strings.Split(s, "=")) > 1 {
							comment.WebCommentId = strings.Split(s, "=")[1]
						}
						//comment.Err = err
					}
				}
			}

			comments := singlecomment.Find("span.comment")

			removeReplySelection := comments.Find("span div.reply")
			removeReplySelection.Remove()

			var sep string
			for iComment, _ := range comments.Nodes {
				s := comments.Eq(iComment)

				h, _ := s.Html()

				if !utf8.ValidString(s.Text()) {
					comment.Err = errors.New(fmt.Sprintf("Ignoring invalid UTF-8: '%s'", s.Text()))
					break
				}

				h, err = HtmlToMarkdown(h)
				if err != nil {
					comment.Err = errors.New(fmt.Sprintf("Ignoring markdownifier: '%s'", err.Error()))
					break
				}

				if h != "" {
					comment.Body = comment.Body + sep + h
				}
				sep = "\n"
			}
			//fmt.Printf("POST %s BODY = %s\n", p.WebPostId, comment.Body)

			if comment.Err == nil && len(comment.WebCommentId) > 0 && len(comment.Body) > 0 {
				p.Comments = append(p.Comments, &comment)
			} else {
				p.CommentParseErrors = append(p.CommentParseErrors, &comment)
			}
		}
	}

	if DebugLevel > 0 {
		fmt.Printf("GET COMMENTS FROM '%s' yielded %d comments\n", geturl, len(p.Comments))
//.........这里部分代码省略.........
开发者ID:jimmytuc,项目名称:intogooglego,代码行数:101,代码来源:hackernewsGoqueryWithComments.go


注:本文中的github.com/kimxilxyong/intogooglego/post.Post.CommentParseErrors方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。