From 3d1f3b92dbe534e4301db34a7b4f58553d667d52 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 4 Mar 2018 02:08:33 +0100 Subject: [PATCH] Deleted avatars job & bot --- bots/avatars/avatars.go | 70 ----------------------------------------- jobs/avatars/avatars.go | 69 ---------------------------------------- jobs/avatars/shell.go | 46 --------------------------- 3 files changed, 185 deletions(-) delete mode 100644 bots/avatars/avatars.go delete mode 100644 jobs/avatars/avatars.go delete mode 100644 jobs/avatars/shell.go diff --git a/bots/avatars/avatars.go b/bots/avatars/avatars.go deleted file mode 100644 index db987a8c..00000000 --- a/bots/avatars/avatars.go +++ /dev/null @@ -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) -} diff --git a/jobs/avatars/avatars.go b/jobs/avatars/avatars.go deleted file mode 100644 index f0fb8c7c..00000000 --- a/jobs/avatars/avatars.go +++ /dev/null @@ -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() - } - }() - } -} diff --git a/jobs/avatars/shell.go b/jobs/avatars/shell.go deleted file mode 100644 index da798e98..00000000 --- a/jobs/avatars/shell.go +++ /dev/null @@ -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 -}