2020-06-05 14:06:59 +00:00
|
|
|
const fs = require('fs-extra'); // Reading README.md
|
|
|
|
const path = require('path'); // Get the correct path for README.md
|
|
|
|
const fetch = require('node-fetch'); // Make calls to Reddit from Node.js
|
|
|
|
const qs = require('qs'); // Properly build a query for node-fetch POST
|
2020-06-05 14:30:23 +00:00
|
|
|
const moment = require('moment'); // Time-related functions
|
2020-06-02 21:20:31 +00:00
|
|
|
|
2021-08-09 18:12:15 +00:00
|
|
|
//#region constants
|
|
|
|
|
2020-06-05 14:06:59 +00:00
|
|
|
// REDDIT_: For authentication with Reddit API. Oauth MUST be used. ID and Secret come from a "script" app type.
|
2020-06-02 21:20:31 +00:00
|
|
|
const REDDIT_USER = process.env.REDDIT_USER || 'username';
|
|
|
|
const REDDIT_PASS = process.env.REDDIT_PASS || 'password';
|
|
|
|
const REDDIT_CLIENT_ID = process.env.REDDIT_CLIENT_ID || 'clientid';
|
|
|
|
const REDDIT_CLIENT_SECRET = process.env.REDDIT_CLIENT_SECRET || 'clientsecret';
|
2020-06-05 14:06:59 +00:00
|
|
|
|
2021-08-09 18:12:15 +00:00
|
|
|
// Initial basic authorization for getting the Oauth token
|
|
|
|
const BASIC_AUTH = `Basic ${Buffer.from(REDDIT_CLIENT_ID + ':' + REDDIT_CLIENT_SECRET).toString('base64')}`;
|
|
|
|
|
2020-06-05 14:06:59 +00:00
|
|
|
// WIKI_: For the Reddit Wiki
|
2020-06-02 21:20:31 +00:00
|
|
|
const WIKI_SUBREDDIT = 'privacy';
|
2020-06-08 14:01:10 +00:00
|
|
|
const WIKI_PAGE = 'de-google';
|
2020-06-05 14:06:59 +00:00
|
|
|
const WIKI_REASON = 'Automated edit from GitHub repo: https://github.com/tycrek/degoogle';
|
|
|
|
|
2021-08-09 18:12:15 +00:00
|
|
|
// Endpoints for each of our fetches to Reddit
|
|
|
|
const ENDPOINTS = {
|
|
|
|
revisions: `https://old.reddit.com/r/${WIKI_SUBREDDIT}/wiki/revisions/${WIKI_PAGE}.json`,
|
|
|
|
token: 'https://www.reddit.com/api/v1/access_token',
|
|
|
|
edit: `https://oauth.reddit.com/r/${WIKI_SUBREDDIT}/api/wiki/edit`
|
|
|
|
};
|
|
|
|
|
2020-06-05 14:06:59 +00:00
|
|
|
// Helps POST data be submitted properly
|
2020-06-02 21:20:31 +00:00
|
|
|
const CONTENT_TYPE = 'application/x-www-form-urlencoded';
|
2021-08-09 18:12:15 +00:00
|
|
|
//#endregion
|
2020-06-02 21:20:31 +00:00
|
|
|
|
2021-08-09 18:12:15 +00:00
|
|
|
// Update the wiki
|
|
|
|
Promise.all([getLastRevision(), getToken()])
|
|
|
|
.then(([lastId, token]) => putWiki(lastId, token))
|
|
|
|
.catch((err) => (console.error(err), process.exit(1)));
|
2020-06-02 21:20:31 +00:00
|
|
|
|
2021-08-09 18:12:15 +00:00
|
|
|
//#region functions
|
2020-06-05 14:06:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the last revision ID on the Wiki. Required otherwise editing the wiki fails
|
|
|
|
*/
|
2021-08-09 18:12:15 +00:00
|
|
|
function getLastRevision() {
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
fetch(ENDPOINTS.revisions)
|
2020-06-05 14:06:59 +00:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((json) => json.data.children[0].id) // children[0] is the most recent edit
|
2021-08-09 18:12:15 +00:00
|
|
|
.then(resolve)
|
|
|
|
.catch(reject));
|
2020-06-05 14:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an Oauth token used to edit the Wiki
|
|
|
|
*/
|
2021-08-09 18:12:15 +00:00
|
|
|
function getToken() {
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
fetch(ENDPOINTS.token, {
|
2020-06-02 21:20:31 +00:00
|
|
|
method: 'POST',
|
2021-08-09 18:12:15 +00:00
|
|
|
headers: { 'Authorization': BASIC_AUTH, 'Content-Type': CONTENT_TYPE },
|
2020-06-02 21:20:31 +00:00
|
|
|
body: qs.stringify({ grant_type: 'password', username: REDDIT_USER, password: REDDIT_PASS })
|
2020-06-05 14:06:59 +00:00
|
|
|
})
|
|
|
|
.then((response) => response.json())
|
2021-08-09 18:12:15 +00:00
|
|
|
.then(({ access_token }) => resolve(access_token))
|
|
|
|
.catch(reject));
|
2020-06-05 14:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post the contents from the README into the wiki
|
|
|
|
* @param {String} lastId Revision ID for the Wiki
|
|
|
|
* @param {String} token Oauth token for authenticating with the Reddit API
|
|
|
|
*/
|
2021-08-09 18:12:15 +00:00
|
|
|
function putWiki(lastId, token) {
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
fetch(ENDPOINTS.edit, {
|
2020-06-02 21:20:31 +00:00
|
|
|
method: 'POST',
|
2020-06-05 14:06:59 +00:00
|
|
|
headers: { 'Authorization': `bearer ${token}`, 'Content-Type': CONTENT_TYPE },
|
2020-06-02 21:20:31 +00:00
|
|
|
body: qs.stringify({
|
2020-06-05 14:30:23 +00:00
|
|
|
content: fixContent(fs.readFileSync(path.join(__dirname, 'README.md')).toString()),
|
2020-06-02 21:20:31 +00:00
|
|
|
page: WIKI_PAGE,
|
|
|
|
reason: WIKI_REASON,
|
|
|
|
previous: lastId
|
|
|
|
})
|
2020-06-05 14:06:59 +00:00
|
|
|
})
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((json) => {
|
2021-08-09 18:12:15 +00:00
|
|
|
if (Object.keys(json).length === 0) resolve();
|
2020-06-05 14:06:59 +00:00
|
|
|
else throw Error(json);
|
|
|
|
})
|
2021-08-09 18:12:15 +00:00
|
|
|
.catch(reject));
|
2020-06-03 22:40:34 +00:00
|
|
|
}
|
2020-06-05 14:30:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fixes certain images to only show text on the Reddit wiki
|
|
|
|
* @param {String} content The content in README.md
|
|
|
|
*/
|
|
|
|
function fixContent(content) {
|
|
|
|
// Fix updated timestamp
|
2020-06-05 14:34:26 +00:00
|
|
|
content = content.replace(/\!\[Updated\](.*?)square\)/g, `#### Updated: ${moment().format('MMMM Do YYYY')}`);
|
2020-06-05 14:30:23 +00:00
|
|
|
|
|
|
|
// Fix published timestamps
|
|
|
|
content = content.replace(/\!\[Published\]\(https\:\/\/img\.shields\.io\/badge\//g, '**');
|
|
|
|
content = content.replace(/-informational\?style=flat-square\)/g, '**');
|
|
|
|
|
|
|
|
return content;
|
2021-08-09 18:12:15 +00:00
|
|
|
} // * If this is highlighted weirdly, it's because of the 'updated timestamp' regex, don't worry about it
|
|
|
|
//#endregion
|