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"
"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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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{