Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Discord = require("discord.js");
- var express = require("express");
- var fs = require("fs");
- var request = require("request");
- var cheerio = require("cheerio");
- var app = express();
- var bot = new Discord.Client();
- // When bot is launched
- bot.on("ready", function()
- {
- console.log("CollegeStatsBot Online and Ready!");
- console.log("Number of Servers CollegeStatsBot is On: " + bot.servers.length);
- });
- // When bot receieves a message
- bot.on("message", function(message)
- {
- var input = message.content;
- // Command +help results in bot explaining how +stats works
- if (input === "+help")
- {
- 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.");
- }
- // Command +stats <insert college> results in bot sending a link to search results and basic stats of the college
- else if (input.startsWith("+stats "))
- {
- // Changing the message to just contain the college name
- var college = input.replace("+stats ", "");
- college = college.replace(/ /g, "+");
- // Search result url
- var url = "http://colleges.usnews.rankingsandreviews.com/best-colleges/search?name=" + college + "&state=";
- // Web scraping search page for the stats using the url created
- request(url, function(err, resp, body)
- {
- if (err)
- throw err;
- // Loads web page
- $ = cheerio.load(body);
- var name, ranking, tuition, enrollment, acceptance;
- var json = {name : "", ranking : "", tuition : "", enrollment : "", acceptance : ""};
- // Getting the name
- $('.collegename').filter(function()
- {
- var data = $(this);
- name = data.text();
- json.name = name;
- });
- // Getting the ranking
- $('.search-mini-badge').filter(function()
- {
- var data = $(this);
- ranking = data.children().first().text();
- json.ranking = ranking;
- });
- // Getting the tuition
- $('.tuition').filter(function()
- {
- var data = $(this);
- tuition = data.children().first().text();
- 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+", "");
- 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+", "");
- tuition = tuition.replace(/^\s*[\r\n]/gm, '');
- json.tuition = tuition;
- });
- // Getting the undergraduate enrollment
- $('.enrollment').filter(function()
- {
- var data = $(this);
- enrollment = data.text();
- enrollment = enrollment.replace("0\n1,750\n3,500\n5,250\n7,000\n8,750\n10,500\n12,250\n14,000+", "");
- enrollment = enrollment.replace("0\n1,750\n3,500\n5,250\n7,000\n8,750\n10,500\n12,250\n14,000+\n014,000+");
- enrollment = enrollment.replace(/^\s*[\r\n]/gm, '');
- json.enrollment = enrollment;
- });
- // Getting the acceptance rate
- $('.acceptance').filter(function()
- {
- var data = $(this);
- acceptance = data.text();
- acceptance = acceptance.replace("<10%\n20%\n30%\n40%\n50%\n60%\n70%\n80%\n90%+");
- acceptance = acceptance.replace("<10%\n20%\n30%\n40%\n50%\n60%\n70%\n80%\n90%+\n<10%90%+");
- acceptance = acceptance.replace(/^\s*[\r\n]/gm, '');
- json.acceptance = acceptance;
- // Bot sends final message with the url and stats
- // bot.sendMessage(message, url + '\nName: ' + name + '\nRanking: ' + ranking + '\nTuition: ' + tuition + '\nUndergrad Enrollment: ' + enrollment + '\nAcceptance Rate: ' + acceptance);
- });
- bot.sendMessage(message, url + '\nName: ' + json.name + '\nRanking: ' + json.ranking + '\nTuition: ' + json.tuition + '\nUndergrad Enrollment: ' + json.enrollment + '\nAcceptance Rate: ' + json.acceptance);
- });
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement