Skip to content

Instantly share code, notes, and snippets.

@ufologist
Created January 17, 2013 12:20
Show Gist options
  • Save ufologist/4555584 to your computer and use it in GitHub Desktop.
Save ufologist/4555584 to your computer and use it in GitHub Desktop.
如何导出/下载豆瓣阅读中的电子书
/**
* 分析豆瓣阅读查看电子书的逻辑
*
* 主要用到的JavaScript为
* 1. OzJS(管理模块)
* 2. jQuery(base库)
* 3. Backbone.js(web application框架)
*
* 过程分析
* --------
* 打开电子书
* http://read.douban.com/reader/ebook/381109/page/1/
*
* 获得电子书数据, 返回数据格式为: {"data": "加密字符串为电子书内容", "time": "1354001385:1:0"},
* 会保存到localStorage中
* POST /j/article/get_reader_data
*
* Form Data
* -----------
* ck: ""
* aid: 381109
* -----------
* ck就是cookie中的ck
* aid就是电子书的id, 在url中有体现
*
* @author Sun
* @version 1.0 2012-11-27
*
* @see http://img3.douban.com/ark/js/dist/reader/main-9317859386.js
* define("reader/views/reading/article"
* fetchArticle
* define("reader/modules/prettify"
* define("mod/cookie"
*
* 警告: 本程序是出于学习目的, 仅限于分析研究时用, 不可用于非法用途
* 由于程序所造成的损失本人概不负责, 如有造成不便请通知本人删除程序
*/
(function() {
define('_test/util', function() {
function getArticleId() {
return location.href.match(/ebook\/(\d+)\//)[1];
}
function logArticle(article) {
console.log(article);
var post = article.posts[0];
var contents = post.contents;
console.log(post.title);
console.log(article.price);
for (var i = 0, length = contents.length; i < length; i++) {
var content = contents[i];
var contentType = content.type;
if (contentType === 'headline') {
console.log('\n' + content.data.text);
} else if (contentType === 'paragraph') {
console.log(content.data.text);
} else if (contentType === 'illus') {
// console.warn('<img src="' + content.data.size.tiny.src + '" />');
}
}
}
return {
getArticleId: getArticleId,
logArticle: logArticle
};
});
// 获取用户所有电子书列表
// var getArticlesApi = "/j/articles/bookshelf";
// 必须先购买该电子书(免费的只要加入阅读列表即可)才能成功获取电子书数据
// http://read.douban.com/ebook/354583/
// 目前测试页数最多的免费电子书(146页)
// 似乎表明电子书的数据是一次性全部加载的, 无论有多少页?
// 由于只是测试的免费书, 收费中找到一本页码比较多的(510页)
// http://read.douban.com/ebook/348972/
// 没有办法测试, 暂时只做猜测吧
require(['jquery', 'reader/modules/prettify', 'mod/cookie', 'reader/collections/pages', '_test/util'], function($, prettify, cookie, Collection, util) {
new Collection().fetch({
type: "POST",
data: {
ck: cookie('ck'),
aid: util.getArticleId()
}
}).success(function(data) {
// var t = $.parseJSON(g.dec(v.data))
// localStorage["e" + o] = v.data + v.time
var article = $.parseJSON(prettify.dec(data.data));
util.logArticle(article);
}).error(function(r) {
console.error(r);
});
});
// 购买豆瓣阅读买电子书
// POST http://read.douban.com/j/article/367102/buy
// secretly:
// ck:""
//
// 购买成功返回
// {"r":0,"order":0,"fiction":1}
// 购买失败返回
// {"r":1,"err":"余额不足"}
//
// $.post('/j/article/301290/buy', {
// secretly: '',
// ck: ''
// }, function(data) {
// console.log(data);
// });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment