113 lines
2.2 KiB
Go
Raw Normal View History

2017-06-12 18:06:31 +00:00
package main
import (
2017-06-13 11:23:54 +00:00
"fmt"
2017-06-12 18:06:31 +00:00
"os"
2017-06-13 12:47:17 +00:00
"reflect"
2017-06-12 18:06:31 +00:00
"runtime"
"time"
2017-06-13 12:47:17 +00:00
_ "image/gif"
_ "image/jpeg"
_ "image/png"
2017-06-12 18:06:31 +00:00
"github.com/animenotifier/arn"
2017-06-13 11:23:54 +00:00
"github.com/fatih/color"
2017-06-12 22:06:35 +00:00
)
const (
2017-06-13 12:47:17 +00:00
networkRateLimit = 100 * time.Millisecond
webPQuality = 80
2017-06-12 18:06:31 +00:00
)
2017-06-13 11:23:54 +00:00
var avatarSources []AvatarSource
2017-06-13 12:47:17 +00:00
var avatarOutputs []AvatarOutput
2017-06-13 11:23:54 +00:00
// Main
2017-06-12 18:06:31 +00:00
func main() {
2017-06-13 11:23:54 +00:00
// Switch to main directory
2017-06-12 22:06:35 +00:00
os.Chdir("../../")
2017-06-13 11:23:54 +00:00
// Define the avatar sources
avatarSources = []AvatarSource{
&Gravatar{},
}
2017-06-13 12:47:17 +00:00
// Define the avatar outputs
avatarOutputs = []AvatarOutput{
&AvatarOriginalFileOutput{
Directory: "images/avatars/large/original/",
Size: arn.AvatarMaxSize,
},
&AvatarOriginalFileOutput{
Directory: "images/avatars/small/original/",
Size: arn.AvatarSmallSize,
},
&AvatarWebPFileOutput{
Directory: "images/avatars/large/webp/",
Size: arn.AvatarMaxSize,
Quality: webPQuality,
},
&AvatarWebPFileOutput{
Directory: "images/avatars/small/webp/",
Size: arn.AvatarSmallSize,
Quality: webPQuality,
},
}
2017-06-13 11:23:54 +00:00
// Stream of all users
2017-06-12 18:06:31 +00:00
users, _ := arn.AllUsers()
2017-06-13 11:23:54 +00:00
// Worker queue
2017-06-12 18:06:31 +00:00
usersQueue := make(chan *arn.User)
2017-06-13 11:23:54 +00:00
StartWorkers(usersQueue, networkRateLimit, Work)
2017-06-12 18:06:31 +00:00
2017-06-13 11:23:54 +00:00
// We'll send each user to one of the worker threads
for user := range users {
usersQueue <- user
}
}
2017-06-12 22:06:35 +00:00
2017-06-13 11:23:54 +00:00
// StartWorkers creates multiple workers to handle a user each.
func StartWorkers(queue chan *arn.User, rateLimit time.Duration, work func(*arn.User)) {
rateLimiter := time.NewTicker(rateLimit)
2017-06-12 19:11:20 +00:00
2017-06-13 11:23:54 +00:00
for w := 0; w < runtime.NumCPU(); w++ {
go func() {
for user := range queue {
<-rateLimiter.C
work(user)
2017-06-12 18:06:31 +00:00
}
2017-06-13 11:23:54 +00:00
}()
2017-06-12 18:06:31 +00:00
}
2017-06-13 11:23:54 +00:00
}
2017-06-12 18:06:31 +00:00
2017-06-13 11:23:54 +00:00
// Work handles a single user.
func Work(user *arn.User) {
2017-06-13 12:47:17 +00:00
user.Avatar = ""
2017-06-13 11:23:54 +00:00
for _, source := range avatarSources {
avatar := source.GetAvatar(user)
if avatar == nil {
2017-06-13 12:47:17 +00:00
fmt.Println(color.RedString("✘"), reflect.TypeOf(source).Elem().Name(), user.Nick)
2017-06-13 11:23:54 +00:00
continue
}
2017-06-13 12:47:17 +00:00
for _, writer := range avatarOutputs {
err := writer.SaveAvatar(avatar)
2017-06-13 11:23:54 +00:00
2017-06-13 12:47:17 +00:00
if err != nil {
color.Red(err.Error())
}
2017-06-13 11:23:54 +00:00
}
2017-06-13 12:47:17 +00:00
fmt.Println(color.GreenString("✔"), user.Nick, "|", avatar.Format, avatar.Image.Bounds().Dx(), avatar.Image.Bounds().Dy())
user.Avatar = "/+" + user.Nick + "/avatar"
break
2017-06-12 18:06:31 +00:00
}
2017-06-13 11:23:54 +00:00
2017-06-13 12:47:17 +00:00
// Save avatar data
user.Save()
2017-06-12 18:06:31 +00:00
}