getmango / mango-plugins

MIT License
65 stars 54 forks source link

[Bug] gifs fail to download for nhentai plugin, (I think I found a solution too) #25

Closed zeldatp151 closed 2 years ago

zeldatp151 commented 2 years ago

Plugin Name/ID of the plugin. nhentai

Describe the bug A clear and concise description of what the bug is. Plugin fails to download gifs

To Reproduce Steps to reproduce the behavior:

  1. try to download a manga/hentai with gifs for some of the pages (ex: https://nhentai.net/g/394380/ chosen for this example because it is only 15 pages.) using the nhentai plugin.
  2. check the results in the download manager
  3. it will have downloaded all but 3 pages of that manga, those 3 pages are the only gifs in the manga.

Expected behavior I expect the plugin to be able to download the entire manga, including animated panels

Mango version E.g., v0.26.2

Additional context Add any other context about the problem here.

zeldatp151 commented 2 years ago

UPDATE: Looking at the mango log, it seems the plugin is replacing the ".gif" of the url for the image with ".png" for some reason.

example, the mango log shows `Error: Failed to download page https://i.nhentia.net/galleries/2157775/14.png. [404] Not Found' and it is correct, that brings up a 404 page, but if I go to https://i.nhentia.net/galleries/2157775/14.gif then the image is properly displayed.

zeldatp151 commented 2 years ago

I have found a solution that works, small change to the plugin, not sure if it is the right way to fix it, but I have confirmed it worked. make the following changes to the indes.js for the nhentai plugin: starting at line 100 add

if (mango.post(url, "").status_code == 404) {
        url = pageURL + pageCount + "." +  (imageType == "png" ? "gif" : "gif");
        imageType = "gif";
    }

    var filename = pad(pageCount, digits) + '.' + imageType;
    imageType = "jpg";

which should go till line 106, right before the

return JSON.stringify({
        url: url,
        filename: filename
    });

which starts 107. This causes it to try gif instead of jpg or png when they 404, and it makes it actually right .gif for the filename for that file. then returns imageType to jpg before looping again, else it causes it to try gif for everything after that, even if it should be jpg or png. I have confirmed that this does work, but like I said, IDK if it causes any other issues.

zeldatp151 commented 2 years ago

Here is the full code of index.js with my changes

var pageURL;
var pageCount = 0;
var ended = false;
var digits = 0;
var imageType;
var totalPages;

const NHENTAI_BASE_URL = 'https://nhentai.net/g/';
const NHENTAI_IMAGE_URL = "https://i.nhentai.net/galleries/";

function listChapters(url) {
    var html = mango.get(url).body;

    var urlMatch = /\/g\/([a-zA-Z0-9]+)\//.exec(url);
    var chapterID = urlMatch[1];

    var chapterTitleNode = mango.css(html, '.title')[0];

    if (!chapterTitleNode)
        mango.raise("Failed to get gallery title");

    var chapterTitle = mango.text(chapterTitleNode);

    var chapters = [{
        id: chapterID,
        title: chapterTitle
    }];
    return JSON.stringify({
        chapters: chapters,
        title: 'nhentai'
    });
}

function selectChapter(id) {
    var url = NHENTAI_BASE_URL + id + '/';
    var html = mango.get(url).body;

    var chapterTitleNode = mango.css(html, '.title')[0];

    if (!chapterTitleNode) {
        mango.raise("Failed to get gallery title");
    }

    var chapterTitle = mango.text(chapterTitleNode);

    var pages = 0;
    try {
        var pagesSpan = mango.css(html, '#tags div:nth-last-child(2) span.name')[0];
        if (!pagesSpan) {
            throw new Error();
        }
        var lengthText = mango.text(pagesSpan);
        pages = parseInt(lengthText);
    } catch (e) {
        mango.raise("Failed to get page count");
    }

    var firstPageATag = mango.css(html, 'div#thumbnail-container img.lazyload')[0];
    if (!firstPageATag) {
        mango.raise("Failed to get URL to the first page");
    }

    uglyPageURL = mango.attribute(firstPageATag, 'data-src');
    var pageURLMatch = /\/galleries\/([0-9]+)\/.*?\.(\w*)/.exec(uglyPageURL);

    pageURL = NHENTAI_IMAGE_URL + pageURLMatch[1] + "/";
    imageType = pageURLMatch[2];

    ended = false;
    pageCount = 0;
    totalPages = pages;
    digits = Math.floor(Math.log10(pages)) + 1;

    return JSON.stringify({
        title: chapterTitle,
        pages: pages
    });
}

function nextPage() {
    if (ended) {
        return JSON.stringify({});
    }

    pageCount += 1;

    if (pageCount === totalPages) {
        ended = true;
    }

    var url = pageURL + pageCount + "." + imageType;

    // Not all galleries have uniform file types
    // Post should return 405 if path exists, 404 otherwise.
    if (mango.post(url, "").status_code == 404) {
        url = pageURL + pageCount + "." +  (imageType == "png" ? "jpg" : "png");
    }

    if (mango.post(url, "").status_code == 404) {
        url = pageURL + pageCount + "." +  (imageType == "png" ? "gif" : "gif");
        imageType = "gif";
    }

    var filename = pad(pageCount, digits) + '.' + imageType;
    imageType = "jpg";
    return JSON.stringify({
        url: url,
        filename: filename
    });
}

// https://stackoverflow.com/a/10073788
function pad(n, width, z) {
    z = z || '0';
    n = n + '';
    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
hkalexling commented 2 years ago

Good catch! I think when the plugin was first developed Mango didn't have GIF support. I will update the plugin code.

zeldatp151 commented 2 years ago

Awesome, IDK if my code is the best or not, my knowledge of JS is basic at best, but it has been working for me.