diff --git a/src/FimFic2Epub.js b/src/FimFic2Epub.js
index c8f1434..ca25c91 100644
--- a/src/FimFic2Epub.js
+++ b/src/FimFic2Epub.js
@@ -11,6 +11,7 @@ import fileType from 'file-type'
import { styleCss, coverstyleCss, titlestyleCss } from './styles'
import { cleanMarkup } from './cleanMarkup'
+import fetch from './fetch'
import fetchRemote from './fetchRemote'
import * as template from './templates'
@@ -40,8 +41,8 @@ module.exports = class FimFic2Epub {
static fetchStoryInfo (storyId, raw = false) {
return new Promise((resolve, reject) => {
storyId = FimFic2Epub.getStoryId(storyId)
- let url = 'https://www.fimfiction.net/api/story.php?story=' + storyId
- fetchRemote(url, (content, type) => {
+ let url = '/api/story.php?story=' + storyId
+ fetch(url).then((content) => {
let data
try {
data = JSON.parse(content)
@@ -76,32 +77,61 @@ module.exports = class FimFic2Epub {
})
}
+ static parseChapterPage (html) {
+ let trimWhitespace = /^\s*(
)+|(
)+\s*$/ig
+
+ let authorNotesPos = html.indexOf('
Author\'s Note:')
+ authorNotes = html.substring(authorNotesPos + 22)
+ authorNotes = authorNotes.substring(0, authorNotes.indexOf('\t\n\t
'))
+ authorNotes = authorNotes.trim()
+ authorNotes = authorNotes.replace(trimWhitespace, '')
+ }
+
+ let chapterPos = html.indexOf('')
+ let chapter = html.substring(chapterPos + 29)
+
+ let pos = chapter.indexOf('\t
\t\t\n\t')
+
+ chapter = chapter.substring(0, pos).trim()
+
+ // remove leading and trailing
tags and whitespace
+ chapter = chapter.replace(trimWhitespace, '')
+ return {content: chapter, notes: authorNotes, notesFirst: authorNotesPos < chapterPos}
+ }
+
constructor (storyId) {
this.storyId = FimFic2Epub.getStoryId(storyId)
this.hasDownloaded = false
- this.downloadPromise = null
+ this.fetchPromise = null
this.storyInfo = null
- this.titlePage = null
+ this.chapters = []
this.chapterContent = []
this.remoteResources = new Map()
this.cachedFile = null
this.hasCoverImage = false
this.coverImageDimensions = {width: 0, height: 0}
- this.includeTitlePage = true
- this.categories = []
- this.tags = []
+ // this.includeTitlePage = true
+ // this.categories = []
+ // this.tags = []
this.zip = new JSZip()
}
- download () {
- if (this.downloadPromise) {
- return this.downloadPromise
+ fetch () {
+ if (this.fetchPromise) {
+ return this.fetchPromise
}
+ this.storyInfo = null
+ this.chapters.length = 0
+ this.remoteResources.clear()
+
console.log('Fetching story metadata...')
let p = FimFic2Epub.fetchStoryInfo(this.storyId)
@@ -112,250 +142,108 @@ module.exports = class FimFic2Epub {
this.filename = FimFic2Epub.getFilename(this.storyInfo)
})
.then(this.fetchTitlePage.bind(this))
+ .then(this.fetchChapters.bind(this))
.then(() => {
- this.downloadPromise = null
+ console.log('Fetch complete')
+ console.log(this)
+ this.fetchPromise = null
})
- this.downloadPromise = p
+ this.fetchPromise = p
return p
}
build () {
- return this.extractTitlePageInfo(this.titlePage)
- .then(this.checkCoverImage.bind(this))
+ this.chapterContent.length = 0
+ return this.checkCoverImage()
+ .then(this.processChapters.bind(this))
+ .then(this.fetchRemoteFiles.bind(this))
+ .then(this.processStory.bind(this))
+ .then(() => {
+ console.log('Build complete')
+ })
}
fetchTitlePage () {
- console.log('Fetching index page...')
- return new Promise((resolve, reject) => {
- fetchRemote(this.storyInfo.url, (raw, type) => {
- this.titlePage = raw
- resolve(raw)
- })
- })
+ console.log('Fetching title page...')
+ let url = this.storyInfo.url.replace('http://www.fimfiction.net', '')
+ return fetch(url).then(this.extractTitlePageInfo.bind(this))
}
extractTitlePageInfo (html) {
- return new Promise((resolve, reject) => {
- let descPos = html.indexOf('')
- let catsHtml = html.substring(startCatsPos, endCatsPos)
- html = html.substring(endCatsPos + 6)
+ let descPos = html.indexOf('')
+ let catsHtml = html.substring(startCatsPos, endCatsPos)
+ html = html.substring(endCatsPos + 6)
- let categories = []
- let matchCategory = /(.*?)<\/a>/g
- for (let c; (c = matchCategory.exec(catsHtml));) {
- categories.push({
- url: 'http://www.fimfiction.net' + c[1],
- className: c[2],
- name: entities.decode(c[3])
- })
- }
- this.categories = categories
-
- ma = html.match(/This story is a sequel to (.*?)<\/a>/)
- if (ma) {
- this.storyInfo.prequel = {
- url: 'http://www.fimfiction.net' + ma[1],
- title: entities.decode(ma[2])
- }
- html = html.substring(html.indexOf('
') + 6)
- }
- let endDescPos = html.indexOf('\n')
- let description = html.substring(0, endDescPos).trim()
-
- html = html.substring(endDescPos + 7)
- let extraPos = html.indexOf('