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 }
func updateArticle(a content.Article, tx *sqlx.Tx, db *db.DB, logger webfw.Logger) { if a.HasErr() { return } if err := a.Validate(); err != nil { a.Err(err) return } logger.Infof("Updating article %s\n", a) d := a.Data() s := db.SQL() stmt, err := tx.Preparex(s.Article.Update) if err != nil { a.Err(err) return } defer stmt.Close() res, err := stmt.Exec(d.Title, d.Description, d.Date, d.Guid, d.Link, d.FeedId) if err != nil { a.Err(err) return } if num, err := res.RowsAffected(); err != nil && err == sql.ErrNoRows || num == 0 { logger.Infof("Creating article %s\n", a) aId, err := db.CreateWithId(tx, s.Article.Create, d.FeedId, d.Link, d.Guid, d.Title, d.Description, d.Date) if err != nil { a.Err(fmt.Errorf("Error updating article %s (guid - %v, link - %s): %v", a, d.Guid, d.Link, err)) return } d.Id = data.ArticleId(aId) d.IsNew = true a.Data(d) } }