added test script for reddit api

This commit is contained in:
tycrek 2020-06-02 15:20:31 -06:00
parent 346ce3a4a3
commit 7d4a06f8aa
4 changed files with 73 additions and 0 deletions

View File

@ -30,3 +30,10 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: master
- name: Wiki
run: node _wiki.js
env:
REDDIT_USER: ${{ secrets.REDDIT_USER}}
REDDIT_PASS: ${{ secrets.REDDIT_PASS}}
REDDIT_CLIENT_ID: ${{ secrets.REDDIT_CLIENT_ID}}
REDDIT_CLIENT_SECRET: ${{ secrets.REDDIT_CLIENT_SECRET}}

10
package-lock.json generated
View File

@ -39,6 +39,16 @@
"resolved": "https://registry.npmjs.org/moment/-/moment-2.26.0.tgz",
"integrity": "sha512-oIixUO+OamkUkwjhAVE18rAMfRJNsNe/Stid/gwHSOfHrOtw9EhAY2AHvdKZ/k/MggcYELFCJz/Sn2pL8b8JMw=="
},
"node-fetch": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
"integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA=="
},
"qs": {
"version": "6.9.4",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.9.4.tgz",
"integrity": "sha512-A1kFqHekCTM7cz0udomYUoYNWjBebHm/5wzU/XqrBRBNWectVH0QIiN+NEcZ0Dte5hvzHwbr8+XQmguPhJ6WdQ=="
},
"universalify": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz",

View File

@ -19,6 +19,8 @@
"dependencies": {
"fs-extra": "^9.0.0",
"moment": "^2.26.0",
"node-fetch": "^2.6.0",
"qs": "^6.9.4",
"yaml": "^1.10.0"
}
}

54
reddit_api_test.js Normal file
View File

@ -0,0 +1,54 @@
const fs = require('fs-extra');
const path = require('path');
const fetch = require('node-fetch');
const qs = require('qs');
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';
const WIKI_SUBREDDIT = 'privacy';
const WIKI_PAGE = 'de-go-git';
const WIKI_REASON = 'Automated edit from [GitHub repo](https://github.com/tycrek/degoogle)';
const CONTENT_TYPE = 'application/x-www-form-urlencoded';
updateWiki()
function updateWiki() {
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`
};
let basicAuth = `Basic ${Buffer.from(REDDIT_CLIENT_ID + ':' + REDDIT_CLIENT_SECRET).toString('base64')}`;
let lastId;
fetch(endpoints.revisions)
.then((response) => response.json())
.then((json) => json.data.children[0].id)
.then((mLastId) => {
lastId = mLastId;
return fetch(endpoints.token, {
method: 'POST',
headers: { 'Authorization': basicAuth, 'Content-Type': CONTENT_TYPE },
body: qs.stringify({ grant_type: 'password', username: REDDIT_USER, password: REDDIT_PASS })
});
})
.then((response) => response.json())
.then((json) => {
return fetch(endpoints.edit, {
method: 'POST',
headers: { 'Authorization': `bearer ${json.access_token}`, 'Content-Type': CONTENT_TYPE },
body: qs.stringify({
content: fs.readFileSync(path.join(__dirname, 'README.md')).toString(),
page: WIKI_PAGE,
reason: WIKI_REASON,
previous: lastId
})
});
})
.then((response) => response.json())
.then((json) => console.log(json))
.catch((err) => console.error(err));
}