|
1 | | -var chapter; |
2 | | -var currentPage; |
3 | | - |
4 | | -function listChapters(query) { |
5 | | - var json = {}; |
6 | | - var URL = 'https://mangadex.org/api/manga/' + query; |
7 | | - try { |
8 | | - json = JSON.parse(mango.get(URL).body); |
9 | | - } catch (e) { |
10 | | - mango.raise('Failed to get JSON from ' + URL); |
| 1 | +function getCoverURL(mangaId, coverId) { |
| 2 | + const res = mango.get('https://api.mangadex.org/cover/' + coverId); |
| 3 | + if (res.status_code !== 200) |
| 4 | + mango.raise('Failed to cover ID. Status ' + res.status_code); |
| 5 | + const filename = JSON.parse(res.body).data.attributes.fileName; |
| 6 | + return 'https://uploads.mangadex.org/covers/' + mangaId + '/' + filename; |
| 7 | +} |
| 8 | + |
| 9 | +function getManga(mangaId) { |
| 10 | + const res = mango.get('https://api.mangadex.org/manga/' + mangaId); |
| 11 | + if (res.status_code !== 200) |
| 12 | + mango.raise('Failed to get manga. Status ' + res.status_code); |
| 13 | + return JSON.parse(res.body).data; |
| 14 | +} |
| 15 | + |
| 16 | +function formatChapter(json, mangaTitle) { |
| 17 | + var title = json.attributes.title || ""; |
| 18 | + if (json.attributes.volume) |
| 19 | + title += ' Vol. ' + json.attributes.volume; |
| 20 | + if (json.attributes.chapter) |
| 21 | + title += ' Ch. ' + json.attributes.chapter; |
| 22 | + var groupId; |
| 23 | + for (i = 0; i < json.relationships.length; i++) { |
| 24 | + const obj = json.relationships[i]; |
| 25 | + if (obj.type === 'scanlation_group') { |
| 26 | + groupId = obj.id; |
| 27 | + break; |
| 28 | + } |
11 | 29 | } |
| 30 | + const obj = { |
| 31 | + id: json.id, |
| 32 | + title: title, |
| 33 | + manga_title: mangaTitle, |
| 34 | + pages: json.attributes.pages, |
| 35 | + volume: json.attributes.volume, |
| 36 | + chapter: json.attributes.chapter, |
| 37 | + language: json.attributes.translatedLanguage, |
| 38 | + group_id: groupId || null, |
| 39 | + published_at: Date.parse(json.attributes.publishAt), |
| 40 | + }; |
| 41 | + return obj; |
| 42 | +} |
12 | 43 |
|
13 | | - if (json.status !== 'OK') |
14 | | - mango.raise('JSON status: ' + json.status); |
| 44 | +function formatTimestamp(timestamp) { |
| 45 | + return new Date(timestamp).toISOString().replace('.000Z', ''); |
| 46 | +} |
15 | 47 |
|
16 | | - var chapters = []; |
17 | | - Object.keys(json.chapter).forEach(function(id) { |
18 | | - var obj = json.chapter[id]; |
| 48 | +function searchManga(query) { |
| 49 | + const res = mango.get('https://api.mangadex.org/manga?title=' + encodeURIComponent(query)); |
| 50 | + if (res.status_code !== 200) |
| 51 | + mango.raise('Failed to search for manga. Status ' + res.status_code); |
| 52 | + const manga = JSON.parse(res.body).data; |
| 53 | + if (!manga) |
| 54 | + mango.raise('Failed to search for manga.'); |
19 | 55 |
|
20 | | - var groups = []; |
21 | | - ['group_name', 'group_name_2', 'group_name_3'].forEach(function(key) { |
22 | | - if (obj[key]) { |
23 | | - groups.push(obj[key]); |
| 56 | + return JSON.stringify(manga.map(function(m) { |
| 57 | + const titleAttr = m.attributes.title; |
| 58 | + const ch = { |
| 59 | + id: m.id, |
| 60 | + title: titleAttr.en || titleAttr['ja-ro'] || titleAttr[Object.keys(titleAttr)[0]] |
| 61 | + }; |
| 62 | + for (i = 0; i < m.relationships.length; i++) { |
| 63 | + const obj = m.relationships[i]; |
| 64 | + if (obj.type === 'cover_art') { |
| 65 | + ch.cover_url = getCoverURL(m.id, obj.id); |
| 66 | + break; |
24 | 67 | } |
| 68 | + } |
| 69 | + return ch; |
| 70 | + })); |
| 71 | +} |
| 72 | + |
| 73 | +function listChapters(id) { |
| 74 | + const manga = getManga(id); |
| 75 | + const titleAttr = manga.attributes.title; |
| 76 | + const title = titleAttr.en || titleAttr['ja-ro'] || titleAttr[Object.keys(titleAttr)[0]]; |
| 77 | + |
| 78 | + var url = 'https://api.mangadex.org/manga/' + id + '/feed?'; |
| 79 | + |
| 80 | + const langStr = mango.settings('language'); |
| 81 | + if (langStr) { |
| 82 | + const langAry = langStr.split(',').forEach(function(lang) { |
| 83 | + url += 'translatedLanguage[]=' + lang.trim() + '&'; |
25 | 84 | }); |
26 | | - groups = groups.join(', '); |
27 | | - var time = new Date(obj.timestamp * 1000); |
28 | | - |
29 | | - var slimObj = {}; |
30 | | - slimObj['id'] = id; |
31 | | - slimObj['volume'] = obj['volume']; |
32 | | - slimObj['chapter'] = obj['chapter']; |
33 | | - slimObj['title'] = obj['title']; |
34 | | - slimObj['lang'] = obj['lang_code']; |
35 | | - slimObj['groups'] = groups; |
36 | | - slimObj['time'] = time; |
37 | | - |
38 | | - chapters.push(slimObj); |
39 | | - }); |
| 85 | + } |
40 | 86 |
|
41 | | - return JSON.stringify({ |
42 | | - title: json.manga.title, |
43 | | - chapters: chapters |
44 | | - }); |
| 87 | + const limit = mango.settings('listChapterLimit'); |
| 88 | + if (limit) { |
| 89 | + url += 'limit=' + limit.trim() + '&'; |
| 90 | + } |
| 91 | + |
| 92 | + const res = mango.get(url); |
| 93 | + if (res.status_code !== 200) |
| 94 | + mango.raise('Failed to list chapters. Status ' + res.status_code); |
| 95 | + |
| 96 | + const chapters = JSON.parse(res.body).data; |
| 97 | + |
| 98 | + return JSON.stringify(chapters.map(function(ch) { |
| 99 | + return formatChapter(ch, title); |
| 100 | + })); |
| 101 | +} |
| 102 | + |
| 103 | +function newChapters(mangaId, after) { |
| 104 | + var url = 'https://api.mangadex.org/manga/' + mangaId + '/feed?publishAtSince=' + formatTimestamp(after) + '&'; |
| 105 | + |
| 106 | + const langStr = mango.settings('language'); |
| 107 | + if (langStr) { |
| 108 | + const langAry = langStr.split(',').forEach(function(lang) { |
| 109 | + url += 'translatedLanguage[]=' + lang.trim() + '&'; |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + const limit = mango.settings('listChapterLimit'); |
| 114 | + if (limit) { |
| 115 | + url += 'limit=' + limit.trim() + '&'; |
| 116 | + } |
| 117 | + |
| 118 | + const res = mango.get(url); |
| 119 | + if (res.status_code !== 200) |
| 120 | + mango.raise('Failed to list new chapters. Status ' + res.status_code); |
| 121 | + |
| 122 | + const chapters = JSON.parse(res.body).data; |
| 123 | + |
| 124 | + const manga = getManga(mangaId); |
| 125 | + const titleAttr = manga.attributes.title; |
| 126 | + const title = titleAttr.en || titleAttr['ja-ro'] || titleAttr[Object.keys(titleAttr)[0]]; |
| 127 | + |
| 128 | + return JSON.stringify(chapters.map(function(ch) { |
| 129 | + return formatChapter(ch, title); |
| 130 | + })); |
45 | 131 | } |
46 | 132 |
|
47 | 133 | function selectChapter(id) { |
48 | | - var json = {}; |
49 | | - var URL = 'https://mangadex.org/api/chapter/' + id; |
50 | | - try { |
51 | | - json = JSON.parse(mango.get(URL).body); |
52 | | - } catch (e) { |
53 | | - mango.raise('Failed to get JSON from ' + URL); |
| 134 | + const res = mango.get('https://api.mangadex.org/chapter/' + id); |
| 135 | + if (res.status_code !== 200) |
| 136 | + mango.raise('Failed to get chapter. Status ' + res.status_code); |
| 137 | + |
| 138 | + const chapter = JSON.parse(res.body).data; |
| 139 | + var mangaId; |
| 140 | + for (i = 0; i < chapter.relationships.length; i++) { |
| 141 | + const obj = chapter.relationships[i]; |
| 142 | + if (obj.type === 'manga') { |
| 143 | + mangaId = obj.id; |
| 144 | + break; |
| 145 | + } |
54 | 146 | } |
55 | 147 |
|
56 | | - if (json.status !== 'OK') |
57 | | - mango.raise('JSON status: ' + json.status); |
| 148 | + if (!mangaId) |
| 149 | + mango.raise('Failed to get Manga ID from chapter'); |
58 | 150 |
|
59 | | - chapter = json; |
60 | | - currentPage = 0; |
| 151 | + const manga = getManga(mangaId); |
| 152 | + const titleAttr = manga.attributes.title; |
| 153 | + const title = titleAttr.en || titleAttr['ja-ro'] || titleAttr[Object.keys(titleAttr)[0]]; |
61 | 154 |
|
62 | | - var info = { |
63 | | - title: json.title.trim() || ('Ch.' + json.chapter), |
64 | | - pages: json.page_array.length |
65 | | - }; |
66 | | - return JSON.stringify(info); |
| 155 | + const atHome = mango.get('https://api.mangadex.org/at-home/server/' + id); |
| 156 | + if (atHome.status_code !== 200) |
| 157 | + mango.raise('Failed to get at-home server. Status ' + atHome.status_code); |
| 158 | + |
| 159 | + const atHomeData = JSON.parse(atHome.body); |
| 160 | + |
| 161 | + mango.storage('atHomeData', JSON.stringify(atHomeData)); |
| 162 | + mango.storage('page', '0'); |
| 163 | + |
| 164 | + return JSON.stringify(formatChapter(chapter, title)); |
67 | 165 | } |
68 | 166 |
|
69 | 167 | function nextPage() { |
70 | | - if (currentPage >= chapter.page_array.length) |
71 | | - return JSON.stringify({}); |
| 168 | + const page = parseInt(mango.storage('page')); |
| 169 | + const atHome = JSON.parse(mango.storage('atHomeData')); |
| 170 | + const filename = atHome.chapter.data[page]; |
| 171 | + if (!filename) return JSON.stringify({}); |
72 | 172 |
|
73 | | - var fn = chapter.page_array[currentPage]; |
74 | | - var info = { |
75 | | - filename: fn, |
76 | | - url: chapter.server + chapter.hash + '/' + fn |
77 | | - }; |
| 173 | + //Get the number of digits of pages |
| 174 | + const len = atHome.chapter.data.length.toString().length; |
| 175 | + //Pad the page number with zeroes depending of the number of pages |
| 176 | + const pageNum = Array(Math.max(len - String(page + 1).length + 1, 0)).join(0) + (page + 1); |
| 177 | + |
| 178 | + const finalFilename = pageNum + '.' + filename.split('.')[filename.split('.').length -1]; |
| 179 | + mango.storage('page', (page + 1).toString()); |
78 | 180 |
|
79 | | - currentPage += 1; |
80 | | - return JSON.stringify(info); |
| 181 | + return JSON.stringify({ |
| 182 | + url: atHome.baseUrl + '/data/' + atHome.chapter.hash + '/' + filename, |
| 183 | + filename: finalFilename, |
| 184 | + }); |
81 | 185 | } |
0 commit comments