From 7d4a06f8aa637cd8b8cd4844e6d6ff736f5db9d0 Mon Sep 17 00:00:00 2001 From: tycrek Date: Tue, 2 Jun 2020 15:20:31 -0600 Subject: [PATCH] added test script for reddit api --- .github/workflows/build.yml | 7 +++++ package-lock.json | 10 +++++++ package.json | 2 ++ reddit_api_test.js | 54 +++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 reddit_api_test.js diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bbe6794..4303008 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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}} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index cc6a0e8..bdb770e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index c2a9eb8..04898e4 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/reddit_api_test.js b/reddit_api_test.js new file mode 100644 index 0000000..a07e3f2 --- /dev/null +++ b/reddit_api_test.js @@ -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)); +} \ No newline at end of file