Advertisement
Guest User

Untitled

a guest
Jan 7th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //module declarations
  2. const Snoowrap = require('snoowrap');
  3. const fs  = require("fs");
  4. const util = require('util')
  5.  
  6. //authentication variables, change as required
  7. const USER_AGENT =
  8. const CLIENT_ID =
  9. const CLIENT_SECRET =
  10. const REDDIT_USER =
  11. const REDDIT_PASS =
  12.  
  13. // this function reads names.txt and returns an array of the names, and removes duplicates
  14. function readFile() {
  15.     //replaces '\r' char with empty string. happens when you use windows to edit your text file...
  16.     var str = fs.readFileSync('names.txt').toString().replace(/(\r)/gm,"");
  17.     var arr = str.toString().split("\n");
  18.  
  19.     //remove duplicates
  20.     var hashSet = new Set();
  21.     for(var i=0;i<arr.length;i++) {
  22.         hashSet.add(arr[i]);
  23.     }
  24.    
  25.     //add back to array and return
  26.     arr = [];
  27.     const it = hashSet.entries();
  28.     for(let entry of it) {
  29.         arr.push(entry[0]);
  30.     }
  31.     return arr;
  32. }
  33.  
  34. // this function creates the an object array of {name: reddit name, text: custom flair, cssClass: surveyresponder2018}
  35. function convertArray(names) {
  36.     var arr = [];
  37.     for(var i=0;i<names.length;i++) {
  38.         arr.push({name: names[i], text: ':SR2018_1::SR2018_2::SR2018_3::SR2018_4::SR2018_5::SR2018_6:', cssClass: 'surveyresponder2018'});
  39.     }
  40.     return arr;
  41. }
  42.  
  43. // this function changes the flairs of specified users in the subreddit
  44. async function changeFlairs(nameArray) {
  45.     var isSuccessful = true;
  46.     var error = {};
  47.     //authenticate with reddit API
  48.     const r = new Snoowrap({
  49.         userAgent: USER_AGENT,
  50.         clientId: CLIENT_ID,
  51.         clientSecret: CLIENT_SECRET,
  52.         username: REDDIT_USER,
  53.         password: REDDIT_PASS
  54.     });
  55.  
  56.     //access subreddit
  57.     var sub = r.getSubreddit('SGExams');
  58.    
  59.     //change flairs according text file inputted and catch error
  60.     try {
  61.         await sub.setMultipleUserFlairs(nameArray);
  62.     } catch(e) {
  63.         error = e;
  64.         isSuccessful = false;
  65.     }
  66.     return {isSuccessful: isSuccessful, error: error};
  67. }
  68.  
  69. // this function handles the response given to the user,
  70. // including names of reddit accounts which were not changed successfully.
  71. function handleResponse(response, nameArray) {
  72.     if(response.isSuccessful) {
  73.         console.log("Flairs for all names were changed successfully :)\n");
  74.     } else {
  75.         var error = response.error;
  76.        
  77.         //save the error object into errorMessage.txt
  78.         fs.appendFile('errorMessage.txt', util.inspect(error, {showHidden: false, depth: null}), 'ascii', function(err, data) {
  79.             if(err) {
  80.                 console.log(err);
  81.             }
  82.         });
  83.        
  84.         //output and save the usernames which have failed
  85.         var failedNames = [];
  86.         for(var i=0;i<error.length;i++) {
  87.             let str = error[i].errors.user.name;
  88.             let firstPos = 24;
  89.             let lastPos = str.indexOf("', ignoring");
  90.             failedNames.push(str.slice(firstPos, lastPos));
  91.         }
  92.         console.log(nameArray.length - error.length + "/" + nameArray.length + " flairs changed successfully.\n");
  93.         console.log("Names of reddit users that were not changed successfully:");
  94.         for(var i=0;i<failedNames.length;i++) {
  95.             let failedName = failedNames[i] + '\r\n';
  96.             fs.appendFile('failedNames.txt', failedName, 'ascii', function(err, data) {
  97.                 if (err) {
  98.                     console.log(err);
  99.                 }
  100.             });
  101.             console.log(failedNames[i]);
  102.         }
  103.     }
  104.     console.log("Names of those which have failed are saved in failedNames.txt " +
  105.         "and the corresponding error message can be viewed in errorMessage.txt.\n" +
  106.         "Both files can be found in the same directory as the script.");
  107. }
  108.  
  109. async function main() {
  110.     var names = readFile();
  111.     var nameArray = convertArray(names);
  112.     var response = await changeFlairs(nameArray);
  113.     handleResponse(response, nameArray);
  114. }
  115.  
  116. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement