Browse Source

feeds(twitter): improve support for twitter avatars (#16)

As mentioned in the [twtxt thread](https://twtxt.net/twt/ed6sbiq), certain twitter feeds weren't getting their avatars properly handeld. After investigation, I found that this was due to a `Feed{}` being returned from `ValidateTwitterFeed` that was missing a preliminary `Avatar` field, resulting in a missing `Avatar` field until `feeds` was eventually restarted. I tested it and this change should fix that issue.

Also a minor change was introduced; if a twitter avatar was already downloaded, it won't be downloaded again. Prior behavior was to download it every time the feed was updated; this may have resulted in ratelimiting.

Reviewed-on: #16
Co-authored-by: servusdei2018 <servusdei2018@noreply@mills.io>
Co-committed-by: servusdei2018 <servusdei2018@noreply@mills.io>
pull/17/head
servusdei2018 1 week ago
committed by James Mills
parent
commit
e7e7680bd0
  1. 34
      feeds.go

34
feeds.go

@ -62,10 +62,10 @@ func TestTwitterFeed(handle string) error {
return fmt.Errorf("error scraping tweets from %s: %w", handle, tweet.Error)
}
count++
if tweet.IsRetweet {
continue
}
count++
}
if count == 0 {
@ -160,8 +160,9 @@ func ValidateTwitterFeed(conf *Config, handle string) (Feed, error) {
log.WithError(err).Warnf("error downloading feed image from %s", profile.Avatar)
}
return Feed{Name: name, URI: uri}, nil
return Feed{Name: name, URI: uri, Avatar: fmt.Sprintf("%s/%s/avatar.png", conf.BaseURL, name)}, nil
}
// ValidateFeed ...
func ValidateRSSFeed(conf *Config, uri string) (Feed, error) {
feed, err := TestRSSFeed(uri)
@ -256,21 +257,24 @@ func UpdateTwitterFeed(conf *Config, name, handle string) error {
}
}
opts := &ImageOptions{
Resize: true,
ResizeW: avatarResolution,
ResizeH: avatarResolution,
}
profile, err := twitterscraper.GetProfile(handle)
if err != nil {
log.WithError(err).Warnf("error retrieving twitter profile for %s", handle)
}
avatarFile := filepath.Join(conf.DataDir, fmt.Sprintf("%s.png", name))
if !Exists(avatarFile) {
filename := fmt.Sprintf("%s.png", name)
filename := fmt.Sprintf("%s.png", name)
opts := &ImageOptions{
Resize: true,
ResizeW: avatarResolution,
ResizeH: avatarResolution,
}
profile, err := twitterscraper.GetProfile(handle)
if err != nil {
log.WithError(err).Warnf("error retrieving twitter profile for %s", handle)
}
if err := DownloadImage(conf, profile.Avatar, filename, opts); err != nil {
log.WithError(err).Warnf("error downloading feed image from %s", profile.Avatar)
if err := DownloadImage(conf, profile.Avatar, filename, opts); err != nil {
log.WithError(err).Warnf("error downloading feed image from %s", profile.Avatar)
}
}
if (old + new) == 0 {

Loading…
Cancel
Save