Skip to content

Instantly share code, notes, and snippets.

@rickycodes
Last active December 11, 2015 05:29
Show Gist options
  • Save rickycodes/4552795 to your computer and use it in GitHub Desktop.
Save rickycodes/4552795 to your computer and use it in GitHub Desktop.
desc ircjsbot plugin
/**
* @module desc
* This module fetches titles + descriptions for sites shared in a channel IF the message is simply an URL
* This module is also an example of how to use cheerio to `parse` html.
* Dependencies not included in ircjsbot: cheerio + request (request because it follows redirects, which the entire internet is build on, apparently)
*/
"use strict";
const irc = require("irc-js");
const cheerio = require("cheerio");
const request = require('request');
const shared = require("./shared");
function say(msg, reply) {
if(reply !== undefined) {
msg.reply("%s, %s", msg.from.nick, shared.unescape(reply));
}
}
function getDesc(msg, query) {
const url = msg.params[1].replace(/:/,'');
request( {
headers : { "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17" },
url : url
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(body);
const desc = $( 'meta[name="description"]' ).attr( 'content' ) || $( 'meta[property="og:description"]' );
const title = $( 'title' ).text();
const toSay = ( desc.length ) ? title + ' → ' + desc : title;
say(msg, toSay);
}
});
}
function load(bot) {
bot.match(/^:(?:[a-z]+:\/\/)?[0-9a-z](?:[\d\w\-\.]+\.)+[a-z]{2,4}(?:\:[0-9]{1,6})?(?:[\d\w\-\.\/\?=&#%+]*$)/i, getDesc);
return irc.STATUS.SUCCESS;
}
function unload() {
return irc.STATUS.SUCCESS;
}
exports.name = "description";
exports.load = load;
exports.unload = unload;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment