import JSZip from 'jszip'
import escapeStringRegexp from 'escape-string-regexp'
import zeroFill from 'zero-fill'
import { XmlEntities } from 'html-entities'
import sanitize from 'sanitize-filename'
import URL from 'url'
import isNode from 'detect-node'
import fileType from 'file-type'
import sizeOf from 'image-size'
import Emitter from 'es6-event-emitter'
import { styleCss, coverstyleCss, titlestyleCss } from './styles'
import { cleanMarkup } from './cleanMarkup'
import fetch from './fetch'
import fetchRemote from './fetchRemote'
import * as template from './templates'
import { containerXml } from './constants'
const entities = new XmlEntities()
module.exports = class FimFic2Epub extends Emitter {
static getStoryId (id) {
if (isNaN(id)) {
let url = URL.parse(id, false, true)
if (url.hostname === 'www.fimfiction.net' || url.hostname === 'fimfiction.net') {
let m = url.pathname.match(/^\/story\/(\d+)/)
if (m) {
id = m[1]
}
}
}
return id
}
static getFilename (storyInfo) {
return sanitize(storyInfo.title + ' by ' + storyInfo.author.name + '.epub')
}
static fetchStoryInfo (storyId, raw = false) {
return new Promise((resolve, reject) => {
storyId = FimFic2Epub.getStoryId(storyId)
let url = '/api/story.php?story=' + storyId
fetch(url).then((content) => {
let data
try {
data = JSON.parse(content)
} catch (e) {}
if (!data) {
reject('Unable to fetch story info')
return
}
if (data.error) {
reject(data.error)
return
}
let story = data.story
if (raw) {
resolve(story)
return
}
// this is so the metadata can be cached.
if (!story.chapters) story.chapters = []
delete story.likes
delete story.dislikes
delete story.views
delete story.total_views
delete story.comments
story.chapters.forEach((ch) => {
delete ch.views
})
// Add version number
story.FIMFIC2EPUB_VERSION = FIMFIC2EPUB_VERSION
resolve(story)
})
})
}
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) {
super()
this.storyId = FimFic2Epub.getStoryId(storyId)
this.fetchPromise = null
this.storyInfo = null
this.description = ''
this.chapters = []
this.remoteResources = new Map()
this.coverImage = null
this.coverFilename = ''
this.coverType = ''
this.coverImageDimensions = {width: 0, height: 0}
this.cachedFile = null
this.categories = []
this.tags = []
this.zip = null
}
setCoverImage (buffer) {
let info = fileType(isNode ? buffer : new Uint8Array(buffer))
if (!info || info.mime.indexOf('image/') !== 0) {
throw new Error('Invalid image')
}
this.coverImage = buffer
this.coverFilename = 'Images/cover.' + info.ext
this.coverType = info.mime
this.coverImageDimensions = sizeOf(new Buffer(buffer))
}
fetch () {
if (this.fetchPromise) {
return this.fetchPromise
}
this.storyInfo = null
this.description = ''
this.chapters.length = 0
this.remoteResources.clear()
this.progress(0, 0, 'Fetching metadata...')
let p =
FimFic2Epub.fetchStoryInfo(this.storyId).then((storyInfo) => {
this.storyInfo = storyInfo
this.storyInfo.uuid = 'urn:fimfiction:' + this.storyInfo.id
this.filename = FimFic2Epub.getFilename(this.storyInfo)
this.progress(0, 0.3)
})
.then(this.fetchTitlePage.bind(this))
.then(() => cleanMarkup(this.description)).then((html) => {
this.storyInfo.description = html
this.findRemoteResources('description', 'description', html)
})
.then(this.fetchCoverImage.bind(this))
.then(this.fetchChapters.bind(this))
// .then(this.processChapters.bind(this))
.then(this.fetchRemoteFiles.bind(this))
.then(() => {
this.fetchPromise = null
})
this.fetchPromise = p
return p
}
build () {
this.cachedFile = null
this.zip = null
this.remoteResources.forEach((r, url) => {
let dest = '../' + r.dest
if (r.dest && r.originalUrl && r.where) {
let ourl = new RegExp(escapeStringRegexp(r.originalUrl), 'g')
for (var i = 0; i < r.where.length; i++) {
let w = r.where[i]
if (typeof w === 'number') {
this.chapters[w] = this.chapters[w].replace(ourl, dest)
} else if (w === 'description') {
this.storyInfo.description = this.storyInfo.description.replace(ourl, dest)
} else if (w === 'tags') {
this.tags.byImage[r.originalUrl].image = dest
}
}
}
})
this.zip = new JSZip()
this.zip.file('mimetype', 'application/epub+zip')
this.zip.file('META-INF/container.xml', containerXml)
this.zip.file('OEBPS/content.opf', template.createOpf(this))
if (this.coverImage) {
this.zip.file('OEBPS/' + this.coverFilename, this.coverImage)
}
this.zip.file('OEBPS/Text/cover.xhtml', template.createCoverPage(this))
this.zip.file('OEBPS/Styles/coverstyle.css', coverstyleCss)
this.zip.file('OEBPS/Text/title.xhtml', template.createTitlePage(this))
this.zip.file('OEBPS/Styles/titlestyle.css', titlestyleCss)
this.zip.file('OEBPS/Text/nav.xhtml', template.createNav(this))
this.zip.file('OEBPS/toc.ncx', template.createNcx(this))
for (let i = 0; i < this.chapters.length; i++) {
let filename = 'OEBPS/Text/chapter_' + zeroFill(3, i + 1) + '.xhtml'
let html = this.chapters[i]
this.zip.file(filename, html)
}
this.zip.file('OEBPS/Styles/style.css', styleCss)
this.remoteResources.forEach((r) => {
this.zip.file('OEBPS/' + r.dest, r.data)
})
this.progress(6, 0, 'Complete!')
}
// for node, resolve a Buffer, in browser resolve a Blob
getFile () {
if (!this.zip) {
return Promise.reject('Not downloaded.')
}
if (this.cachedFile) {
return Promise.resolve(this.cachedFile)
}
return this.zip
.generateAsync({
type: isNode ? 'nodebuffer' : 'blob',
mimeType: 'application/epub+zip',
compression: 'DEFLATE',
compressionOptions: {level: 9}
})
.then((file) => {
this.cachedFile = file
return file
})
}
// example usage: .pipe(fs.createWriteStream(filename))
streamFile () {
if (!this.zip) {
return null
}
return this.zip
.generateNodeStream({
type: 'nodebuffer',
streamFiles: false,
mimeType: 'application/epub+zip',
compression: 'DEFLATE',
compressionOptions: {level: 9}
})
}
// Internal/private methods
progress (part, percent, status) {
let parts = 6
let partsize = 1 / parts
percent = (part / parts) + percent * partsize
this.trigger('progress', percent, status)
}
findRemoteResources (prefix, where, html) {
let remoteCounter = 1
let matchUrl = /]*\/([^">]*?))".*?>/g
let emoticonUrl = /static\.fimfiction\.net\/images\/emoticons\/([a-z_]*)\.[a-z]*$/
for (let ma; (ma = matchUrl.exec(html));) {
let url = ma[1]
let cleanurl = decodeURI(entities.decode(url))
if (this.remoteResources.has(cleanurl)) {
let r = this.remoteResources.get(cleanurl)
if (r.where.indexOf(where) === -1) {
r.where.push(where)
}
continue
}
let filename = prefix + '_' + remoteCounter
let emoticon = url.match(emoticonUrl)
if (emoticon) {
filename = 'emoticon_' + emoticon[1]
}
remoteCounter++
this.remoteResources.set(cleanurl, {filename: filename, where: [where], originalUrl: url})
}
}
fetchCoverImage () {
if (this.coverImage) {
return this.coverImage
}
this.coverImage = null
let url = this.storyInfo.full_image
if (!url) {
return null
}
this.progress(0, 0.6, 'Fetching cover image...')
return fetchRemote(url, 'arraybuffer').then((data) => {
this.progress(0, 1)
let info = fileType(isNode ? data : new Uint8Array(data))
if (info) {
let type = info.mime
let isImage = type.indexOf('image/') === 0
if (!isImage) {
return null
}
let filename = 'Images/cover.' + info.ext
this.coverFilename = filename
this.coverType = type
this.coverImageDimensions = sizeOf(new Buffer(data))
this.coverImage = data
this.coverFilename = filename
return this.coverImage
} else {
return null
}
})
}
fetchTitlePage () {
this.progress(0, 0.3, 'Fetching title page...')
let url = this.storyInfo.url.replace('http://www.fimfiction.net', '')
return fetch(url).then(this.extractTitlePageInfo.bind(this))
}
extractTitlePageInfo (html) {
this.progress(0, 0.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()
this.description = description
html = html.substring(endDescPos + 7)
let extraPos = html.indexOf('