Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
request = require('request')
cheerio = require('cheerio')
iconv = require('iconv-jp')
module.exports = (robot) ->
robot.hear /http(s?):\/\/(.*)/i, (msg) ->
url = msg.match[0]
unless url.match(/\.(png|jpg|jpeg|gif|txt|zip|tar\.bz|js|css)/) # filter out some common files from trying
request url: url, encoding: null, (error, response, body) ->
if 200 <= response.statusCode < 300
charset = response.headers['content-type']?.match(/charset=([\w\-]+)/)?[1]
charset = body.toString('binary').match(/charset=([\w\-]+)/)?[1] unless charset?
if charset?
converter = new iconv.Iconv(charset, 'UTF-8//IGNORE')
body = converter.convert(body).toString()
$ = cheerio.load(body)
title = $('title').text()
description = $('meta[name=description]').attr("content")
send = (s) ->
msg.send s.replace(/^\s+|\s+$|\r|\n/g, "") if s
send title
send description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment