Deleted avatars job & bot

This commit is contained in:
Eduard Urbach 2018-03-04 02:08:33 +01:00
parent 46bde58aaf
commit 3d1f3b92db
3 changed files with 0 additions and 185 deletions

View File

@ -1,70 +0,0 @@
package main
import (
"encoding/json"
"flag"
"io"
"log"
"net/http"
"os"
"path"
"strings"
"github.com/animenotifier/arn"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/animenotifier/avatar/lib"
)
var port = "8001"
func init() {
flag.StringVar(&port, "port", "", "Port the HTTP server should listen on")
flag.Parse()
}
func main() {
// Switch to main directory
exe, err := os.Executable()
if err != nil {
panic(err)
}
root := path.Dir(exe)
os.Chdir(path.Join(root, "../../"))
// Start server
http.HandleFunc("/", onRequest)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
// onRequest handles requests and refreshes the requested avatar.
func onRequest(w http.ResponseWriter, req *http.Request) {
// User ID is simply the path without the slash
userID := strings.TrimPrefix(req.URL.Path, "/")
// Get user from database
user, err := arn.GetUser(userID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
// Refresh
lib.RefreshAvatar(user)
// Send JSON response
buffer, err := json.Marshal(user.Avatar)
if err != nil {
io.WriteString(w, err.Error())
}
w.Write(buffer)
}

View File

@ -1,69 +0,0 @@
package main
import (
"os"
"path"
"runtime"
"sync"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/aerogo/log"
"github.com/animenotifier/arn"
"github.com/animenotifier/avatar/lib"
"github.com/fatih/color"
)
var wg sync.WaitGroup
// Main
func main() {
color.Yellow("Generating user avatars")
defer arn.Node.Close()
// Switch to main directory
exe, err := os.Executable()
if err != nil {
panic(err)
}
root := path.Dir(exe)
os.Chdir(path.Join(root, "../../"))
// Log
lib.Log.AddOutput(log.File("logs/avatar.log"))
defer lib.Log.Flush()
if InvokeShellArgs() {
return
}
// Worker queue
usersQueue := make(chan *arn.User, runtime.NumCPU())
StartWorkers(usersQueue, lib.RefreshAvatar)
// We'll send each user to one of the worker threads
for user := range arn.StreamUsers() {
wg.Add(1)
usersQueue <- user
}
wg.Wait()
color.Green("Finished.")
}
// StartWorkers creates multiple workers to handle a user each.
func StartWorkers(queue chan *arn.User, work func(*arn.User)) {
for w := 0; w < runtime.NumCPU(); w++ {
go func() {
for user := range queue {
work(user)
wg.Done()
}
}()
}
}

View File

@ -1,46 +0,0 @@
package main
import (
"flag"
"github.com/animenotifier/arn"
"github.com/animenotifier/avatar/lib"
)
// Shell parameters
var userID string
var userNick string
// Shell flags
func init() {
flag.StringVar(&userID, "id", "", "ID of the user whose avatar you want to refresh")
flag.StringVar(&userNick, "nick", "", "Nickname of the user whose avatar you want to refresh")
flag.Parse()
}
// InvokeShellArgs ...
func InvokeShellArgs() bool {
if userID != "" {
user, err := arn.GetUser(userID)
if err != nil {
panic(err)
}
lib.RefreshAvatar(user)
return true
}
if userNick != "" {
user, err := arn.GetUserByNick(userNick)
if err != nil {
panic(err)
}
lib.RefreshAvatar(user)
return true
}
return false
}