|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +parserFactory.register("mottruyen.com.vn", () => new MottruyenParser()); |
| 4 | +parserFactory.register("mottruyen.vn", () => new MottruyenParser()); |
| 5 | + |
| 6 | +class MottruyenParser extends Parser{ |
| 7 | + constructor() { |
| 8 | + super(); |
| 9 | + } |
| 10 | + |
| 11 | + async getChapterUrls(dom) { |
| 12 | + let leaves = dom.baseURI.split("/").filter(a => a != ""); |
| 13 | + let id = leaves[leaves.length - 1]; |
| 14 | + let bookinfo = (await HttpClient.fetchJson("https://api.mottruyen.vn/api/v1/story/"+id)).json; |
| 15 | + let chapter_count = bookinfo.countChapter; |
| 16 | + let slug = bookinfo.slug; |
| 17 | + let chapters = (await HttpClient.fetchJson("https://api.mottruyen.vn/api/v1/story/"+id+"/chapter?size="+chapter_count+"&page=0&sort=asc")).json; |
| 18 | + return chapters.data.map(a => ({ |
| 19 | + sourceUrl: "https://mottruyen.com.vn/"+slug+"/"+id+"/chuong/"+a.chapter, |
| 20 | + title: a.chapterTitle?"Chương "+a.chapter + " :"+ a.chapterTitle:"Chương "+a.chapter, |
| 21 | + isIncludeable: (a.price == 0) |
| 22 | + })); |
| 23 | + } |
| 24 | + |
| 25 | + async loadEpubMetaInfo(dom){ |
| 26 | + let leaves = dom.baseURI.split("/").filter(a => a != ""); |
| 27 | + let id = leaves[leaves.length - 1]; |
| 28 | + let bookinfo = (await HttpClient.fetchJson("https://api.mottruyen.vn/api/v1/story/"+id)).json; |
| 29 | + this.title = bookinfo.name; |
| 30 | + this.author = bookinfo.author.name; |
| 31 | + this.description = bookinfo.introduce?.trim(); |
| 32 | + this.password = bookinfo.password; |
| 33 | + this.img = "https://api.mottruyen.vn/api/v1/storage"+bookinfo.image; |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + extractTitleImpl() { |
| 38 | + return this.title; |
| 39 | + } |
| 40 | + |
| 41 | + extractAuthor() { |
| 42 | + return this.author; |
| 43 | + } |
| 44 | + |
| 45 | + extractDescription() { |
| 46 | + return this.description; |
| 47 | + } |
| 48 | + |
| 49 | + findCoverImageUrl() { |
| 50 | + return this.img; |
| 51 | + } |
| 52 | + |
| 53 | + findContent(dom) { |
| 54 | + return Parser.findConstrutedContent(dom); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + async fetchChapter(url) { |
| 59 | + let restUrl = this.toRestUrl(url); |
| 60 | + let options = { |
| 61 | + parser: this |
| 62 | + }; |
| 63 | + let json = (await HttpClient.fetchJson(restUrl, options)).json; |
| 64 | + return this.buildChapter(json, url); |
| 65 | + } |
| 66 | + |
| 67 | + toRestUrl(url) { |
| 68 | + let leaves = url.split("/").filter(a => a != ""); |
| 69 | + let id = leaves[leaves.length - 3]; |
| 70 | + let chapternumber = leaves[leaves.length - 1]; |
| 71 | + return "https://api.mottruyen.vn/api/v1/story/"+id+"/chapter/"+chapternumber+"?password="+this.password; |
| 72 | + } |
| 73 | + |
| 74 | + isCustomError(response){ |
| 75 | + if (response?.json?.lock) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + if (response?.json?.statusCode != 200) { |
| 79 | + return true; |
| 80 | + } |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + setCustomErrorResponse(url, wrapOptions, checkedresponse){ |
| 85 | + if (checkedresponse?.json?.lock) { |
| 86 | + //Is only viewable in the app |
| 87 | + let newresp = {}; |
| 88 | + newresp.url = url; |
| 89 | + newresp.wrapOptions = wrapOptions; |
| 90 | + newresp.response = {}; |
| 91 | + newresp.response.url = this.PostToUrl(checkedresponse.response.url, checkedresponse); |
| 92 | + newresp.response.status = 999; |
| 93 | + newresp.response.retryDelay = [1]; |
| 94 | + newresp.errorMessage = "This Chapter '"+newresp.response.url+"' could not be downloaded\nMessage: "+checkedresponse?.json.content; |
| 95 | + return newresp; |
| 96 | + } |
| 97 | + if (checkedresponse?.json?.statusCode != 200) { |
| 98 | + //to catch an error response |
| 99 | + //not tested |
| 100 | + let newresp = {}; |
| 101 | + newresp.url = url; |
| 102 | + newresp.wrapOptions = wrapOptions; |
| 103 | + newresp.response = {}; |
| 104 | + newresp.response.url = checkedresponse.response.url; |
| 105 | + newresp.response.status = checkedresponse?.json?.statusCode; |
| 106 | + return newresp; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + PostToUrl(url, checkedresponse){ |
| 111 | + let leaves = url.split("/").filter(a => a != ""); |
| 112 | + let id = leaves[leaves.length - 3]; |
| 113 | + let chapternumber = leaves[leaves.length - 1]; |
| 114 | + return "https://mottruyen.com.vn/"+checkedresponse?.json?.nameIndex+"/"+id+"/chuong/"+chapternumber; |
| 115 | + } |
| 116 | + |
| 117 | + buildChapter(json, url) { |
| 118 | + let newDoc = Parser.makeEmptyDocForContent(url); |
| 119 | + let title = newDoc.dom.createElement("h1"); |
| 120 | + title.textContent = json.chapterTitle?"Chương "+json.chapter + " :"+ json.chapterTitle:"Chương "+json.chapter; |
| 121 | + newDoc.content.appendChild(title); |
| 122 | + let content = new DOMParser().parseFromString(json.content, "text/html"); |
| 123 | + for(let n of [...content.body.childNodes]) { |
| 124 | + newDoc.content.appendChild(n); |
| 125 | + } |
| 126 | + return newDoc.dom; |
| 127 | + } |
| 128 | +} |
0 commit comments