-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (151 loc) · 4.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Generated by CoffeeScript 1.7.1
(function() {
var Tumblr2Peepub, cheerio, tumblr;
tumblr = require('tumblr');
cheerio = require('cheerio');
Tumblr2Peepub = function(tumblrConfig) {
var allPosts, bookDoc, fetchPosts, formatPosts, that, totalPosts;
that = this;
this.tumblrConfig = tumblrConfig;
allPosts = [];
totalPosts = false;
bookDoc = {};
fetchPosts = function(cb, offset) {
return setTimeout(function() {
var o;
if (totalPosts === false || totalPosts > allPosts.length) {
o = offset || 0;
return that.blog.posts({
offset: o
}, function(err, res) {
if (err) {
return cb(err);
}
if (that.tumblrConfig.verbose) {
console.log(that.blogUrl + ": " + o + '/' + res.total_posts);
}
bookDoc = {
title: res.blog.title || that.blogUrl,
url: 'http://' + that.blogUrl,
subtitle: res.blog.description,
description: res.blog.description
};
totalPosts = res.total_posts;
allPosts = allPosts.concat(res.posts);
return fetchPosts(cb, allPosts.length);
});
} else {
return cb(null, allPosts);
}
}, 500);
};
formatPosts = function(p) {
var $, body, embed_code, exp, pbody, pcap, pdesc, pphotos, pquotesrc, psource, ptext, ptitle, pvideo, regex, title, video_domain, video_url;
if (p.title) {
ptitle = "<h1>" + p.title + "</h1>";
} else {
ptitle = "";
}
if (p.source_url) {
psource = "<p>Source: <a href=\"" + p.source_url + "\">" + p.source_title + "</a></p>";
} else {
psource = "";
}
if (p.body) {
pbody = p.body;
} else {
pbody = "";
}
if (p.text) {
ptext = "<h1><blockquote>“" + p.text + "”</blockquote></h1>";
} else {
ptext = "";
}
if (p.source) {
pquotesrc = "<p>— " + p.source + "</p>";
} else {
pquotesrc = "";
}
if (p.url) {
if (p.title) {
ptitle = "<h1><a href=\"" + p.url + "\">" + p.title + "</a></h1>";
} else {
ptitle = "<h1><a href=\"" + p.url + "\">" + p.url + "</a></h1>";
}
}
if (p.description) {
pdesc = p.description;
} else {
pdesc = "";
}
if (p.player) {
if (p.player[0]["embed_code"]) {
embed_code = p.player[0]["embed_code"];
$ = cheerio.load(embed_code);
video_url = $('iframe').attr('src');
if (video_url) {
if (video_url.indexOf('//') === 0) {
video_url = video_url.replace('//', 'http://');
}
video_domain = video_url.replace('http://', '').replace('https://', '').split(/[/?#]/)[0];
if (video_domain === "") {
video_domain = video_url;
}
pvideo = "<h1><p>Video: <a href=\"" + video_url + "\">" + video_domain + "</a></p></h1>";
}
} else {
pvideo = "";
}
} else {
pvideo = "";
}
if (p.photos) {
pphotos = p.photos.map(function(photo) {
return "<p><img src=\"" + photo.original_size.url + "\" /></p>";
}).join('');
} else {
pphotos = "";
}
if (p.caption) {
pcap = p.caption;
} else {
pcap = "";
}
title = p.date.replace(" GMT", "");
body = ptitle + ptext + pphotos + pvideo + pcap + pbody + pquotesrc + pdesc + psource;
exp = /<iframe.+<\/iframe>/g;
regex = new RegExp(exp);
body = body.replace(regex, '');
return {
title: title,
body: body,
toc: true
};
};
this.fetch = function(tumblrPrefix, cb) {
this.blogUrl = tumblrPrefix + '.tumblr.com';
this.blog = new tumblr.Blog(this.blogUrl, this.tumblrConfig);
allPosts = [];
totalPosts = false;
bookDoc = {};
return fetchPosts(function(err, posts) {
var photos;
if (err) {
return cb(err);
}
photos = posts.filter(function(p) {
return p.photos;
}).map(function(p) {
return p.photos[0].original_size.url;
});
bookDoc.cover = photos ? photos[photos.length - 1] : "https://raw.githubusercontent.com/peoples-e/tumblr-2-pe-epub/master/assets/cover.jpg";
bookDoc.pages = posts.map(formatPosts).filter(function(p) {
return p.body != null;
});
return cb(null, bookDoc);
});
};
return this;
};
module.exports = Tumblr2Peepub;
}).call(this);