Last active
August 29, 2015 14:06
-
-
Save moomindani/b2891509347535ca85a1 to your computer and use it in GitHub Desktop.
Utility commands for outputting url title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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