87 lines
1.7 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"
"runtime"
"time"
"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 11:23:54 +00:00
networkRateLimit = 100 * time.Millisecond
avatarsDirectoryOriginal = "images/avatars/large/original/"
avatarsDirectoryWebP = "images/avatars/large/webp/"
2017-06-12 18:06:31 +00:00
)
2017-06-13 11:23:54 +00:00
var avatarSources []AvatarSource
var avatarWriters []AvatarWriter
// 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{},
}
// 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) {
for _, source := range avatarSources {
avatar := source.GetAvatar(user)
if avatar == nil {
fmt.Println(color.RedString("✘"), user.Nick)
continue
}
fmt.Println(color.GreenString("✔"), user.Nick, "|", avatar.Format, avatar.Image.Bounds().Dx(), avatar.Image.Bounds().Dy())
for _, writer := range avatarWriters {
writer.SaveAvatar(avatar)
}
return
2017-06-12 18:06:31 +00:00
}
2017-06-13 11:23:54 +00:00
// if downloadAvatar(user) {
// makeWebPAvatar(user)
// user.Avatar = "/+" + user.Nick + "/avatar"
// } else {
// user.Avatar = ""
// }
// user.Save()
2017-06-12 18:06:31 +00:00
}