Improved logging in avatar code

This commit is contained in:
Eduard Urbach 2017-06-15 16:56:06 +02:00
parent c858a58123
commit bccee5968b
4 changed files with 25 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"image" "image"
"net/http"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/parnurzeal/gorequest" "github.com/parnurzeal/gorequest"
@ -27,7 +28,13 @@ func AvatarFromURL(url string, user *arn.User) *Avatar {
// Download // Download
response, data, networkErr := gorequest.New().Get(url).EndBytes() 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 return nil
} }
@ -35,6 +42,7 @@ func AvatarFromURL(url string, user *arn.User) *Avatar {
img, format, decodeErr := image.Decode(bytes.NewReader(data)) img, format, decodeErr := image.Decode(bytes.NewReader(data))
if decodeErr != nil { if decodeErr != nil {
avatarLog.Error("IMG", user.Nick, url, decodeErr)
return nil return nil
} }

View File

@ -18,6 +18,7 @@ type Gravatar struct {
func (source *Gravatar) GetAvatar(user *arn.User) *Avatar { func (source *Gravatar) GetAvatar(user *arn.User) *Avatar {
// If the user has no Email registered we can't get a Gravatar. // If the user has no Email registered we can't get a Gravatar.
if user.Email == "" { if user.Email == "" {
avatarLog.Error("GRA", user.Nick, "No Email")
return nil return nil
} }

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"net/http"
"regexp" "regexp"
"time" "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 the user has no username we can't get an avatar.
if malNick == "" { if malNick == "" {
avatarLog.Error("MAL", user.Nick, "No MAL nick")
return nil return nil
} }
@ -28,7 +30,13 @@ func (source *MyAnimeList) GetAvatar(user *arn.User) *Avatar {
userInfoURL := "https://myanimelist.net/malappinfo.php?u=" + malNick userInfoURL := "https://myanimelist.net/malappinfo.php?u=" + malNick
response, xml, networkErr := gorequest.New().Get(userInfoURL).End() 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 return nil
} }
@ -36,6 +44,7 @@ func (source *MyAnimeList) GetAvatar(user *arn.User) *Avatar {
matches := userIDRegex.FindStringSubmatch(xml) matches := userIDRegex.FindStringSubmatch(xml)
if matches == nil || len(matches) < 2 { if matches == nil || len(matches) < 2 {
avatarLog.Error("MAL", user.Nick, "Could not find user ID")
return nil return nil
} }

View File

@ -11,6 +11,7 @@ import (
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
"github.com/aerogo/log"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/fatih/color" "github.com/fatih/color"
) )
@ -21,6 +22,7 @@ const (
var avatarSources []AvatarSource var avatarSources []AvatarSource
var avatarOutputs []AvatarOutput var avatarOutputs []AvatarOutput
var avatarLog = log.NewChannel("avatar")
// Main // Main
func main() { func main() {
@ -29,6 +31,9 @@ func main() {
// Switch to main directory // Switch to main directory
os.Chdir("../../") os.Chdir("../../")
// Log
avatarLog.AddOutput(log.File("logs/avatar.log"))
// Define the avatar sources // Define the avatar sources
avatarSources = []AvatarSource{ avatarSources = []AvatarSource{
&Gravatar{ &Gravatar{