Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created August 8, 2012 10:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mashiro/3294140 to your computer and use it in GitHub Desktop.
Save mashiro/3294140 to your computer and use it in GitHub Desktop.
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