Added MAL avatar source

This commit is contained in:
Eduard Urbach 2017-06-13 17:06:30 +02:00
parent 688c09760b
commit 6836fea857
4 changed files with 111 additions and 34 deletions

View File

@ -1,9 +1,12 @@
package main package main
import ( import (
"bytes"
"fmt"
"image" "image"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/parnurzeal/gorequest"
) )
// Avatar represents a single image and the name of the format. // Avatar represents a single image and the name of the format.
@ -13,3 +16,32 @@ type Avatar struct {
Data []byte Data []byte
Format string Format string
} }
// String returns a text representation of the format, width and height.
func (avatar *Avatar) String() string {
return fmt.Sprint(avatar.Format, " | ", avatar.Image.Bounds().Dx(), "x", avatar.Image.Bounds().Dy())
}
// AvatarFromURL downloads and decodes the image from an URL and creates an Avatar.
func AvatarFromURL(url string, user *arn.User) *Avatar {
// Download
response, data, networkErr := gorequest.New().Get(url).EndBytes()
if networkErr != nil || response.StatusCode != 200 {
return nil
}
// Decode
img, format, decodeErr := image.Decode(bytes.NewReader(data))
if decodeErr != nil {
return nil
}
return &Avatar{
User: user,
Image: img,
Data: data,
Format: format,
}
}

View File

@ -1,17 +1,18 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"image" "time"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/parnurzeal/gorequest"
gravatar "github.com/ungerik/go-gravatar" gravatar "github.com/ungerik/go-gravatar"
) )
// Gravatar - https://gravatar.com/ // Gravatar - https://gravatar.com/
type Gravatar struct{} type Gravatar struct {
Rating string
RequestLimiter *time.Ticker
}
// GetAvatar returns the Gravatar image for a user (if available). // GetAvatar returns the Gravatar image for a user (if available).
func (source *Gravatar) GetAvatar(user *arn.User) *Avatar { func (source *Gravatar) GetAvatar(user *arn.User) *Avatar {
@ -21,26 +22,11 @@ func (source *Gravatar) GetAvatar(user *arn.User) *Avatar {
} }
// Build URL // Build URL
gravatarURL := gravatar.Url(user.Email) + "?s=" + fmt.Sprint(arn.AvatarMaxSize) + "&d=404&r=pg" gravatarURL := gravatar.Url(user.Email) + "?s=" + fmt.Sprint(arn.AvatarMaxSize) + "&d=404&r=" + source.Rating
// Wait for request limiter to allow us to send a request
<-source.RequestLimiter.C
// Download // Download
response, data, networkErr := gorequest.New().Get(gravatarURL).EndBytes() return AvatarFromURL(gravatarURL, user)
if networkErr != nil || response.StatusCode != 200 {
return nil
}
// Decode
img, format, decodeErr := image.Decode(bytes.NewReader(data))
if decodeErr != nil {
return nil
}
return &Avatar{
User: user,
Image: img,
Data: data,
Format: format,
}
} }

View File

@ -0,0 +1,50 @@
package main
import (
"regexp"
"time"
"github.com/animenotifier/arn"
"github.com/parnurzeal/gorequest"
)
var userIDRegex = regexp.MustCompile(`<user_id>(\d+)<\/user_id>`)
// MyAnimeList - https://myanimelist.net/
type MyAnimeList struct {
RequestLimiter *time.Ticker
}
// GetAvatar returns the Gravatar image for a user (if available).
func (source *MyAnimeList) GetAvatar(user *arn.User) *Avatar {
malNick := user.Accounts.MyAnimeList.Nick
// If the user has no username we can't get an avatar.
if malNick == "" {
return nil
}
// Download user info
userInfoURL := "https://myanimelist.net/malappinfo.php?u=" + malNick
response, xml, networkErr := gorequest.New().Get(userInfoURL).End()
if networkErr != nil || response.StatusCode != 200 {
return nil
}
// Build URL
matches := userIDRegex.FindStringSubmatch(xml)
if matches == nil || len(matches) < 2 {
return nil
}
malID := matches[1]
malAvatarURL := "https://myanimelist.cdn-dena.com/images/userimages/" + malID + ".jpg"
// Wait for request limiter to allow us to send a request
<-source.RequestLimiter.C
// Download
return AvatarFromURL(malAvatarURL, user)
}

