-
Notifications
You must be signed in to change notification settings - Fork 0
MangaDex Library
The MangaDex Library is a library I wrote to facilitate getting data from the MangaDex API more easily and seamlessly, without having to write and re-write the code that gets and processes the result. It has a rate limiter that is in accordance with the MangaDex API limits, as well as three major components, each being its own class. Here I will outline what each of them do, as well as expected usage.
The only external dependency is Newtonsoft.Json.
This class is the one which must be called often, and at least once to initialize the library. The user is expected to call the method SetUserAgent(string userAgent)
once; this will set the User Agent for the HttpClient
, as well as add the header application/json
, which is required to report page downloads to the API. If this is never called, the library will throw an error. Other than that, there are three other variables that can be accessed:
- (string)MangaID - sets the ID of the manga for which the API will be called in all calls
- (string)Language - sets the language for which to try to get the chapters and titles of the manga; the language must be set as the language's
TwoLetterISOLanguageName
, including MangaDex's exceptions
This is the class which will obtain and return the data from the API. It "caches" the last manga's results, so only one API call will be made for each of the three types of call, after which all the data, whether it is used or not, is kept. To force the library to update the data again, you can call the ForceReset
method. The three sections, and what they offer are:
This calls the manga/
endpoint. The methods that can be called are (yes, they all start with "Get", yes this is redundant, I am not changing it):
- (List<string>)GetTitles - returns the main display title as the first entry, after which are all the subtitles, in the selected language if available
- (string)GetDescription - returns the manga's descriprion as listed on it MangaDex page, in the selected language if available
- (string)GetOriginalLanguage - returns the manga's original language in the same
TwoLetterISOLanguageName
format - (string)GetPublicationDemographic
- (string)GetStatus
- (string)GetContentRating
- (List<string>)GetTags - returns a string list of the tags that are of types
genre
ortheme
; theformat
tags are ignored - (string)GetCreationDate()
- (List<string>)GetAuthors
- (List<string>)GetArtists
- (Stream)GetCover - returns a stream of the cover shown on the manga's page
This calls the feed/
endpoint. The methods that can be called are:
- (List<string>)GetChapterIDs
- (List<decimal>)GetChapterNumbers
- (List<string>)GetChapterTitles
- (List<int>)GetChapterNrPages - returns an int list of how many pages each chapter has
- (List<string>)GetChapterGroups - returns a string list of the scanlator group of each chapter
- (decimal)GetLastChapter - returns the latest chapter number
- (List<int>)GetCuratedChapterIndexes - returns an int list of the indexes of the chapters that should be auto-selected for downloading; for chapters with multiple scanlators it prefers the group that scanlated the most chapters up to each chapter
- (List<int>)GetCuratedChapterIndexes - same as above, however this prefers the group that scanlated the most chapters in the entire manga
This calls the at-home/server/
endpoint. The methods that can be called are:
- (List<string>)GetPageLinks(string chapterID) - returns a string list of all the chapter images, as given by the API; the links are fully assembled and ready to use
- (Stream)GetPageImage(string link) - returns a stream of the page image at the given link
Here is an example intended use-case, which does not have error handling:
MDLParameters.SetUserAgent("example user agent");
MDLParameters.MangaID = "example-id";
List<string> pageLinks = MDLGetData.GetPageLinks(chapterID); // This must only be called once, because every time it will prompt the API to generate new links. This should only be called again for the same chapter ID if the downloads fail.
foreach (string link in pageLinks)
{
Bitmap pageImage = new Bitmap(MDLGetData.GetPageImage(link));
pageImage.Save(Path.GetFileName(link));
}
When the program encounters an error from the API, it will call the event ApiRequestFailed
which can be subscribed to. If a page download fails, it will instead raise the event DownloadError
, so you can call GetPageLinks(string chapterID)
again for a retry. The library will report back to the API regardless.