From bccee5968b780e54c321320ceea587843d8d8190 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 15 Jun 2017 16:56:06 +0200 Subject: [PATCH] Improved logging in avatar code --- jobs/avatars/Avatar.go | 10 +++++++++- jobs/avatars/Gravatar.go | 1 + jobs/avatars/MyAnimeList.go | 11 ++++++++++- jobs/avatars/main.go | 5 +++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/jobs/avatars/Avatar.go b/jobs/avatars/Avatar.go index 80ba48ce..2b82c650 100644 --- a/jobs/avatars/Avatar.go +++ b/jobs/avatars/Avatar.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "image" + "net/http" "github.com/animenotifier/arn" "github.com/parnurzeal/gorequest" @@ -27,7 +28,13 @@ func AvatarFromURL(url string, user *arn.User) *Avatar { // Download response, data, networkErr := gorequest.New().Get(url).EndBytes() - if networkErr != nil || response.StatusCode != 200 { + if networkErr != nil { + avatarLog.Error("NET", user.Nick, url, networkErr) + return nil + } + + if response.StatusCode != http.StatusOK { + avatarLog.Error("NET", user.Nick, url, response.StatusCode) return nil } @@ -35,6 +42,7 @@ func AvatarFromURL(url string, user *arn.User) *Avatar { img, format, decodeErr := image.Decode(bytes.NewReader(data)) if decodeErr != nil { + avatarLog.Error("IMG", user.Nick, url, decodeErr) return nil } diff --git a/jobs/avatars/Gravatar.go b/jobs/avatars/Gravatar.go index 7389e230..44c3fbaa 100644 --- a/jobs/avatars/Gravatar.go +++ b/jobs/avatars/Gravatar.go @@ -18,6 +18,7 @@ type Gravatar struct { func (source *Gravatar) GetAvatar(user *arn.User) *Avatar { // If the user has no Email registered we can't get a Gravatar. if user.Email == "" { + avatarLog.Error("GRA", user.Nick, "No Email") return nil } diff --git a/jobs/avatars/MyAnimeList.go b/jobs/avatars/MyAnimeList.go index fd4cedb6..d38f6e0a 100644 --- a/jobs/avatars/MyAnimeList.go +++ b/jobs/avatars/MyAnimeList.go @@ -1,6 +1,7 @@ package main import ( + "net/http" "regexp" "time" @@ -21,6 +22,7 @@ func (source *MyAnimeList) GetAvatar(user *arn.User) *Avatar { // If the user has no username we can't get an avatar. if malNick == "" { + avatarLog.Error("MAL", user.Nick, "No MAL nick") return nil } @@ -28,7 +30,13 @@ func (source *MyAnimeList) GetAvatar(user *arn.User) *Avatar { userInfoURL := "https://myanimelist.net/malappinfo.php?u=" + malNick response, xml, networkErr := gorequest.New().Get(userInfoURL).End() - if networkErr != nil || response.StatusCode != 200 { + if networkErr != nil { + avatarLog.Error("MAL", user.Nick, userInfoURL, networkErr) + return nil + } + + if response.StatusCode != http.StatusOK { + avatarLog.Error("MAL", user.Nick, userInfoURL, response.StatusCode) return nil } @@ -36,6 +44,7 @@ func (source *MyAnimeList) GetAvatar(user *arn.User) *Avatar { matches := userIDRegex.FindStringSubmatch(xml) if matches == nil || len(matches) < 2 { + avatarLog.Error("MAL", user.Nick, "Could not find user ID") return nil } diff --git a/jobs/avatars/main.go b/jobs/avatars/main.go index 6962d06c..99a63fa4 100644 --- a/jobs/avatars/main.go +++ b/jobs/avatars/main.go @@ -11,6 +11,7 @@ import ( _ "image/jpeg" _ "image/png" + "github.com/aerogo/log" "github.com/animenotifier/arn" "github.com/fatih/color" ) @@ -21,6 +22,7 @@ const ( var avatarSources []AvatarSource var avatarOutputs []AvatarOutput +var avatarLog = log.NewChannel("avatar") // Main func main() { @@ -29,6 +31,9 @@ func main() { // Switch to main directory os.Chdir("../../") + // Log + avatarLog.AddOutput(log.File("logs/avatar.log")) + // Define the avatar sources avatarSources = []AvatarSource{ &Gravatar{