View File

@ -16,7 +16,6 @@ import (
) )
const ( const (
networkRateLimit = 100 * time.Millisecond
webPQuality = 80 webPQuality = 80
) )
@ -30,24 +29,37 @@ func main() {
// Define the avatar sources // Define the avatar sources
avatarSources = []AvatarSource{ avatarSources = []AvatarSource{
&Gravatar{}, &Gravatar{
Rating: "pg",
RequestLimiter: time.NewTicker(250 * time.Millisecond),
},
&MyAnimeList{
RequestLimiter: time.NewTicker(500 * time.Millisecond),
},
} }
// Define the avatar outputs // Define the avatar outputs
avatarOutputs = []AvatarOutput{ avatarOutputs = []AvatarOutput{
// Original - Large
&AvatarOriginalFileOutput{ &AvatarOriginalFileOutput{
Directory: "images/avatars/large/original/", Directory: "images/avatars/large/original/",
Size: arn.AvatarMaxSize, Size: arn.AvatarMaxSize,
}, },
// Original - Small
&AvatarOriginalFileOutput{ &AvatarOriginalFileOutput{
Directory: "images/avatars/small/original/", Directory: "images/avatars/small/original/",
Size: arn.AvatarSmallSize, Size: arn.AvatarSmallSize,
}, },
// WebP - Large
&AvatarWebPFileOutput{ &AvatarWebPFileOutput{
Directory: "images/avatars/large/webp/", Directory: "images/avatars/large/webp/",
Size: arn.AvatarMaxSize, Size: arn.AvatarMaxSize,
Quality: webPQuality, Quality: webPQuality,
}, },
// WebP - Small
&AvatarWebPFileOutput{ &AvatarWebPFileOutput{
Directory: "images/avatars/small/webp/", Directory: "images/avatars/small/webp/",
Size: arn.AvatarSmallSize, Size: arn.AvatarSmallSize,
@ -60,7 +72,7 @@ func main() {
// Worker queue // Worker queue
usersQueue := make(chan *arn.User) usersQueue := make(chan *arn.User)
StartWorkers(usersQueue, networkRateLimit, Work) StartWorkers(usersQueue, Work)
// We'll send each user to one of the worker threads // We'll send each user to one of the worker threads
for user := range users { for user := range users {
@ -69,13 +81,10 @@ func main() {
} }
// StartWorkers creates multiple workers to handle a user each. // StartWorkers creates multiple workers to handle a user each.
func StartWorkers(queue chan *arn.User, rateLimit time.Duration, work func(*arn.User)) { func StartWorkers(queue chan *arn.User, work func(*arn.User)) {
rateLimiter := time.NewTicker(rateLimit)
for w := 0; w < runtime.NumCPU(); w++ { for w := 0; w < runtime.NumCPU(); w++ {
go func() { go func() {
for user := range queue { for user := range queue {
<-rateLimiter.C
work(user) work(user)
} }
}() }()
@ -90,7 +99,7 @@ func Work(user *arn.User) {
avatar := source.GetAvatar(user) avatar := source.GetAvatar(user)
if avatar == nil { if avatar == nil {
fmt.Println(color.RedString("✘"), reflect.TypeOf(source).Elem().Name(), user.Nick) // fmt.Println(color.RedString("✘"), reflect.TypeOf(source).Elem().Name(), user.Nick)
continue continue
} }
@ -102,7 +111,7 @@ func Work(user *arn.User) {
} }
} }
fmt.Println(color.GreenString("✔"), user.Nick, "|", avatar.Format, avatar.Image.Bounds().Dx(), avatar.Image.Bounds().Dy()) fmt.Println(color.GreenString("✔"), reflect.TypeOf(source).Elem().Name(), "|", user.Nick, "|", avatar)
user.Avatar = "/+" + user.Nick + "/avatar" user.Avatar = "/+" + user.Nick + "/avatar"
break break
} }