Permalink
Browse files

Fix url parser

  • Loading branch information...
1 parent 2cefbc1 commit e4dd9adec2d3c0c3a80a4887afd4f017b3109a5c @dz0ny dz0ny committed Sep 25, 2017
Showing with 15 additions and 23 deletions.
  1. +13 −21 scripts/url.coffee
  2. +2 −2 test/url.coffee
View
@@ -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
View
@@ -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 e4dd9ad

Please sign in to comment.