Advertisement
45b16

CollegeStatsBot

Aug 13th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Discord = require("discord.js");
  2.  
  3. var express = require("express");
  4. var fs = require("fs");
  5. var request = require("request");
  6. var cheerio = require("cheerio");
  7. var app = express();
  8.  
  9. var bot = new Discord.Client();
  10.  
  11. // When bot is launched
  12. bot.on("ready", function()
  13. {
  14.     console.log("CollegeStatsBot Online and Ready!");
  15.     console.log("Number of Servers CollegeStatsBot is On: " + bot.servers.length);
  16. });
  17.  
  18. // When bot receieves a message
  19. bot.on("message", function(message)
  20. {
  21.     var input = message.content;
  22.  
  23.     // Command +help results in bot explaining how +stats works
  24.     if (input === "+help")
  25.     {
  26.         bot.sendMessage(message, "Just type '+stats <name of university>' without the quotes to get a link to its statistics page. For example, '+stats princeton' will return a link to Princeton's statistics.");
  27.     }
  28.     // Command +stats <insert college> results in bot sending a link to search results and basic stats of the college
  29.     else if (input.startsWith("+stats "))
  30.     {
  31.         // Changing the message to just contain the college name
  32.         var college = input.replace("+stats ", "");
  33.         college = college.replace(/ /g, "+");
  34.  
  35.         // Search result url
  36.         var url = "http://colleges.usnews.rankingsandreviews.com/best-colleges/search?name=" + college + "&state=";
  37.  
  38.         // Web scraping search page for the stats using the url created
  39.         request(url, function(err, resp, body)
  40.         {
  41.             if (err)
  42.                 throw err;
  43.  
  44.             // Loads web page
  45.             $ = cheerio.load(body);
  46.  
  47.             var name, ranking, tuition, enrollment, acceptance;
  48.             var json = {name : "", ranking : "", tuition : "", enrollment : "", acceptance : ""};
  49.  
  50.             // Getting the name
  51.             $('.collegename').filter(function()
  52.             {
  53.                 var data = $(this);
  54.                 name = data.text();
  55.  
  56.                 json.name = name;
  57.             });
  58.  
  59.             // Getting the ranking
  60.             $('.search-mini-badge').filter(function()
  61.             {
  62.                 var data = $(this);
  63.                 ranking = data.children().first().text();
  64.  
  65.                 json.ranking = ranking;
  66.             });
  67.  
  68.             // Getting the tuition
  69.             $('.tuition').filter(function()
  70.             {
  71.                 var data = $(this);
  72.                 tuition = data.children().first().text();
  73.  
  74.                 tuition = tuition.replace("<$5,000\n$10,625\n$16,250\n$21,875\n$27,500\n$33,125\n$38,750\n$44,375\n$50,000+", "");
  75.                 tuition = tuition.replace("<$5,000\n$10,625\n$16,250\n$21,875\n$27,500\n$33,125\n$38,750\n$44,375\n$50,000+", "");
  76.                 tuition = tuition.replace(/^\s*[\r\n]/gm, '');
  77.  
  78.                 json.tuition = tuition;
  79.             });
  80.  
  81.             // Getting the undergraduate enrollment
  82.             $('.enrollment').filter(function()
  83.             {
  84.                 var data = $(this);
  85.                 enrollment = data.text();
  86.  
  87.                 enrollment = enrollment.replace("0\n1,750\n3,500\n5,250\n7,000\n8,750\n10,500\n12,250\n14,000+", "");
  88.                 enrollment = enrollment.replace("0\n1,750\n3,500\n5,250\n7,000\n8,750\n10,500\n12,250\n14,000+\n014,000+");
  89.                 enrollment = enrollment.replace(/^\s*[\r\n]/gm, '');
  90.  
  91.                 json.enrollment = enrollment;
  92.             });
  93.  
  94.             // Getting the acceptance rate
  95.             $('.acceptance').filter(function()
  96.             {
  97.                 var data = $(this);
  98.                 acceptance = data.text();
  99.  
  100.                 acceptance = acceptance.replace("<10%\n20%\n30%\n40%\n50%\n60%\n70%\n80%\n90%+");
  101.                 acceptance = acceptance.replace("<10%\n20%\n30%\n40%\n50%\n60%\n70%\n80%\n90%+\n<10%90%+");
  102.                 acceptance = acceptance.replace(/^\s*[\r\n]/gm, '');
  103.  
  104.                 json.acceptance = acceptance;
  105.  
  106.                 // Bot sends final message with the url and stats
  107.                 // bot.sendMessage(message, url + '\nName: ' + name + '\nRanking: ' + ranking + '\nTuition: ' + tuition + '\nUndergrad Enrollment: ' + enrollment + '\nAcceptance Rate: ' + acceptance);
  108.             });
  109.  
  110.             bot.sendMessage(message, url + '\nName: ' + json.name + '\nRanking: ' + json.ranking + '\nTuition: ' + json.tuition + '\nUndergrad Enrollment: ' + json.enrollment + '\nAcceptance Rate: ' + json.acceptance);
  111.         });
  112.     }
  113. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement