mirror of https://github.com/tycrek/degoogle
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.6 KiB
JavaScript
105 lines
3.6 KiB
JavaScript
11 months ago
|
// Build tool for uploading generated README.md to Reddit Wiki
|
||
|
|
||
|
const qs = require('qs');
|
||
12 months ago
|
const fs = require('fs-extra');
|
||
|
const path = require('path');
|
||
|
const fetch = require('node-fetch');
|
||
|
const { DateTime } = require('luxon');
|
||
3 years ago
|
|
||
11 months ago
|
// 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',
|
||
|
PASS: process.env.REDDIT_PASS || 'password',
|
||
|
CLIENT_ID: process.env.REDDIT_CLIENT_ID || 'clientid',
|
||
|
CLIENT_SECRET: process.env.REDDIT_CLIENT_SECRET || 'clientsecret',
|
||
|
};
|
||
4 years ago
|
|
||
3 years ago
|
// Endpoints for each of our fetches to Reddit
|
||
|
const ENDPOINTS = {
|
||
11 months ago
|
revisions: `https://old.reddit.com/r/privacy/wiki/revisions/de-google.json`,
|
||
3 years ago
|
token: 'https://www.reddit.com/api/v1/access_token',
|
||
11 months ago
|
edit: `https://oauth.reddit.com/r/privacy/api/wiki/edit`
|
||
3 years ago
|
};
|
||
|
|
||
|
// Update the wiki
|
||
|
Promise.all([getLastRevision(), getToken()])
|
||
|
.then(([lastId, token]) => putWiki(lastId, token))
|
||
|
.catch((err) => (console.error(err), process.exit(1)));
|
||
4 years ago
|
|
||
4 years ago
|
/**
|
||
|
* Get the last revision ID on the Wiki. Required otherwise editing the wiki fails
|
||
|
*/
|
||
3 years ago
|
function getLastRevision() {
|
||
|
return new Promise((resolve, reject) =>
|
||
|
fetch(ENDPOINTS.revisions)
|
||
4 years ago
|
.then((response) => response.json())
|
||
|
.then((json) => json.data.children[0].id) // children[0] is the most recent edit
|
||
3 years ago
|
.then(resolve)
|
||
|
.catch(reject));
|
||
4 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Gets an Oauth token used to edit the Wiki
|
||
|
*/
|
||
3 years ago
|
function getToken() {
|
||
|
return new Promise((resolve, reject) =>
|
||
|
fetch(ENDPOINTS.token, {
|
||
4 years ago
|
method: 'POST',
|
||
11 months ago
|
headers: {
|
||
|
'Authorization': `Basic ${Buffer.from(`${REDDIT.CLIENT_ID}:${REDDIT.CLIENT_SECRET}`).toString('base64')}`,
|
||
|
'Content-Type': 'application/x-www-form-urlencoded'
|
||
|
},
|
||
|
body: qs.stringify({
|
||
|
grant_type: 'password',
|
||
|
username: REDDIT.USER,
|
||
|
password: REDDIT.PASS
|
||
|
})
|
||
4 years ago
|
})
|
||
|
.then((response) => response.json())
|
||
3 years ago
|
.then(({ access_token }) => resolve(access_token))
|
||
|
.catch(reject));
|
||
4 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* 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
|
||
|
*/
|
||
3 years ago
|
function putWiki(lastId, token) {
|
||
|
return new Promise((resolve, reject) =>
|
||
|
fetch(ENDPOINTS.edit, {
|
||
4 years ago
|
method: 'POST',
|
||
11 months ago
|
headers: {
|
||
|
'Authorization': `bearer ${token}`,
|
||
|
'Content-Type': 'application/x-www-form-urlencoded'
|
||
|
},
|
||
4 years ago
|
body: qs.stringify({
|
||
11 months ago
|
content: fixContent(fs.readFileSync(path.join(process.cwd(), 'README.md')).toString()),
|
||
|
page: 'de-google',
|
||
|
reason: 'Automated edit from GitHub repo: https://github.com/tycrek/degoogle',
|
||
4 years ago
|
previous: lastId
|
||
|
})
|
||
4 years ago
|
})
|
||
|
.then((response) => response.json())
|
||
|
.then((json) => {
|
||
3 years ago
|
if (Object.keys(json).length === 0) resolve();
|
||
4 years ago
|
else throw Error(json);
|
||
|
})
|
||
3 years ago
|
.catch(reject));
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
/**
|
||
|
* Fixes certain images to only show text on the Reddit wiki
|
||
|
* @param {String} content The content in README.md
|
||
|
*/
|
||
|
function fixContent(content) {
|
||
11 months ago
|
return content
|
||
4 years ago
|
|
||
11 months ago
|
// Fix updated timestamp
|
||
|
.replace(/\!\[Updated\](.*?)square\)/g, `#### Updated: ${DateTime.now().toFormat('MMMM dd, yyyy')}`)
|
||
4 years ago
|
|
||
11 months ago
|
// Fix published timestamps
|
||
|
.replace(/\!\[Published\]\(https\:\/\/img\.shields\.io\/badge\//g, '**')
|
||
|
.replace(/-informational\?style=flat-square\)/g, '**');
|
||
|
}
|