Esempio n. 1
0
func formatArticle(user content.User, id data.ArticleId, webfwConfig webfw.Config, readeefConfig readeef.Config) (resp responseError) {
	resp = newResponse()

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

	formatting := article.Format(webfwConfig.Renderer.Dir, readeefConfig.ArticleFormatter.ReadabilityKey)
	if article.HasErr() {
		resp.err = user.Err()
		return
	}

	s := summarize.NewFromString(formatting.Title, readeef.StripTags(formatting.Content))

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

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

	resp.val["KeyPoints"] = keyPoints
	resp.val["Content"] = formatting.Content
	resp.val["TopImage"] = formatting.TopImage
	resp.val["Id"] = id
	return
}
Esempio 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
}