Skip to content

Commit

Permalink
Fix url parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dz0ny committed Sep 25, 2017
1 parent 2cefbc1 commit 15669d6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
34 changes: 13 additions & 21 deletions scripts/url.coffee
@@ -1,35 +1,27 @@
module.exports = (bot) ->

is_url = /(https?[\w:\/\.;&\?_\-\#=]+)/g;

resolve = (url, cb)->
if is_url.test url
url = url.match(is_url)[0]
bot.fetchHTML url, ($)->
if $
stack = []
stack.push $("title").eq(0).text().replace(/\s\s+/g, "").replace(/ /g,"").replace(/\n/g, "")
opis = $("meta[name=description]").attr("content")
if opis
stack.push opis.replace(/\s\s+/g, "").replace(/ /g,"").replace(/\n/g, "")
cb stack.join("\n")
bot.fetchHTML url, ($)->
if $
stack = []
stack.push $("title").eq(0).text().replace(/\s\s+/g, "").replace(/ /g,"").replace(/\n/g, "")
opis = $("meta[name=description]").attr("content")
if opis
stack.push opis.replace(/\s\s+/g, "").replace(/ /g,"").replace(/\n/g, "")
cb stack.join("\n")

bot.on 'user:talk', (r) ->
if is_url.test r.text
is_url = /(?:.+)?(https?:\/\/[\S]+)(?:.+)?/ig;
url = is_url.exec(r.text)
if url
msg = "#{r.text} @#{moment().toString()}"
redis.rpush("irc:zgodovina:#{r.nick}", msg)
resolve r.text, r.reply
resolve url[1], r.reply

bot.regexp /^\.url (.+)/i,
".url <nick> Prikaži urlje, ki jih je objavil <nick>"
(match, r) ->
nick = match[1]
redis.lrange("irc:zgodovina:#{nick}", -5, 5).then (data)->
for msg in data
r.reply msg

bot.regexp /^\.nalozi (.+)/i,
".nalozi <url> Prikaži opis in naslov za <url>"
(match, r) ->
key = match[1]
resolve key, r.reply
r.reply msg
4 changes: 2 additions & 2 deletions test/url.coffee
Expand Up @@ -14,7 +14,7 @@ describe 'url.coffee', ->
done()

it 'test .nalozi veljaven url', (done)->
bot.test ".nalozi https://www.google.si/", (msg)->
bot.test "hudo https://www.ubuntu.si/", (msg)->
expect(msg).to.be.a('string')
expect(msg.length).to.be.at.least 2
done()
done()

0 comments on commit 15669d6

Please sign in to comment.