Advertisement
Guest User

Untitled

a guest
Oct 16th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. const util = require('util');
  2. var res = require("res");
  3. var rawjs = require("raw.js");
  4. var reddit = new rawjs("raw.js example script");
  5. var http = require("http");
  6. const snoowrap = require('snoowrap');
  7. var r = new snoowrap({
  8. userAgent: 'script',
  9. clientId: 'xxx',
  10. clientSecret: 'xxx',
  11. username: 'xxx',
  12. password: 'xxx'
  13. });
  14.  
  15. //HTTP SERVER
  16. http.createServer(function(request, response) {
  17. console.log("I got kicked");
  18. response.writeHeader(200, {
  19. "Content-Type": "text/plain"
  20. });
  21. response.end();
  22. }).listen(8080);
  23. console.log("Server Running on 8080");
  24.  
  25. //OAUTH2 Setup
  26. reddit.setupOAuth2("xxx", "xxx");
  27.  
  28. //global variables and objects
  29. var LastPostRun = {
  30. name: ''
  31. };
  32.  
  33. function criticalError(message) {
  34. console.log(message);
  35. return process.exit();
  36. };
  37.  
  38.  
  39. //GET LATEST POSTS
  40. function newPosts(sub) {
  41. reddit.new({
  42. r: sub,
  43. limit: 1
  44. }, function(err, res) {
  45. //error handling
  46. if (err || res.children == null || res.children.length == 0) return CriticalError('Unable to scan for posts in /r/' + sub + ' . Error: ' + err);
  47. //set the latest post's unique name to global variable
  48. LastPostRun.name = res.children[0].data.name;
  49.  
  50. //checking if a new post has been made
  51. if (res.children[0].data.name != LastPostRun.name) {
  52. console.log('new post has been made');
  53. grabSubmissionCount(res.children[0].data.author, sub);
  54. }
  55. });
  56. }
  57.  
  58. ///COUNT NUMBER OF SUBMISSIONS
  59. function grabSubmissionCount(author, sub) {
  60. reddit.userLinks({
  61. user: author,
  62. limit: 100
  63. }, function(err, res) {
  64. if (err || res.children == null || res.children.length == 0 || res.children[] == null) return CriticalError('Unable to scan for posts in /r/' + sub + ' . Error: ' + err);
  65. var postCount;
  66. for (x = 0; x < res.children.length; x++) {
  67. if (res.children[x].data.subreddit === sub) {
  68. postCount++;
  69. }
  70. }
  71. addFlair(author, sub, postCount);
  72. });
  73. }
  74.  
  75. //add flair -- snoowrap
  76. function addFlair(user, sub, postCount) {
  77. if (postCount >= 3 && postCount < 25) {
  78. r.getSubreddit(sub).setMultipleUserFlairs([{
  79. name: user,
  80. text: 'Novice',
  81. cssClass: 'novice'
  82. },
  83. ]);
  84. console.log('asdded');
  85. LastPostRun.name = res.children[0].data.name;
  86. } else {
  87. console.log('no flair assigned');
  88. }
  89. }
  90.  
  91. //REDDIT AUTHORIZATION
  92. reddit.auth({
  93. "username": "xxx",
  94. "password": "xxx"
  95. }, function(err, response) {
  96. if (err) return CriticalError('Unable to scan for posts in /r/' + subverse + ' . Error: ' + err);
  97. else {
  98. console.log('Authenticated');
  99. newPosts('BannedFromCoC');
  100. }
  101. });
  102.  
  103. // Either as one line
  104. if (err || res.children == null || res.children.length == 0 || res.children[] == null) return CriticalError('Unable to scan for posts in /r/' + subverse + ' . Error: ' + err);
  105.  
  106. // Or as two
  107. if (err || res.children == null || res.children.length == 0)
  108. return CriticalError('Unable to scan for posts in /r/' + subverse + ' . Error: ' + err);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement