1
+ // Build tool for uploading generated README.md to Reddit Wiki
2
+
3
+ const qs = require ( 'qs' ) ;
1
4
const fs = require ( 'fs-extra' ) ;
2
5
const path = require ( 'path' ) ;
3
6
const fetch = require ( 'node-fetch' ) ;
4
- const qs = require ( 'qs' ) ;
5
7
const { DateTime } = require ( 'luxon' ) ;
6
8
7
- // REDDIT_: For authentication with Reddit API. Oauth MUST be used. ID and Secret come from a "script" app type.
8
- const REDDIT_USER = process . env . REDDIT_USER || 'username' ;
9
- const REDDIT_PASS = process . env . REDDIT_PASS || 'password' ;
10
- const REDDIT_CLIENT_ID = process . env . REDDIT_CLIENT_ID || 'clientid' ;
11
- const REDDIT_CLIENT_SECRET = process . env . REDDIT_CLIENT_SECRET || 'clientsecret' ;
12
-
13
- // Initial basic authorization for getting the Oauth token
14
- const BASIC_AUTH = `Basic ${ Buffer . from ( REDDIT_CLIENT_ID + ':' + REDDIT_CLIENT_SECRET ) . toString ( 'base64' ) } ` ;
15
-
16
- // WIKI_: For the Reddit Wiki
17
- const WIKI_SUBREDDIT = 'privacy' ;
18
- const WIKI_PAGE = 'de-google' ;
19
- const WIKI_REASON = 'Automated edit from GitHub repo: https://github.com/tycrek/degoogle' ;
9
+ // REDDIT.: For authentication with Reddit API. Oauth MUST be used. ID and Secret come from a "script" app type.
10
+ const REDDIT = {
11
+ USER : process . env . REDDIT_USER || 'username' ,
12
+ PASS : process . env . REDDIT_PASS || 'password' ,
13
+ CLIENT_ID : process . env . REDDIT_CLIENT_ID || 'clientid' ,
14
+ CLIENT_SECRET : process . env . REDDIT_CLIENT_SECRET || 'clientsecret' ,
15
+ } ;
20
16
21
17
// Endpoints for each of our fetches to Reddit
22
18
const ENDPOINTS = {
23
- revisions : `https://old.reddit.com/r/${ WIKI_SUBREDDIT } /wiki/revisions/${ WIKI_PAGE } .json` ,
19
+ revisions : `https://old.reddit.com/r/privacy /wiki/revisions/de-google .json` ,
24
20
token : 'https://www.reddit.com/api/v1/access_token' ,
25
- edit : `https://oauth.reddit.com/r/${ WIKI_SUBREDDIT } /api/wiki/edit`
21
+ edit : `https://oauth.reddit.com/r/privacy /api/wiki/edit`
26
22
} ;
27
23
28
- // Helps POST data be submitted properly
29
- const CONTENT_TYPE = 'application/x-www-form-urlencoded' ;
30
-
31
24
// Update the wiki
32
25
Promise . all ( [ getLastRevision ( ) , getToken ( ) ] )
33
26
. then ( ( [ lastId , token ] ) => putWiki ( lastId , token ) )
@@ -52,8 +45,15 @@ function getToken() {
52
45
return new Promise ( ( resolve , reject ) =>
53
46
fetch ( ENDPOINTS . token , {
54
47
method : 'POST' ,
55
- headers : { 'Authorization' : BASIC_AUTH , 'Content-Type' : CONTENT_TYPE } ,
56
- body : qs . stringify ( { grant_type : 'password' , username : REDDIT_USER , password : REDDIT_PASS } )
48
+ headers : {
49
+ 'Authorization' : `Basic ${ Buffer . from ( `${ REDDIT . CLIENT_ID } :${ REDDIT . CLIENT_SECRET } ` ) . toString ( 'base64' ) } ` ,
50
+ 'Content-Type' : 'application/x-www-form-urlencoded'
51
+ } ,
52
+ body : qs . stringify ( {
53
+ grant_type : 'password' ,
54
+ username : REDDIT . USER ,
55
+ password : REDDIT . PASS
56
+ } )
57
57
} )
58
58
. then ( ( response ) => response . json ( ) )
59
59
. then ( ( { access_token } ) => resolve ( access_token ) )
@@ -69,11 +69,14 @@ function putWiki(lastId, token) {
69
69
return new Promise ( ( resolve , reject ) =>
70
70
fetch ( ENDPOINTS . edit , {
71
71
method : 'POST' ,
72
- headers : { 'Authorization' : `bearer ${ token } ` , 'Content-Type' : CONTENT_TYPE } ,
72
+ headers : {
73
+ 'Authorization' : `bearer ${ token } ` ,
74
+ 'Content-Type' : 'application/x-www-form-urlencoded'
75
+ } ,
73
76
body : qs . stringify ( {
74
- content : fixContent ( fs . readFileSync ( path . join ( __dirname , 'README.md' ) ) . toString ( ) ) ,
75
- page : WIKI_PAGE ,
76
- reason : WIKI_REASON ,
77
+ content : fixContent ( fs . readFileSync ( path . join ( process . cwd ( ) , 'README.md' ) ) . toString ( ) ) ,
78
+ page : 'de-google' ,
79
+ reason : 'Automated edit from GitHub repo: https://github.com/tycrek/degoogle' ,
77
80
previous : lastId
78
81
} )
79
82
} )
@@ -90,12 +93,12 @@ function putWiki(lastId, token) {
90
93
* @param {String } content The content in README.md
91
94
*/
92
95
function fixContent ( content ) {
93
- // Fix updated timestamp
94
- content = content . replace ( / \! \[ U p d a t e d \] ( .* ?) s q u a r e \) / g, `#### Updated: ${ DateTime . now ( ) . toFormat ( 'MMMM dd, yyyy' ) } ` ) ;
96
+ return content
95
97
96
- // Fix published timestamps
97
- content = content . replace ( / \! \[ P u b l i s h e d \] \( h t t p s \: \/ \/ i m g \. s h i e l d s \. i o \/ b a d g e \/ / g, '**' ) ;
98
- content = content . replace ( / - i n f o r m a t i o n a l \? s t y l e = f l a t - s q u a r e \) / g, '**' ) ;
98
+ // Fix updated timestamp
99
+ . replace ( / \! \[ U p d a t e d \] ( .* ?) s q u a r e \) / g, `#### Updated: ${ DateTime . now ( ) . toFormat ( 'MMMM dd, yyyy' ) } ` )
99
100
100
- return content ;
101
- } // * If this is highlighted weirdly, it's because of the 'updated timestamp' regex, don't worry about it
101
+ // Fix published timestamps
102
+ . replace ( / \! \[ P u b l i s h e d \] \( h t t p s \: \/ \/ i m g \. s h i e l d s \. i o \/ b a d g e \/ / g, '**' )
103
+ . replace ( / - i n f o r m a t i o n a l \? s t y l e = f l a t - s q u a r e \) / g, '**' ) ;
104
+ }
0 commit comments