137 lines
2.6 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-15 14:56:06 +00:00
"github.com/aerogo/log"
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 15:06:30 +00:00
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-21 18:33:57 +00:00
var avatarLog = log.New()
2017-06-13 11:23:54 +00:00
// Main
2017-06-12 18:06:31 +00:00
func main() {
2017-06-14 15:16:03 +00:00
color.Yellow("Generating user avatars")
2017-06-13 11:23:54 +00:00
// Switch to main directory
2017-06-12 22:06:35 +00:00
os.Chdir("../../")
2017-06-15 14:56:06 +00:00
// Log
avatarLog.AddOutput(log.File("logs/avatar.log"))
2017-06-15 15:03:22 +00:00
defer avatarLog.Flush()
2017-06-15 14:56:06 +00:00
2017-06-13 11:23:54 +00:00
// Define the avatar sources
avatarSources = []AvatarSource{
2017-06-13 15:06:30 +00:00
&Gravatar{
Rating: "pg",
2017-06-15 17:22:09 +00:00
RequestLimiter: time.NewTicker(250 * time.Millisecond),
2017-06-13 15:06:30 +00:00
},
&MyAnimeList{
2017-06-15 17:22:09 +00:00
RequestLimiter: time.NewTicker(250 * time.Millisecond),
2017-06-13 15:06:30 +00:00
},
2017-06-13 11:23:54 +00:00
}
2017-06-13 12:47:17 +00:00
// Define the avatar outputs
avatarOutputs = []AvatarOutput{
2017-06-13 15:06:30 +00:00
// Original - Large
2017-06-13 12:47:17 +00:00
&AvatarOriginalFileOutput{
Directory: "images/avatars/large/original/",
Size: arn.AvatarMaxSize,
},
2017-06-13 15:06:30 +00:00
// Original - Small
2017-06-13 12:47:17 +00:00
&AvatarOriginalFileOutput{
Directory: "images/avatars/small/original/",
Size: arn.AvatarSmallSize,
},
2017-06-13 15:06:30 +00:00
// WebP - Large
2017-06-13 12:47:17 +00:00
&AvatarWebPFileOutput{
Directory: "images/avatars/large/webp/",
Size: arn.AvatarMaxSize,
Quality: webPQuality,
},
2017-06-13 15:06:30 +00:00
// WebP - Small
2017-06-13 12:47:17 +00:00
&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-15 15:03:22 +00:00
users, _ := arn.FilterUsers(func(user *arn.User) bool {
return true
})
// Log user count
println(len(users), "users")
2017-06-12 18:06:31 +00:00
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 15:06:30 +00:00
StartWorkers(usersQueue, 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
2017-06-15 15:03:22 +00:00
for _, user := range users {
2017-06-13 11:23:54 +00:00
usersQueue <- user
}
2017-06-14 15:16:03 +00:00
color.Green("Finished.")
2017-06-13 11:23:54 +00:00
}
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.
2017-06-13 15:06:30 +00:00
func StartWorkers(queue chan *arn.User, work func(*arn.User)) {
2017-06-13 11:23:54 +00:00
for w := 0; w < runtime.NumCPU(); w++ {
go func() {
for user := range queue {
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 15:06:30 +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 15:06:30 +00:00
fmt.Println(color.GreenString("✔"), reflect.TypeOf(source).Elem().Name(), "|", user.Nick, "|", avatar)
2017-06-13 12:47:17 +00:00
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
}