kulmapaikka-ircbot/plugins/getUrl.js
2019-08-13 19:37:22 +03:00

47 lines
1.1 KiB
JavaScript

var Promise = require('promise')
var request = require('request')
var requestPromise = function (url, urlArgs) {
var promise = new Promise(function (resolve, reject) {
request({
url: url,
qs: urlArgs,
json: false
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
resolve(body)
} else {
reject(error)
}
})
})
return promise
}
module.exports = function (config) {
// http://stackoverflow.com/questions/13087888/getting-the-page-title-from-a-scraped-webpage
var getTitle = function (url) {
var re = /(<\s*title[^>]*>((.|\n)+?)<\s*\/\s*title)>/gi
var urlArgs = {}
var promise = new Promise(function (resolve, reject) {
var urlPromise = requestPromise(url, urlArgs)
urlPromise.then(function (data) {
var match = re.exec(data)
if (match && match[2]) {
var title = match[2].trim() // remove whitespace
title = title.replace(/\r?\n|\r/g, '') // remove newlines
resolve(title)
} else {
reject(new Error())
}
})
})
return promise
}
return getTitle
}