Skip to content

Instantly share code, notes, and snippets.

@PatheticMustan
Created May 10, 2021 16:27
Show Gist options
  • Save PatheticMustan/dd45087a557c481b7c2f0079923dc66a to your computer and use it in GitHub Desktop.
Save PatheticMustan/dd45087a557c481b7c2f0079923dc66a to your computer and use it in GitHub Desktop.
async function searchManga(title) {
const result = await (await fetch(`https://api.mangadex.org/manga?title=${encodeURI(title)}`)).json();
return result.results.map(v => v.data)
}
async function getChapters(mangaID) {
const result = await (await fetch(`https://api.mangadex.org/chapter/?manga=${mangaID}&limit=50`)).json();
return result.results;
}
async function getChapterInfo(chapterID) {
const result = await (await fetch(`https://api.mangadex.org/chapter/${chapterID}`)).json();
return result;
}
async function atHomeLink(chapterID) {
const result = await (await fetch(`https://api.mangadex.org/at-home/server/${chapterID}`)).json();
return result.baseUrl;
}
function craftImageLink(homeLink, chapter, isDataSaver=true) {
// {server-specific base url}/{temporary access token}/{quality mode}/{chapter hash}/{filename}
const mode = isDataSaver? "data-saver" : "data";
const chapterHash = chapter.data.attributes.hash;
const images = chapter.data.attributes[isDataSaver? "dataSaver" : "data"]
.map(img => `${homeLink}/${isDataSaver}/${chapterHash}/${img}`);
return images;
}
function listChapters(chapters) {
return chapters.map(chapter => chapter.data.attributes).map(v => `v${v.volume}c${v.chapter}: ${v.title}`)
}
let MANGA_NAME = "kubera";
let CHAPTER_INDEX = 0;
// get first search result
let manga = (await searchManga(MANGA_NAME))[0];
// get chapter IDs
let mangaChapters = await getChapters(manga.id);
// get home link with first chapter ID
let homeLink = await atHomeLink(mangaChapters[CHAPTER_INDEX].data.id);
// get image links
console.log(mangaChapters[CHAPTER_INDEX])
let firstChapter = (await getChapterInfo(mangaChapters[CHAPTER_INDEX].data.id));
let imageLinks = craftImageLink(homeLink, firstChapter, true);
//console.log(listChapters(mangaChapters));
console.log(listChapters(mangaChapters)[CHAPTER_INDEX]);
console.log(imageLinks.join("\n"));
imageLinks.forEach(s => {
let m = document.createElement("img");
m.src = s;
document.body.append(m);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment