Example #1
0
func (t Extract) Generate(a content.Article) error {
	ad := a.Data()

	thumbnail := a.Repo().ArticleThumbnail()
	td := data.ArticleThumbnail{
		ArticleId: ad.Id,
		Processed: true,
	}

	t.logger.Debugf("Generating thumbnail for article %s\n", a)

	td.Thumbnail, td.Link =
		generateThumbnailFromDescription(strings.NewReader(ad.Description))

	if td.Link == "" {
		t.logger.Debugf("%s description doesn't contain suitable link, getting extract\n", a)

		extract := a.Extract()
		if a.HasErr() {
			return a.Err()
		}

		extractData := extract.Data()

		if extract.HasErr() {
			switch err := extract.Err(); err {
			case content.ErrNoContent:
				t.logger.Debugf("Generating article extract for %s\n", a)
				extractData, err = t.extractor.Extract(a.Data().Link)
				if err != nil {
					return err
				}

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

		if extractData.TopImage == "" {
			t.logger.Debugf("Extract for %s doesn't contain a top image\n", a)
		} else {
			t.logger.Debugf("Generating thumbnail from top image %s of %s\n", extractData.TopImage, a)
			td.Thumbnail = generateThumbnailFromImageLink(extractData.TopImage)
			td.Link = extractData.TopImage
		}
	}

	thumbnail.Data(td)
	if thumbnail.Update(); thumbnail.HasErr() {
		return fmt.Errorf("Error saving thumbnail of %s to database :%v\n", a, thumbnail.Err())
	}

	return nil
}
Example #2
0
func (a *Article) Thumbnail() (at content.ArticleThumbnail) {
	at = a.Repo().ArticleThumbnail()
	if a.HasErr() {
		at.Err(a.Err())
		return
	}

	id := a.Data().Id
	if id == 0 {
		a.Err(content.NewValidationError(errors.New("Invalid article id")))
		return
	}

	a.logger.Infof("Getting article '%d' thumbnail\n", id)

	var i data.ArticleThumbnail
	if err := a.db.Get(&i, a.db.SQL().Article.GetThumbnail, id); err != nil {
		if err == sql.ErrNoRows {
			err = content.ErrNoContent
		}
		at.Err(err)
	}

	i.ArticleId = id
	at.Data(i)

	return
}