@@ -13,13 +13,14 @@ const dotenv = require('dotenv').config({ path: path })
13
13
let epub
14
14
let maxPages = process . env . max_pages - 1
15
15
let currentPage = 0
16
- const subreddit = process . env . subreddit
16
+ let subreddit = process . env . subreddit
17
17
const page = process . env . page || 'new'
18
18
const from = process . env . from || 'all'
19
19
let url = ''
20
20
let urlExtra = ''
21
21
let metadata = { }
22
22
let comments_include = false
23
+ let wikiLinks = [ ]
23
24
24
25
//one request per second
25
26
const axios = axiosClass . create ( )
@@ -34,9 +35,40 @@ async function main() {
34
35
process . exit ( )
35
36
}
36
37
38
+ if ( process . env . wikipage !== undefined ) {
39
+ await axios
40
+ . get ( process . env . wikipage + '.json' )
41
+ . then ( async r => {
42
+ if ( r . data . kind !== 'wikipage' ) {
43
+ console . log ( 'Invalid wikipage link' )
44
+ await pause ( )
45
+ process . exit ( )
46
+ }
47
+ let regexp = / \( ( h t t p s .* \/ c o m m e n t s \/ .* ) \) / g
48
+ let matches = r . data . data . content_md . matchAll ( regexp )
49
+ for ( const match of matches ) {
50
+ wikiLinks . push ( match [ 1 ] )
51
+ }
52
+ } )
53
+ . catch ( async _ => {
54
+ console . log ( 'Invalid wikipage link' )
55
+ await pause ( )
56
+ process . exit ( )
57
+ } )
58
+ if ( wikiLinks . length === 0 ) {
59
+ console . log ( 'No links found in wikipage' )
60
+ await pause ( )
61
+ process . exit ( )
62
+ } else {
63
+ // set defaultvalues if wikiLinks is not empty
64
+ maxPages = wikiLinks . length - 1
65
+ subreddit = process . env . wikipage_title
66
+ }
67
+ }
68
+
37
69
// checking if configuration is valid and setting default values
38
- if ( process . env . subreddit === undefined ) {
39
- console . log ( 'Missing subreddit from config file' )
70
+ if ( process . env . subreddit === undefined && process . env . wikipage === undefined ) {
71
+ console . log ( 'Missing subreddit or wikipage from config file' )
40
72
await pause ( )
41
73
process . exit ( )
42
74
}
@@ -65,10 +97,11 @@ async function main() {
65
97
66
98
url = '/' + page + '.json?limit=10&sort=' + page + urlExtra
67
99
100
+ let filename = wikiLinks . length > 0 ? subreddit . replace ( / [ ^ a - z 0 - 9 ] / gi, '_' ) . toLowerCase ( ) : subreddit . split ( '/' ) . pop ( )
68
101
metadata = {
69
102
id : Date . now ( ) ,
70
- title : subreddit ,
71
- series : subreddit ,
103
+ title : subreddit . replace ( / [ ^ a - z 0 - 9 ] / gi , ' ' ) ,
104
+ series : subreddit . replace ( / [ ^ a - z 0 - 9 ] / gi , ' ' ) ,
72
105
sequence : 1 ,
73
106
author : 'Anonymous' ,
74
107
fileAs : 'Anonymous' ,
@@ -98,31 +131,41 @@ async function main() {
98
131
process . env . comments_max_replies_indentation = process . env . comments_max_replies_indentation || 2
99
132
100
133
// just to get this as async function
101
- generateEbook ( )
134
+ generateEbook ( wikiLinks . length > 0 )
102
135
}
103
136
104
137
async function pause ( ) {
105
138
readlineSync . question ( 'Press enter to continue...' )
106
139
}
107
140
108
- async function generateEbook ( ) {
109
- console . log ( 'Creating ebook from: ' + subreddit + ' (' + page + ', links from ' + ( [ 'all' , 'new' ] . includes ( from ) ? '' : 'past ' ) + from + ')' + ( comments_include ? ' (comments enabled)' : '' ) )
141
+ async function generateEbook ( wikipage = false ) {
142
+ if ( wikipage ) {
143
+ console . log ( 'Generating wikipage ebook: ' + process . env . wikipage_title + ( comments_include ? ' (comments enabled)' : '' ) )
144
+ } else {
145
+ console . log ( 'Creating ebook from: ' + subreddit + ' (' + page + ', links from ' + ( [ 'all' , 'new' ] . includes ( from ) ? '' : 'past ' ) + from + ')' + ( comments_include ? ' (comments enabled)' : '' ) )
146
+ }
110
147
111
148
//creating custom cover with subreddit as text
112
149
await createCover ( )
113
150
epub = makepub . document ( metadata , './cover/cover.jpg' )
114
151
epub . addCSS ( 'h1>a{color:inherit;text-decoration:none}.comment-parent{margin-left:0!important}.comment{margin-left:5px;padding-left:5px;border-left:1px solid gray;}' )
115
152
116
- await getContent ( 'https://old.reddit.com/' + subreddit + url )
153
+ if ( wikipage ) {
154
+ await getWikipageContent ( )
155
+ } else {
156
+ await getContent ( 'https://old.reddit.com/' + subreddit + url , wikipage )
157
+ }
158
+
159
+ let filename = wikiLinks . length > 0 ? subreddit . replace ( / [ ^ a - z 0 - 9 ] / gi, '_' ) . toLowerCase ( ) : subreddit . split ( '/' ) . pop ( )
117
160
118
161
await epub . writeEPUB (
119
162
function ( e ) {
120
163
console . log ( 'Error:' , e )
121
164
} ,
122
165
'./output' ,
123
- subreddit . split ( '/' ) . pop ( ) ,
166
+ filename ,
124
167
async function ( ) {
125
- ora ( 'EPUB created to output/' + subreddit . split ( '/' ) . pop ( ) + '.epub\n' )
168
+ ora ( 'EPUB created to output/' + filename + '.epub\n' )
126
169
. start ( )
127
170
. succeed ( )
128
171
@@ -140,32 +183,7 @@ async function getContent(url) {
140
183
const spinner = ora ( 'Current page: ' + ( currentPage + 1 ) + '/' + ( maxPages + 1 ) ) . start ( )
141
184
spinner . start ( )
142
185
await asyncForEach ( r . data . data . children , async c => {
143
- // we only want selfposts and non-sticky posts
144
- if ( ! c . data . is_self || c . data . stickied ) return
145
- let comments = ''
146
-
147
- // load comments if they are enabled
148
- if ( comments_include ) {
149
- comments = await getComments ( c . data . url . slice ( 0 , - 1 ) + '.json?sort=confidence' )
150
- if ( comments !== '' ) comments = '<br /><h3>Comments<hr /></h3>' + comments
151
- }
152
-
153
- //add section to epub. Title as h1 with link. small text for author and date.
154
- epub . addSection (
155
- c . data . title ,
156
- "<h1><a href='" +
157
- c . data . url +
158
- "'>" +
159
- c . data . title +
160
- '</a></h1>' +
161
- '<small><i>By</i> ' +
162
- c . data . author +
163
- ' <i>on</i> ' +
164
- new Date ( c . data . created * 1000 ) . toISOString ( ) . split ( 'T' ) [ 0 ] +
165
- '</small>' +
166
- decode ( c . data . selftext_html ) . replace ( '<!-- SC_ON -->' , '' ) +
167
- comments
168
- )
186
+ await addPost ( c )
169
187
} )
170
188
spinner . succeed ( )
171
189
process . stdout . write ( '\r' )
@@ -180,6 +198,54 @@ async function getContent(url) {
180
198
} )
181
199
}
182
200
201
+ async function getWikipageContent ( ) {
202
+ await asyncForEach ( wikiLinks , async link => {
203
+ await axios
204
+ . get ( link + '.json' )
205
+ . then ( async r => {
206
+ const spinner = ora ( 'Current page: ' + ( currentPage + 1 ) + '/' + ( maxPages + 1 ) ) . start ( )
207
+ spinner . start ( )
208
+ await addPost ( r . data [ 0 ] . data . children [ 0 ] )
209
+ spinner . succeed ( )
210
+ process . stdout . write ( '\r' )
211
+
212
+ currentPage ++
213
+ } )
214
+ . catch ( function ( error ) {
215
+ console . log ( error )
216
+ } )
217
+ } )
218
+ }
219
+
220
+ async function addPost ( c ) {
221
+ // we only want selfposts and non-sticky posts
222
+ if ( ! c . data . is_self || c . data . stickied ) return
223
+ let comments = ''
224
+
225
+ // load comments if they are enabled
226
+ if ( comments_include ) {
227
+ comments = await getComments ( c . data . url . slice ( 0 , - 1 ) + '.json?sort=confidence' )
228
+ if ( comments !== '' ) comments = '<br /><h3>Comments<hr /></h3>' + comments
229
+ }
230
+
231
+ //add section to epub. Title as h1 with link. small text for author and date.
232
+ epub . addSection (
233
+ c . data . title ,
234
+ "<h1><a href='" +
235
+ c . data . url +
236
+ "'>" +
237
+ c . data . title +
238
+ '</a></h1>' +
239
+ '<small><i>By</i> ' +
240
+ c . data . author +
241
+ ' <i>on</i> ' +
242
+ new Date ( c . data . created * 1000 ) . toISOString ( ) . split ( 'T' ) [ 0 ] +
243
+ '</small>' +
244
+ decode ( c . data . selftext_html ) . replace ( '<!-- SC_ON -->' , '' ) +
245
+ comments
246
+ )
247
+ }
248
+
183
249
async function getComments ( url ) {
184
250
// get comments from url
185
251
const child = await axios ( url )
0 commit comments