Avatar improvements

This commit is contained in:
2017-06-13 00:06:35 +02:00
parent d3f49ad53d
commit 5d97a87152
11 changed files with 171 additions and 145 deletions

View File

@ -1,18 +1,21 @@
package main
import (
"io/ioutil"
"os"
"runtime"
"time"
"github.com/animenotifier/arn"
"github.com/fatih/color"
"github.com/parnurzeal/gorequest"
gravatar "github.com/ungerik/go-gravatar"
)
const (
avatarsDirectoryOriginal = "images/avatars/original/"
avatarsDirectoryWebP = "images/avatars/webp/"
)
func main() {
os.Chdir("../../")
users, _ := arn.AllUsers()
usersQueue := make(chan *arn.User)
@ -23,6 +26,7 @@ func main() {
go func(workerID int) {
for user := range usersQueue {
<-rateLimiter.C
if downloadAvatar(user) {
makeWebPAvatar(user)
user.Avatar = "/+" + user.Nick + "/avatar"
@ -39,90 +43,3 @@ func main() {
usersQueue <- user
}
}
func findAvatar(user *arn.User, dir string) string {
testExtensions := []string{".jpg", ".png", ".gif", ".webp", ""}
for _, testExt := range testExtensions {
if _, err := os.Stat(dir + user.ID + testExt); !os.IsNotExist(err) {
return user.ID + testExt
}
}
return ""
}
func makeWebPAvatar(user *arn.User) {
baseName := findAvatar(user, "../../images/avatars/original/")
if baseName == "" {
return
}
original := "../../images/avatars/original/" + baseName
outFile := "../../images/avatars/webp/" + user.ID + ".webp"
err := convertFileToWebP(original, outFile, 80)
if err != nil {
color.Red("[WebP] " + original + " -> " + outFile)
} else {
color.Green("[WebP] " + original + " -> " + outFile)
}
}
func downloadAvatar(user *arn.User) bool {
if user.Email == "" {
return false
}
directory := "../../images/avatars/original/"
fileName := directory + user.ID
// Build URL
url := gravatar.Url(user.Email) + "?s=560&d=404&r=pg"
// Skip existing avatars
if findAvatar(user, directory) != "" {
color.Yellow(url)
return true
}
// Download
response, data, err := gorequest.New().Get(url).EndBytes()
if err != nil {
color.Red(url)
return false
}
contentType := response.Header.Get("content-type")
if response.StatusCode != 200 {
color.Red(url)
return false
}
color.Green(url)
// Determine file extension
extension := ""
switch contentType {
case "image/jpeg":
extension = ".jpg"
case "image/png":
extension = ".png"
case "image/gif":
extension = ".gif"
case "image/webp":
extension = ".webp"
}
fileName += extension
// Write to disk
ioutil.WriteFile(fileName, data, 0644)
return true
}