Skip to content

Instantly share code, notes, and snippets.

@moomindani
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moomindani/b2891509347535ca85a1 to your computer and use it in GitHub Desktop.
Save moomindani/b2891509347535ca85a1 to your computer and use it in GitHub Desktop.
Utility commands for outputting url title
request = require 'request'
cheerio = require 'cheerio'
iconv = require 'iconv'
convertEncode = (body) ->
charset = body.toString('ascii').match /<meta[^>]*charset\s*=\s*["']?([-\w]+)["']?/i
return new iconv.Iconv(charset[1], 'UTF-8//TRANSLIT//IGNORE').convert(body) if charset
body
urlBlackList = {
'ignore hoge.com' : 'https://hoge.com',
'ignore hoge.co.jp' : 'https://hoge.co.jp'
}
module.exports = (robot) ->
robot.hear /(h?ttps?:\/\/[-\w@:%\+.~#?&\/=]+)/i, (msg)->
uri = msg.match[1]
for key, value of urlBlackList
if uri.indexOf(value) != -1
return
options = {
uri: uri,
followRedirect: true,
followAllRedirects: true,
encoding: null,
strictSSL: false
}
request options, (error, response, body)->
return if error
return if response.statusCode != 200
$ = cheerio.load convertEncode(body).toString().replace(/<!\[CDATA\[([^\]]+)]\]>/ig, "$1")
title = $("title").first()
if title
titleText = title.text().replace(/^[\s\n]+/, '').replace(/[\s\n]+$/, '')
msg.send "title: #{titleText}" if titleText != ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment