61 lines
1.4 KiB
Go
Raw Normal View History

2017-06-13 15:06:30 +00:00
package main
import (
2017-06-15 14:56:06 +00:00
"net/http"
2017-06-13 15:06:30 +00:00
"regexp"
"time"
"github.com/animenotifier/arn"
"github.com/parnurzeal/gorequest"
)
var userIDRegex = regexp.MustCompile(`<user_id>(\d+)<\/user_id>`)
2017-06-16 11:48:11 +00:00
var malLog = avatarLog.NewChannel("MAL")
2017-06-13 15:06:30 +00:00
// MyAnimeList - https://myanimelist.net/
type MyAnimeList struct {
RequestLimiter *time.Ticker
}
// GetAvatar returns the Gravatar image for a user (if available).
func (source *MyAnimeList) GetAvatar(user *arn.User) *Avatar {
malNick := user.Accounts.MyAnimeList.Nick
// If the user has no username we can't get an avatar.
if malNick == "" {
2017-06-16 11:48:11 +00:00
malLog.Error(user.Nick, "No MAL nick")
2017-06-13 15:06:30 +00:00
return nil
}
// Download user info
userInfoURL := "https://myanimelist.net/malappinfo.php?u=" + malNick
response, xml, networkErr := gorequest.New().Get(userInfoURL).End()
2017-06-15 14:56:06 +00:00
if networkErr != nil {
2017-06-16 11:48:11 +00:00
malLog.Error(user.Nick, userInfoURL, networkErr)
2017-06-15 14:56:06 +00:00
return nil
}
if response.StatusCode != http.StatusOK {
2017-06-16 11:48:11 +00:00
malLog.Error(user.Nick, userInfoURL, response.StatusCode)
2017-06-13 15:06:30 +00:00
return nil
}
// Build URL
matches := userIDRegex.FindStringSubmatch(xml)
if matches == nil || len(matches) < 2 {
2017-06-16 11:48:11 +00:00
malLog.Error(user.Nick, "Could not find user ID")
2017-06-13 15:06:30 +00:00
return nil
}
malID := matches[1]
malAvatarURL := "https://myanimelist.cdn-dena.com/images/userimages/" + malID + ".jpg"
// Wait for request limiter to allow us to send a request
<-source.RequestLimiter.C
// Download
return AvatarFromURL(malAvatarURL, user)
}