Exemplo n.º 1
0
func ttRssHeadlinesFromArticles(articles []content.UserArticle, feedTitle string, content, excerpt bool) (c ttRssHeadlinesContent) {
	c = ttRssHeadlinesContent{}
	for _, a := range articles {
		d := a.Data()
		title := feedTitle
		h := ttRssHeadline{
			Id:        d.Id,
			Unread:    !d.Read,
			Marked:    d.Favorite,
			Updated:   d.Date.Unix(),
			IsUpdated: !d.Read,
			Title:     d.Title,
			Link:      d.Link,
			FeedId:    strconv.FormatInt(int64(d.FeedId), 10),
			FeedTitle: title,
		}

		if content {
			h.Content = d.Description
		}

		if excerpt {
			excerpt := search.StripTags(d.Description)
			if len(excerpt) > 100 {
				excerpt = excerpt[:100]
			}

			h.Excerpt = excerpt
		}

		c = append(c, h)
	}
	return
}
Exemplo n.º 2
0
func formatArticle(user content.User, id data.ArticleId, extractor content.Extractor, webfwConfig webfw.Config, readeefConfig readeef.Config) (resp responseError) {
	resp = newResponse()

	article := user.ArticleById(id)
	if user.HasErr() {
		resp.err = user.Err()
		return
	}

	extract := article.Extract()
	if article.HasErr() {
		resp.err = article.Err()
		return
	}

	extractData := extract.Data()
	if extract.HasErr() {
		switch err := extract.Err(); err {
		case content.ErrNoContent:
			if extractor == nil {
				resp.err = fmt.Errorf("Error formatting article: A valid extractor is reequired")
				return
			}

			extractData, resp.err = extractor.Extract(article.Data().Link)
			if resp.err != nil {
				return
			}

			extractData.ArticleId = article.Data().Id
			extract.Data(extractData)
			extract.Update()
			if extract.HasErr() {
				resp.err = extract.Err()
				return
			}
		default:
			resp.err = err
			return
		}
	}

	processors := user.Repo().ArticleProcessors()
	if len(processors) > 0 {
		a := user.Repo().UserArticle(user)
		a.Data(data.Article{Description: extractData.Content})

		ua := []content.UserArticle{a}

		if extractData.TopImage != "" {
			a = user.Repo().UserArticle(user)
			a.Data(data.Article{
				Description: fmt.Sprintf(`<img src="%s">`, extractData.TopImage),
			})

			ua = append(ua, a)
		}

		for _, p := range processors {
			ua = p.ProcessArticles(ua)
		}

		extractData.Content = ua[0].Data().Description

		if extractData.TopImage != "" {
			content := ua[1].Data().Description

			content = strings.Replace(content, `<img src="`, "", -1)
			i := strings.Index(content, `"`)
			content = content[:i]

			extractData.TopImage = content
		}
	}

	s := summarize.NewFromString(extractData.Title, search.StripTags(extractData.Content))

	s.Language = extractData.Language
	keyPoints := s.KeyPoints()

	for i := range keyPoints {
		keyPoints[i] = html.UnescapeString(keyPoints[i])
	}

	resp.val["KeyPoints"] = keyPoints
	resp.val["Content"] = extractData.Content
	resp.val["TopImage"] = extractData.TopImage
	resp.val["Id"] = id
	return
}