diff --git a/bin/fimfic2epub.js b/bin/fimfic2epub.js index 82d6be4..0242e0b 100755 --- a/bin/fimfic2epub.js +++ b/bin/fimfic2epub.js @@ -16,4 +16,6 @@ const STORY_ID = process.argv[2] const ffc = new FimFic2Epub(STORY_ID) -ffc.download() +ffc.download().then(() => { + ffc.saveStory() +}) diff --git a/src/FimFic2Epub.js b/src/FimFic2Epub.js index c1083d4..42b1e67 100644 --- a/src/FimFic2Epub.js +++ b/src/FimFic2Epub.js @@ -41,135 +41,142 @@ export default class FimFic2Epub { } download () { - if (this.isDownloading) { - alert("Calm down, I'm working on it (it's processing)") - return - } - if (this.cachedBlob) { - this.saveStory() - return - } - this.build() + return new Promise((resolve, reject) => { + if (this.isDownloading) { + reject() + return + } + if (this.cachedBlob) { + resolve() + return + } + this.build().then(resolve).catch(reject) + }) } build () { - this.isDownloading = true + return new Promise((resolve, reject) => { + this.isDownloading = true - this.zip = new JSZip() - this.zip.file('mimetype', 'application/epub+zip') - this.zip.file('META-INF/container.xml', containerXml) + this.zip = new JSZip() + this.zip.file('mimetype', 'application/epub+zip') + this.zip.file('META-INF/container.xml', containerXml) - console.log('Fetching story metadata...') + console.log('Fetching story metadata...') - fetchRemote('https://www.fimfiction.net/api/story.php?story=' + this.storyId, (raw, type) => { - let data - try { - data = JSON.parse(raw) - } catch (e) { - console.log('Unable to fetch story json') - return - } - if (data.error) { - console.error(data.error) - return - } - this.storyInfo = data.story - this.storyInfo.chapters = this.storyInfo.chapters || [] - this.storyInfo.uuid = 'urn:fimfiction:' + this.storyInfo.id + fetchRemote('https://www.fimfiction.net/api/story.php?story=' + this.storyId, (raw, type) => { + let data + try { + data = JSON.parse(raw) + } catch (e) { + console.log('Unable to fetch story json') + return + } + if (data.error) { + console.error(data.error) + return + } + this.storyInfo = data.story + this.storyInfo.chapters = this.storyInfo.chapters || [] + this.storyInfo.uuid = 'urn:fimfiction:' + this.storyInfo.id - this.zip.file('Styles/style.css', styleCss) - this.zip.file('Styles/coverstyle.css', coverstyleCss) - if (this.includeTitlePage) { - this.zip.file('Styles/titlestyle.css', titlestyleCss) - } + this.zip.file('Styles/style.css', styleCss) + this.zip.file('Styles/coverstyle.css', coverstyleCss) + if (this.includeTitlePage) { + this.zip.file('Styles/titlestyle.css', titlestyleCss) + } - this.zip.file('toc.ncx', template.createNcx(this)) - this.zip.file('Text/nav.xhtml', template.createNav(this)) + this.zip.file('toc.ncx', template.createNcx(this)) + this.zip.file('Text/nav.xhtml', template.createNav(this)) - this.fetchTitlePage() - }) - } - - fetchTitlePage () { - fetchRemote(this.storyInfo.url, (raw, type) => { - this.extractTitlePageInfo(raw, () => this.checkCoverImage()) - }) - } - - extractTitlePageInfo (html, cb) { - let descPos = html.indexOf('