Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MangaDex plugin to target the v2 plugin API #21

Merged
merged 5 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ To install a plugin, simply download the folder containing the desired plugin (e

## Contributing

Pull requests are always welcome! Please check out the [development guidelne](https://github.com/hkalexling/mango-plugins/wiki/Development-Guideline).
Pull requests are always welcome! Please check out the [development guidelne](https://github.com/hkalexling/mango-plugins/wiki/Development-Guideline-v2).
2 changes: 2 additions & 0 deletions plugins/mangadex/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
storage.json
subscriptions.json
16 changes: 15 additions & 1 deletion plugins/mangadex/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# MangaDex Plugin

This is the Mango plugin for [MangaDex](https://mangadex.org/). It's developed to demonstrate the usage of the plugin system, and you should use the built-in MangaDex downloader in Mango instead.
This is the official Mango plugin for [MangaDex](https://mangadex.org/).

If you are looking for the v1 version of this plugin (deprecated), check out commit f045ecd5752823f100baf4f298d89f2f99237cd4.

### Configuration

Under the `settings` field in `info.json`, you can customize the following values:

#### `language`

You can list one or more language codes in this field (separated by commas) for the plugin to search MangaDex in. Only chapters in one of the specified languages will be shown and used. Check [here](https://api.mangadex.org/docs.html#section/Language-Codes-and-Localization) for the list of available language codes. When it's not set, the plugin will show you chapters in all languages, and you probably don't want that.

#### `listChapterLimit`

This field controls the maximum number of chapters the plugin lists when browsing a manga or when checking for updates. It can be anything between `1` and `500`. Unset it to use the default value `100` from MangaDex.

Maintained by [@hkalexling](https://github.com/hkalexling).
217 changes: 155 additions & 62 deletions plugins/mangadex/index.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,174 @@
var chapter;
var currentPage;

function listChapters(query) {
var json = {};
var URL = 'https://mangadex.org/api/manga/' + query;
try {
json = JSON.parse(mango.get(URL).body);
} catch (e) {
mango.raise('Failed to get JSON from ' + URL);
function getCoverURL(mangaId, coverId) {
const res = mango.get('https://api.mangadex.org/cover/' + coverId);
if (res.status_code !== 200)
mango.raise('Failed to cover ID. Status ' + res.status_code);
const filename = JSON.parse(res.body).data.attributes.fileName;
return 'https://uploads.mangadex.org/covers/' + mangaId + '/' + filename;
}

function getManga(mangaId) {
const res = mango.get('https://api.mangadex.org/manga/' + mangaId);
if (res.status_code !== 200)
mango.raise('Failed to get manga. Status ' + res.status_code);
return JSON.parse(res.body).data;
}

function formatChapter(json, mangaTitle) {
var title = json.attributes.title || "";
if (json.attributes.volume)
title += ' Vol. ' + json.attributes.volume;
if (json.attributes.chapter)
title += ' Ch. ' + json.attributes.chapter;
var groupId;
for (i = 0; i < json.relationships.length; i++) {
const obj = json.relationships[i];
if (obj.type === 'scanlation_group') {
groupId = obj.id;
break;
}
}
const obj = {
id: json.id,
title: title,
manga_title: mangaTitle,
pages: json.attributes.pages,
volume: json.attributes.volume,
chapter: json.attributes.chapter,
language: json.attributes.translatedLanguage,
group_id: groupId || null,
published_at: Date.parse(json.attributes.publishAt),
};
return obj;
}

if (json.status !== 'OK')
mango.raise('JSON status: ' + json.status);
function formatTimestamp(timestamp) {
return new Date(timestamp).toISOString().replace('.000Z', '');
}

var chapters = [];
Object.keys(json.chapter).forEach(function(id) {
var obj = json.chapter[id];
function searchManga(query) {
const res = mango.get('https://api.mangadex.org/manga?title=' + encodeURIComponent(query));
if (res.status_code !== 200)
mango.raise('Failed to search for manga. Status ' + res.status_code);
const manga = JSON.parse(res.body).data;
if (!manga)
mango.raise('Failed to search for manga.');

var groups = [];
['group_name', 'group_name_2', 'group_name_3'].forEach(function(key) {
if (obj[key]) {
groups.push(obj[key]);
return JSON.stringify(manga.map(function(m) {
const ch = {
id: m.id,
title: m.attributes.title.en,
};
for (i = 0; i < m.relationships.length; i++) {
const obj = m.relationships[i];
if (obj.type === 'cover_art') {
ch.cover_url = getCoverURL(m.id, obj.id);
break;
}
}
return ch;
}));
}

function listChapters(id) {
const manga = getManga(id);
const title = manga.attributes.title.en;

var url = 'https://api.mangadex.org/manga/' + id + '/feed?';

const langStr = mango.settings('language');
if (langStr) {
const langAry = langStr.split(',').forEach(function(lang) {
url += 'translatedLanguage[]=' + lang.trim() + '&';
});
groups = groups.join(', ');
var time = new Date(obj.timestamp * 1000);

var slimObj = {};
slimObj['id'] = id;
slimObj['volume'] = obj['volume'];
slimObj['chapter'] = obj['chapter'];
slimObj['title'] = obj['title'];
slimObj['lang'] = obj['lang_code'];
slimObj['groups'] = groups;
slimObj['time'] = time;

chapters.push(slimObj);
});
}

return JSON.stringify({
title: json.manga.title,
chapters: chapters
});
const limit = mango.settings('listChapterLimit');
if (limit) {
url += 'limit=' + limit.trim() + '&';
}

const res = mango.get(url);
if (res.status_code !== 200)
mango.raise('Failed to list chapters. Status ' + res.status_code);

const chapters = JSON.parse(res.body).data;

return JSON.stringify(chapters.map(function(ch) {
return formatChapter(ch, title);
}));
}

function newChapters(mangaId, after) {
var url = 'https://api.mangadex.org/manga/' + mangaId + '/feed?publishAtSince=' + formatTimestamp(after) + '&';

const langStr = mango.settings('language');
if (langStr) {
const langAry = langStr.split(',').forEach(function(lang) {
url += 'translatedLanguage[]=' + lang.trim() + '&';
});
}

const limit = mango.settings('listChapterLimit');
if (limit) {
url += 'limit=' + limit.trim() + '&';
}

const res = mango.get(url);
if (res.status_code !== 200)
mango.raise('Failed to list new chapters. Status ' + res.status_code);

const chapters = JSON.parse(res.body).data;

const manga = getManga(mangaId);
const title = manga.attributes.title.en;

return JSON.stringify(chapters.map(function(ch) {
return formatChapter(ch, title);
}));
}

function selectChapter(id) {
var json = {};
var URL = 'https://mangadex.org/api/chapter/' + id;
try {
json = JSON.parse(mango.get(URL).body);
} catch (e) {
mango.raise('Failed to get JSON from ' + URL);
const res = mango.get('https://api.mangadex.org/chapter/' + id);
if (res.status_code !== 200)
mango.raise('Failed to get chapter. Status ' + res.status_code);

const chapter = JSON.parse(res.body).data;
var mangaId;
for (i = 0; i < chapter.relationships.length; i++) {
const obj = chapter.relationships[i];
if (obj.type === 'manga') {
mangaId = obj.id;
break;
}
}

if (json.status !== 'OK')
mango.raise('JSON status: ' + json.status);
if (!mangaId)
mango.raise('Failed to get Manga ID from chapter');

chapter = json;
currentPage = 0;
const manga = getManga(mangaId);
const title = manga.attributes.title.en;

var info = {
title: json.title.trim() || ('Ch.' + json.chapter),
pages: json.page_array.length
};
return JSON.stringify(info);
const atHome = mango.get('https://api.mangadex.org/at-home/server/' + id);
if (atHome.status_code !== 200)
mango.raise('Failed to get at-home server. Status ' + atHome.status_code);

const atHomeData = JSON.parse(atHome.body);

mango.storage('atHomeData', JSON.stringify(atHomeData));
mango.storage('page', '0');

return JSON.stringify(formatChapter(chapter, title));
}

function nextPage() {
if (currentPage >= chapter.page_array.length)
return JSON.stringify({});
const page = parseInt(mango.storage('page'));
const atHome = JSON.parse(mango.storage('atHomeData'));
const filename = atHome.chapter.data[page]
mango.storage('page', (page + 1).toString());
if (!filename) return JSON.stringify({});

var fn = chapter.page_array[currentPage];
var info = {
filename: fn,
url: chapter.server + chapter.hash + '/' + fn
};

currentPage += 1;
return JSON.stringify(info);
return JSON.stringify({
url: atHome.baseUrl + '/data/' + atHome.chapter.hash + '/' + filename,
filename: filename,
});
}
11 changes: 8 additions & 3 deletions plugins/mangadex/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"id": "mangadex",
"title": "MangaDex",
"author": "Alex Ling - hkalexling@gmail.com",
"version": "v1.0",
"placeholder": "MangaDex manga ID (e.g., 7139)",
"wait_seconds": 5
"version": "v2.0",
"placeholder": "Search MangaDex",
"wait_seconds": 5,
"api_version": 2,
"settings": {
"language": "en",
"listChapterLimit": "200"
}
}
5 changes: 0 additions & 5 deletions plugins/mangadexV5/README.md

This file was deleted.

101 changes: 0 additions & 101 deletions plugins/mangadexV5/index.js

This file was deleted.

8 changes: 0 additions & 8 deletions plugins/mangadexV5/info.json

This file was deleted.