From e7842e453f861fd532eb92eef6bac1de8760afe5 Mon Sep 17 00:00:00 2001 From: tycrek Date: Mon, 9 Aug 2021 11:12:15 -0700 Subject: [PATCH] improved wiki script --- _wiki.js | 85 ++++++++++++++++++++++++-------------------------------- 1 file changed, 36 insertions(+), 49 deletions(-) diff --git a/_wiki.js b/_wiki.js index 9c0db47..9d3b796 100644 --- a/_wiki.js +++ b/_wiki.js @@ -4,88 +4,75 @@ const fetch = require('node-fetch'); // Make calls to Reddit from Node.js const qs = require('qs'); // Properly build a query for node-fetch POST const moment = require('moment'); // Time-related functions +//#region constants + // REDDIT_: For authentication with Reddit API. Oauth MUST be used. ID and Secret come from a "script" app type. 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'; +// Initial basic authorization for getting the Oauth token +const BASIC_AUTH = `Basic ${Buffer.from(REDDIT_CLIENT_ID + ':' + REDDIT_CLIENT_SECRET).toString('base64')}`; + // WIKI_: For the Reddit Wiki const WIKI_SUBREDDIT = 'privacy'; const WIKI_PAGE = 'de-google'; const WIKI_REASON = 'Automated edit from GitHub repo: https://github.com/tycrek/degoogle'; +// 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` +}; + // Helps POST data be submitted properly const CONTENT_TYPE = 'application/x-www-form-urlencoded'; +//#endregion -updateWiki(); +// Update the wiki +Promise.all([getLastRevision(), getToken()]) + .then(([lastId, token]) => putWiki(lastId, token)) + .catch((err) => (console.error(err), process.exit(1))); -/** - * Update the Reddit wiki - */ -function updateWiki() { - // Endpoints for each of our fetches to Reddit - let 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` - }; - - // Initial basic authorization for getting the Oauth token - let basicAuth = `Basic ${Buffer.from(REDDIT_CLIENT_ID + ':' + REDDIT_CLIENT_SECRET).toString('base64')}`; - - let lastId, token; - - getLastRevision(endpoints.revisions) - .then((mLastId) => lastId = mLastId) - .then(() => getToken(endpoints.token, basicAuth)) - .then((mToken) => token = mToken) - .then(() => putWiki(endpoints.edit, lastId, token)) - .catch((err) => (console.error(err), process.exit(1))); -} +//#region functions /** * Get the last revision ID on the Wiki. Required otherwise editing the wiki fails - * @param {String} endpoint Endpoint of where to get the ID's */ -function getLastRevision(endpoint) { - return new Promise((resolve, reject) => { - fetch(endpoint) +function getLastRevision() { + return new Promise((resolve, reject) => + fetch(ENDPOINTS.revisions) .then((response) => response.json()) .then((json) => json.data.children[0].id) // children[0] is the most recent edit - .then((lastId) => resolve(lastId)) - .catch((err) => reject(err)); - }); + .then(resolve) + .catch(reject)); } /** * Gets an Oauth token used to edit the Wiki - * @param {String} endpoint Endpoint of where to get the token - * @param {String} auth Initial authorization data to get the better Oauth data */ -function getToken(endpoint, auth) { - return new Promise((resolve, reject) => { - fetch(endpoint, { +function getToken() { + return new Promise((resolve, reject) => + fetch(ENDPOINTS.token, { method: 'POST', - headers: { 'Authorization': auth, 'Content-Type': CONTENT_TYPE }, + headers: { 'Authorization': BASIC_AUTH, 'Content-Type': CONTENT_TYPE }, body: qs.stringify({ grant_type: 'password', username: REDDIT_USER, password: REDDIT_PASS }) }) .then((response) => response.json()) - .then((json) => json.access_token) - .then((token) => resolve(token)) - .catch((err) => reject(err)); - }); + .then(({ access_token }) => resolve(access_token)) + .catch(reject)); } /** * Post the contents from the README into the wiki - * @param {String} endpoint Endpoint for editing the wiki * @param {String} lastId Revision ID for the Wiki * @param {String} token Oauth token for authenticating with the Reddit API */ -function putWiki(endpoint, lastId, token) { - return new Promise((resolve, reject) => { - fetch(endpoint, { +function putWiki(lastId, token) { + return new Promise((resolve, reject) => + fetch(ENDPOINTS.edit, { method: 'POST', headers: { 'Authorization': `bearer ${token}`, 'Content-Type': CONTENT_TYPE }, body: qs.stringify({ @@ -97,11 +84,10 @@ function putWiki(endpoint, lastId, token) { }) .then((response) => response.json()) .then((json) => { - if (Object.keys(json).length == 0) resolve(); + if (Object.keys(json).length === 0) resolve(); else throw Error(json); }) - .catch((err) => reject(err)); - }); + .catch(reject)); } /** @@ -117,4 +103,5 @@ function fixContent(content) { content = content.replace(/-informational\?style=flat-square\)/g, '**'); return content; -} \ No newline at end of file +} // * If this is highlighted weirdly, it's because of the 'updated timestamp' regex, don't worry about it +//#endregion