Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //module declarations
- const Snoowrap = require('snoowrap');
- const fs = require("fs");
- const util = require('util')
- //authentication variables, change as required
- const USER_AGENT =
- const CLIENT_ID =
- const CLIENT_SECRET =
- const REDDIT_USER =
- const REDDIT_PASS =
- // this function reads names.txt and returns an array of the names, and removes duplicates
- function readFile() {
- //replaces '\r' char with empty string. happens when you use windows to edit your text file...
- var str = fs.readFileSync('names.txt').toString().replace(/(\r)/gm,"");
- var arr = str.toString().split("\n");
- //remove duplicates
- var hashSet = new Set();
- for(var i=0;i<arr.length;i++) {
- hashSet.add(arr[i]);
- }
- //add back to array and return
- arr = [];
- const it = hashSet.entries();
- for(let entry of it) {
- arr.push(entry[0]);
- }
- return arr;
- }
- // this function creates the an object array of {name: reddit name, text: custom flair, cssClass: surveyresponder2018}
- function convertArray(names) {
- var arr = [];
- for(var i=0;i<names.length;i++) {
- arr.push({name: names[i], text: ':SR2018_1::SR2018_2::SR2018_3::SR2018_4::SR2018_5::SR2018_6:', cssClass: 'surveyresponder2018'});
- }
- return arr;
- }
- // this function changes the flairs of specified users in the subreddit
- async function changeFlairs(nameArray) {
- var isSuccessful = true;
- var error = {};
- //authenticate with reddit API
- const r = new Snoowrap({
- userAgent: USER_AGENT,
- clientId: CLIENT_ID,
- clientSecret: CLIENT_SECRET,
- username: REDDIT_USER,
- password: REDDIT_PASS
- });
- //access subreddit
- var sub = r.getSubreddit('SGExams');
- //change flairs according text file inputted and catch error
- try {
- await sub.setMultipleUserFlairs(nameArray);
- } catch(e) {
- error = e;
- isSuccessful = false;
- }
- return {isSuccessful: isSuccessful, error: error};
- }
- // this function handles the response given to the user,
- // including names of reddit accounts which were not changed successfully.
- function handleResponse(response, nameArray) {
- if(response.isSuccessful) {
- console.log("Flairs for all names were changed successfully :)\n");
- } else {
- var error = response.error;
- //save the error object into errorMessage.txt
- fs.appendFile('errorMessage.txt', util.inspect(error, {showHidden: false, depth: null}), 'ascii', function(err, data) {
- if(err) {
- console.log(err);
- }
- });
- //output and save the usernames which have failed
- var failedNames = [];
- for(var i=0;i<error.length;i++) {
- let str = error[i].errors.user.name;
- let firstPos = 24;
- let lastPos = str.indexOf("', ignoring");
- failedNames.push(str.slice(firstPos, lastPos));
- }
- console.log(nameArray.length - error.length + "/" + nameArray.length + " flairs changed successfully.\n");
- console.log("Names of reddit users that were not changed successfully:");
- for(var i=0;i<failedNames.length;i++) {
- let failedName = failedNames[i] + '\r\n';
- fs.appendFile('failedNames.txt', failedName, 'ascii', function(err, data) {
- if (err) {
- console.log(err);
- }
- });
- console.log(failedNames[i]);
- }
- }
- console.log("Names of those which have failed are saved in failedNames.txt " +
- "and the corresponding error message can be viewed in errorMessage.txt.\n" +
- "Both files can be found in the same directory as the script.");
- }
- async function main() {
- var names = readFile();
- var nameArray = convertArray(names);
- var response = await changeFlairs(nameArray);
- handleResponse(response, nameArray);
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement