Downloads book content.
This commit is contained in:
101
src/sidebar.js
101
src/sidebar.js
@@ -77,7 +77,7 @@ class Book{
|
|||||||
console.info(`Downloading book info for ${this.book_id}`);
|
console.info(`Downloading book info for ${this.book_id}`);
|
||||||
let url = `https://www.safaribooksonline.com/api/v1/book/${this.book_id}/`;
|
let url = `https://www.safaribooksonline.com/api/v1/book/${this.book_id}/`;
|
||||||
return this.downloadJson(url)
|
return this.downloadJson(url)
|
||||||
.then((book_info)=>{
|
.then((book_info) => {
|
||||||
this.book_info = book_info;
|
this.book_info = book_info;
|
||||||
}, onError);
|
}, onError);
|
||||||
}
|
}
|
||||||
@@ -86,7 +86,7 @@ class Book{
|
|||||||
function helper(book, url){
|
function helper(book, url){
|
||||||
console.info(`Downloading chapter list ${url}`);
|
console.info(`Downloading chapter list ${url}`);
|
||||||
return book.downloadJson(url)
|
return book.downloadJson(url)
|
||||||
.then((chapter_list)=>{
|
.then((chapter_list) => {
|
||||||
book.chapter_list
|
book.chapter_list
|
||||||
= book.chapter_list.concat(chapter_list.results);
|
= book.chapter_list.concat(chapter_list.results);
|
||||||
|
|
||||||
@@ -105,21 +105,18 @@ class Book{
|
|||||||
downloadMetaContent(){
|
downloadMetaContent(){
|
||||||
let downloads = []
|
let downloads = []
|
||||||
|
|
||||||
this.insertBookFile({
|
this.insertBookFile(
|
||||||
url: this.book_info.cover,
|
this.book_info.cover,
|
||||||
file: "cover.img",
|
"cover.img")
|
||||||
context: null,
|
|
||||||
mime: null
|
|
||||||
})
|
|
||||||
|
|
||||||
downloads.push(
|
downloads.push(
|
||||||
this.downloadJson(this.book_info.toc)
|
this.downloadJson(this.book_info.toc)
|
||||||
.then((json)=>{
|
.then((json) => {
|
||||||
this.book_toc=json
|
this.book_toc=json
|
||||||
}))
|
}))
|
||||||
downloads.push(
|
downloads.push(
|
||||||
this.downloadJson(this.book_info.flat_toc)
|
this.downloadJson(this.book_info.flat_toc)
|
||||||
.then((json)=>{
|
.then((json) => {
|
||||||
this.book_flattoc=json
|
this.book_flattoc=json
|
||||||
}))
|
}))
|
||||||
downloads.push(
|
downloads.push(
|
||||||
@@ -135,41 +132,49 @@ class Book{
|
|||||||
|
|
||||||
|
|
||||||
extractChapterAssets(json){
|
extractChapterAssets(json){
|
||||||
if (!json.asset_base_url
|
if (!json.asset_base_url || !json.content){
|
||||||
|| !json.content){
|
|
||||||
throw "Missing data."
|
throw "Missing data."
|
||||||
}
|
}
|
||||||
|
|
||||||
// Html
|
// Html
|
||||||
this.insertBookFile({
|
this.insertBookFile(
|
||||||
url: json.content,
|
json.content,
|
||||||
file: json.full_path,
|
json.full_path)
|
||||||
context: null,
|
|
||||||
mime: null,
|
|
||||||
content: null,
|
|
||||||
})
|
|
||||||
// List of images.
|
// List of images.
|
||||||
for (let idx in json.images){
|
for (let idx in json.images){
|
||||||
this.insertBookFile({
|
this.insertBookFile(
|
||||||
url: json.asset_base_url + json.images[idx],
|
json.asset_base_url + json.images[idx],
|
||||||
file: json.images[idx],
|
json.images[idx])
|
||||||
context: null,
|
|
||||||
mime: null
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
// List of stylesheets.
|
// List of stylesheets.
|
||||||
for (let idx in json.stylesheets){
|
for (let idx in json.stylesheets){
|
||||||
this.insertBookFile({
|
this.insertBookFile(
|
||||||
url: json.stylesheets[idx].original_url,
|
json.stylesheets[idx].original_url,
|
||||||
file: json.stylesheets[idx].full_path,
|
json.stylesheets[idx].full_path)
|
||||||
context: null,
|
|
||||||
mime: null
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
insertBookFile(obj){
|
downloadContent(){
|
||||||
this.book_files[obj.url] = obj
|
return Promise.map(Object.keys(this.book_files), (url) => {
|
||||||
|
return this.downloadResource(url)
|
||||||
|
.then((res) => {
|
||||||
|
if (res.ok){
|
||||||
|
console.log(res.headers.get('Content-Type'))
|
||||||
|
this.book_files[url].headers = res.headers
|
||||||
|
this.book_files[url].mime = res.headers.get('Content-Type')
|
||||||
|
this.book_files[url].body = res.blob()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},{concurrency: 1})
|
||||||
|
}
|
||||||
|
|
||||||
|
insertBookFile(url, filename){
|
||||||
|
this.book_files[url] = {
|
||||||
|
url: url,
|
||||||
|
filename: filename,
|
||||||
|
headers: null,
|
||||||
|
mime: null,
|
||||||
|
body: null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,7 +187,6 @@ function renderInfo(book){
|
|||||||
function renderChapterList(book){
|
function renderChapterList(book){
|
||||||
// Add chapters to UI
|
// Add chapters to UI
|
||||||
for (let chapter_idx in book.chapter_list) {
|
for (let chapter_idx in book.chapter_list) {
|
||||||
console.log(book.chapter_list[chapter_idx])
|
|
||||||
let chapter = book.chapter_list[chapter_idx];
|
let chapter = book.chapter_list[chapter_idx];
|
||||||
var chapter_dom = $("<li></li>")
|
var chapter_dom = $("<li></li>")
|
||||||
.addClass("list-group-item")
|
.addClass("list-group-item")
|
||||||
@@ -194,15 +198,11 @@ function renderChapterList(book){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createEpub(book, epub){
|
function createEpub(book, epub){
|
||||||
for (let url in book.raw_book){
|
|
||||||
let name = "meta/"+book.raw_book[url].url.replace(/[^a-z0-9]/gi, '_').toLowerCase()
|
|
||||||
epub.addFile(name, book.raw_book[url].clone().blob())
|
|
||||||
}
|
|
||||||
// epub.addFile("book_info.json", JSON.stringify(book.book_info))
|
|
||||||
// epub.addFile("book_files.json", JSON.stringify(book.book_files))
|
|
||||||
// epub.addFile("book_toc.json", JSON.stringify(book.book_toc))
|
|
||||||
// epub.addFile("book_flattoc.json", JSON.stringify(book.book_flattoc))
|
|
||||||
epub.addFile("book.json", JSON.stringify(book, null, '\t'))
|
epub.addFile("book.json", JSON.stringify(book, null, '\t'))
|
||||||
|
for (let url in book.book_files){
|
||||||
|
file = book.book_files[url]
|
||||||
|
epub.addFile(file.filename, file.body)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onDownloadBookClicked(){
|
function onDownloadBookClicked(){
|
||||||
@@ -218,15 +218,18 @@ function onDownloadBookClicked(){
|
|||||||
book = new Book(book_id);
|
book = new Book(book_id);
|
||||||
book.downloadBookInfo()
|
book.downloadBookInfo()
|
||||||
.then(() => { renderInfo(book); })
|
.then(() => { renderInfo(book); })
|
||||||
.then(() => { return book.downloadChapterList(); })
|
// .then(() => { return book.downloadChapterList(); })
|
||||||
.then(() => { return renderChapterList(book); })
|
// .then(() => { return renderChapterList(book); })
|
||||||
.then(() => { return book.downloadMetaContent(); })
|
.then(() => { return book.downloadMetaContent(); })
|
||||||
// .then(() => { return book.downloadContent(); })
|
.then(() => { return book.downloadContent(); })
|
||||||
.then(() => { return createEpub(book, epub); })
|
.then(() => { return createEpub(book, epub); })
|
||||||
.then(() => { return epub.generateAsync(); })
|
.then(() => { return epub.generateAsync(); })
|
||||||
.then((file) => {
|
.then((file) => {
|
||||||
let filename = "books/" + "test" + ".zip"
|
let title = book.book_info.title
|
||||||
// book.filename.replace(/[^a-z0-9]/gi, '_').toLowerCase()
|
let filename = "books/"
|
||||||
|
+ title.replace(/[^a-z0-9]/gi, '_').toLowerCase()
|
||||||
|
+ ".zip"
|
||||||
|
console.log(`Zip file name ${filename}`)
|
||||||
let url = window.URL.createObjectURL(file)
|
let url = window.URL.createObjectURL(file)
|
||||||
browser.downloads.download({ "filename" : filename, url : url})
|
browser.downloads.download({ "filename" : filename, url : url})
|
||||||
})
|
})
|
||||||
@@ -248,6 +251,6 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
|
|
||||||
$('#deselect-all-button').show()
|
$('#deselect-all-button').show()
|
||||||
$('#download-button').show()
|
$('#download-button').show()
|
||||||
$('#download-section').hide();
|
$('#download-section').hide();
|
||||||
// let bookInfo = new BookInfo();
|
// let bookInfo = new BookInfo();
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user