Guest User

Untitled

a guest
Jul 13th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var request = require("request");
  2. var tough = require('tough-cookie');
  3. var Cookie = tough.Cookie;
  4. var FileCookieStore = require("tough-cookie-store");
  5. var fs = require("fs");
  6. var cheerio = require("cheerio");
  7. var mongojs = require("mongojs");
  8. var db = mongojs("reddit");
  9.  
  10.  
  11. function Reddit(user, pass) {
  12.     var me = this;
  13.     this.user = user;
  14.     this.pass = pass;
  15.     if (!this.user) this.user = "none";
  16.    
  17.     me.cookieFile = new FileCookieStore("./cookies/" + me.user + ".json", {encrypt: false});
  18.     var jar = request.jar(me.cookieFile);
  19.     me.jar = jar;
  20.     this.request = request.defaults({
  21.         jar : me.jar,
  22.         headers: {
  23.             "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
  24.         }
  25.     });
  26. }
  27.  
  28.  
  29. Reddit.prototype.login = function(cb) {
  30.     var me = this;
  31.     this.request.post({
  32.         url: "https://www.reddit.com/api/login/" + me.user,
  33.         headers: {
  34.             "x-requested-with": "XMLHttpRequest",
  35.             "referer": "https://www.reddit.com/"
  36.         },
  37.         form: {
  38.             op: "login-main",
  39.             user: me.user,
  40.             passwd: me.pass,
  41.             rem: "on",
  42.             api_type: "json"
  43.         }
  44.     }, function(err, res, body) {
  45.         me.cookieFile.flush();
  46.         cb();
  47.     });
  48. };
  49.  
  50. Reddit.prototype.getHome = function(cb) {
  51.     var me = this;
  52.     this.request("https://www.reddit.com/", function(err, res, body) {
  53.         fs.writeFileSync("scratch.html", body);
  54.     });
  55. }
  56.  
  57. Reddit.prototype.getSubReddits = function(pages, cb) {
  58.     var me = this;
  59.    
  60.     var subs = [];
  61.    
  62.     function getPage(link) {
  63.         me.request(link, function(err, res, body) {
  64.             var $ = cheerio.load(body);
  65.             $("div.subreddit").each(function(i, el) {
  66.                 var sub = {
  67.                     _id: $(el).find(".titlerow a").text().split(":")[0],
  68.                     subscribed: null,
  69.                     title: $(el).find(".titlerow a").text(),
  70.                     url: $(el).find(".titlerow a").attr("href"),
  71.                     text: $(el).find(".usertext-body").text().trim(),
  72.                     subs: Number($(el).find(".score.unvoted .number").eq(0).text().replace(/,/g, ""))
  73.                 };
  74.                 if ($(el).find("a").eq(0).text() === "subscribe") {
  75.                     sub.subscribed = false;
  76.                 } else {
  77.                     sub.subscribed = true;
  78.                 }
  79.                 db.collection("subreddits").update({_id: sub._id}, {$set: sub}, {upsert: true});
  80.                 subs.push(sub);
  81.             });
  82.            
  83.             pages--;
  84.             if (pages === 0) {
  85.                 cb(subs);
  86.             } else {
  87.                 var next = $(".next-button a").attr("href");
  88.                 getPage(next);
  89.             }
  90.            
  91.         });
  92.     }
  93.    
  94.     getPage("https://www.reddit.com/subreddits/");
  95. };
  96.  
  97. Reddit.prototype.getSubReddit = function(id, cb) {
  98.     var ret = new SubReddit(id);
  99.     ret.request = this.request;
  100.     return ret;
  101. }
  102.  
  103. function SubReddit(id) {
  104.     var me = this;
  105.     me._id = id;
  106.     me.url = "https://www.reddit.com/" + me._id;
  107.     me.nextUrl = "";
  108.     me.posts = [];
  109. }
  110.  
  111. SubReddit.prototype.getTopYear = function(pages, cb) {
  112.     var me = this;
  113.     me.getPosts(me.url + "/top/?sort=top&t=year", pages, cb);
  114. };
  115.  
  116. SubReddit.prototype.getPosts = function(url, pages, cb) {
  117.     var me = this;
  118.     me.request(url, function(err, res, body) {
  119.         var $ = cheerio.load(body);
  120.         $("div.thing.link").each(function(i, el) {
  121.             var post = {
  122.                 _id: $(el).attr("data-fullname"),
  123.                 sub: me._id,
  124.                 title: $(el).find("a.title").text().trim(),
  125.                 link: $(el).find("a.title").attr("href"),
  126.                 score: Number($(el).find(".score.likes").text()),
  127.                 comments: Number($(el).find(".bylink.comments").text().split(" ")[0]),
  128.                 thumbnail: $(el).find("a.thumbnail img").attr("src"),
  129.                 posted: new Date($(el).find("time").attr("title"))
  130.             };
  131.  
  132.             me.posts.push(post);
  133.            
  134.         });
  135.         pages--;
  136.         if (pages === 0) {
  137.             cb();
  138.         } else {
  139.             var nextUrl = $(".next-button a").attr("href");
  140.             me.getPosts(nextUrl, pages, cb);
  141.         }
  142.     });
  143. };
  144.  
  145. function getPosts(req, url, cb) {
  146.     var ret = {
  147.         nextUrl: null,
  148.         posts: []
  149.     };
  150.     req(url, function(err, res, body) {
  151.         var $ = cheerio.load(body);
  152.        
  153.         $("div.thing.link").each(function(i, el) {
  154.             var post = {
  155.                 title: $(el).find("a.title").text().trim(),
  156.                 link: $(el).find("a.title").attr("href"),
  157.                 score: Number($(el).find(".score.likes").text()),
  158.                 comments: Number($(el).find(".bylink.comments").text().split(" ")[0]),
  159.                 thumbnail: $(el).find("a.thumbnail img").attr("src"),
  160.                 posted: new Date($(el).find("time").attr("title"))
  161.             };
  162.            
  163.             ret.posts.push(post);
  164.         });
  165.         ret.nextUrl = $(".next-button a").attr("href");
  166.         cb(ret);
  167.     });
  168. }
  169.  
  170.  
  171. module.exports = function(user, pass) {
  172.     return new Reddit(user, pass);
  173. };
Add Comment
Please, Sign In to add comment