Retrieving User stats from MAL with a discord bot

1k Views Asked by At

I am trying to find a way to retrieve user stats from a given user's "My Anime List" profile and can't figure out how to retrieve the data. I am looking to find their completed, watching, dropped etc from their profile (Found using MyAnimeList.net/profile/<insert user name here>).

How would I go about doing this?

1

There are 1 best solutions below

1
On BEST ANSWER

The official MyAnimeList API doesn't have any endpoints for getting user profile info. What you can do is scrape the webpage.

  • Thankfully, MyAnimeList can retrieve XML for user profiles, so it's much easier than parsing HTML. Here's an example.
  • After retrieving the XML profile, you can parse it in C# using classes/methods in the System.Xml namespace, such as XDocument.Parse(). There's a lot of resources for help doing that here on StackOverflow.

If you're unsure of how to get this data from the web in the first place, I'd recommend looking into libraries like RestSharp or Flurl. They abstract a way a lot of the boilerplate for getting info from the web for you so you can just focus on coding. If you want to skip both the XML parsing and web requests altogether, you can let a library do it for you.

To keep your Discord.NET bot clean, I'd recommend having a MyAnimeListService class that you do this web request/scraping in. You can then inject it into the module you're using for your (I assume) !anime command and focus on using the MAL profile data there. This way, you're separating the logic of getting the data from the logic of presenting the data to your Discord users. You can read about dependency injection in Discord.NET here. Assuming you're using C#, the end result would be accessing the module like this:

[Group("anime")]
public class AnimeModule : Module
{
    private AnimeModule(MyAnimeListService service)
    {
        Service = service;
    }

    private MyAnimeListService Service { get; }

    [Command]
    public async Task AnimeCommand(string username)
    {
        // call your service here
    }
}