md-y/mangadex-full-api

Manga.getFeed() is not returning all the available chapters

Closed this issue · 1 comments

I am using this package to make a discord bot for reading manga. I am searching for a manga using the getByQuery method. Then using getFeed to get the chapters. My issue is that getFeed() isnt returning all the available chapters on Mangadex.

here's my code

            const manga = await MFA.Manga.getByQuery(args2.join(' '));
            var chapters = await manga.getFeed({ translatedLanguage: ['en'], order: { chapter: 'asc' } });

            var chapterindex = -1;
            for(var i = 0; i < chapters.length; i++)
            {
                console.log(chapters[i].chapter);
                if(chapters[i].chapter == chapter_num_arg)
                {                    
                    chapterindex = i;
                    break;
                }                
            }

            if(chapterindex == -1)
            {
                return message.channel.send('Chapter not found !');
            }

Here's the output on Discord :
Image

Here's the console output :
image

it starts from 1 but ends at 84.

Here's the chapter list on mangadex :
image

md-y commented

By default, the limit for freed requests is 100, so only the first 100 English chapters every uploaded are returned. You can increase the limit with an argument to the getFeed() function like so:

var chapters = await manga.getFeed({ translatedLanguage: ['en'], order: { chapter: 'asc' }, limit: Infinity });

This will request every chapter and take more time, so a more efficient solution would be something like:

// Increase offset of feed query until the chapter is found or if no chapters are returned
// Null is returned when no chapter is found
// Also sort by most-recent chapters since people are more likely to request them from a Discord bot
const findChapter = async (targetManga, targetChap, offset = 0) => {
   const chapters = await targetManga.getFeed({ translatedLanguage: ['en'], order: { publishAt: 'desc' }, offset: offset, limit: 100 });
   if (chapters.length === 0) return null;
   for (const chap of chapters) if (chap.chapter == targetChap) return chap;
   return findChapter(targetManga, targetChap, offset + 100);
}

const manga = await MFA.Manga.getByQuery('kanojo okarishimasu');
const foundChapter = await findChapter(manga, 105);
console.log(foundChapter);