This is a forum request asked by the member StonaJakey to have any posted URL shortened using Google's URL Shortener, along with the ability to display the URL leading to its website title. I thought it'd be useful to submit this code as a snippet and for those who find it for some related beneficial uses.
If you want to make the short URL clickable, parenthesize the whole regex match as such:
/(http:\/\/\S+)/
You must start a URL either with www. or http:// in a sentence. Please note that some websites may not return their titles.
A personal thanks to jaytea for the heads up on the regex.
Visit www.youtube.com **Title:** YouTube - Broadcast Yourself. **URL:** goo.gl/NlKL www.tvguide.com **Title:** TV Guide, TV Listings, Online Videos, Entertainment News and Celebrity News | TVGuide.com **URL:** goo.gl/EBFF Go to http://www.hawkee.com **Title:** Hawkee Technology Social Network **URL:** goo.gl/IM7Ti www.mirc.com **Title:** mIRC: Internet Relay Chat client **URL:** goo.gl/YwVB**-Edit-** Script updated to make the socket events regex, thanks to Ford's suggestion that it may cause a conflict with people who have similar socket names while having this script installed.
-Edit-Rearranged the output to have the website title displayed first before its URL.
on *:exit:$iif($exists(info),.remove info)
on $*:sockopen:/^goo$|^goo\.title$/:{
if $sockerr { msg %chash I'm experiencing a connection error! | halt }
if $sock(goo).name = goo {
tokenize 124 $sock(goo).mark
var %goo $+(url=http%3A%2F%2F,$2,&security_token)
$3 POST /api/shorten HTTP/1.0 | $3 Host: goo.gl
$3 Content-Type: application/x-www-form-urlencoded;charset=utf-8
$3 Connection: close
$3 Content-Length: $len(%goo)
$3 $crlf $+ %goo
}
else {
var %title sockwrite -nt goo.title
%title GET / HTTP/1.1
%title Host: $sock(goo.title).addr
%title Connection: close | %title
}
}
on $*:sockread:/^goo$|^goo\.title$/:{
if $sockerr { msg %chash I'm experiencing a reading error! | halt }
if $sock(goo).name = goo {
tokenize 124 $sock(goo).mark | sockread &goo
if $regex($bvar(&goo,1-).text,/http:\/\/(\S+)/) {
write info $addtok(%info,$+($chr(2),URL:,$chr(2)) $regml(1) $chr(160),94)
}
halt
}
sockread &title
if $bvar(&title,0) && $regex($bvar(&title,1-).text,/(?s)<title>(\s*\K.+?(?=\s*))<\/title>/i) {
var %t-- $iif($istok(object moved|error page|301 moved permanently,$regml(1),124),N/A,$regml(1))
write info $+($chr(2),Title:,$chr(2)) $replace($remove(%t--,amp;),®,$chr(174))
}
}
on $*:text:/((?i)(https?\72\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6}))/S:#:{
if !%tc {
inc -u4 %tc | set %chash # | write -c info
var %- $iif(http:// isin $regml(1),$remove($v2,$v1),$v2)
$iif($sock(goo),sockclose $v1) | sockopen goo goo.gl 80
sockmark goo $+(set $+(%,info),|,%-,|,sockwrite -nt goo,|,$&
$iif($regex($regml(1),/(w{3}\.|http:\/\/)?(\S+)\./),$remove($regml(2),www.)))
}
}
on $*:sockclose:/^goo$|^goo\.title$/:{
if $sock(goo).name = goo {
tokenize 124 $sock(goo).mark
if $sock(goo.title) { sockclose $v1 }
sockopen goo.title $+(www.,$4,.com) 80
sockmark goo.title .msg %chash
}
else {
tokenize 94 %info
$sock(goo.title).mark $1 $+($read(info,2),$str($chr(160),2),$read(info,1))
unset %chash
}
}
Hey Jethro,
Is there a way to add a !snipr into this script? to bring back a link that would define http://snipr.me/489T3 (for short URL) ? instead of having http:// or www. ? thanks for your time :)
Ok, I've updated the code to catch only the section for www.sitename.com or http://www.sitename.com. Everything should work correctly now. So, say, if someone posts a link like this in a channel:> http://www.newegg.com/Product/Product.aspx?Item=079-0001-000T2The script should read the bit for http://www.newegg.com and returns its title.
The error occurred on account of the sub-path existed in the URL.
i know why the script isnt working for me... you need to update it :D its something with goole.. please fix the: HTTP part i dont know how sorry D:
<~chachin> http://www.newegg.com/Product/Product.aspx?Item=079-0001-000T2
I'm experiencing a connection error!chachin, use this one here and see how it goes for you: http://pastebin.com/Rqxw96er
http://kpaste.net/2c829 here's the script. you see i only modded the line that needed to be modded to make the links clickable. line 24 should be it. its highlighted with @@ so you know i did it the way you want. oh now the bot says this every time on any and every link:
[9:01:53pm] <~chachin> http://kpaste.net/2c829
[9:01:56pm] <@chicken> I'm experiencing a connection error!
How come you have two URL outputs? Did you have the original script loaded in another remote somewhere?
I updated the script on Sep. 2nd to have the output rearranged. The title of a website will come out first before its URL.
Check your mIRC remote and make sure you have the old one removed, and then copy and paste the script again.
i keep getting this error i dont know if i didnt load it right or wtf i dont have other url checkers or title scripts only this one.
[6:17:23pm] <@chachin> www.youtube.com
[6:17:24pm] <+chicken> URL: goo.gl/NJwy Title: URL: goo.gl/NJwy ^URL: goo.gl/NlKL
Jethro: see its not suppose to be like that its suppose to look like your example.
chanchin, what error are you referring to?
If you want to make the shortened URL clickable, I've mentioned this matter in my description, and here is the re-post:
If you want to make the short URL clickable, parenthesize the whole regex match as such:
/(http:\/\/\S+)/
I'm sure you can do the adding in my script; if you can't, let me know.
hmm I don't know if this is a problem But I'm getting an error of some kind. as you see it posts tiny url 3 times. and i cant find the line to make it clickable :( I still give it a 7/10 :)
[3:49:19pm] <~chachin> http://www.iheart.com/#/live/2281/
[3:49:20pm] URL: goo.gl/NJwy Title: Real & Custom Radio Stations | Listen Free Online | iHeartRadio URL: goo.gl/NJwy ^URL: goo.gl/tC3f2
had to delete it to many errors