Youtube URL Checker & Info

By RaZ on Apr 02, 2011

11:06:52am / http://youtu.be/YIBo4yvLkfU
11:06:53am /
Raanjhanaa - Title Track ft. Dhanush & Sonam Kapoor - YouTube' ( 2 minutes 22 seconds ) Viewed 1.018.908
11:06:57am / http://www.youtube.com/watch?v=69HNy2r7f-M
11:06:58am /
Let's Battle Minecraft S4 #3 [Battlemap/HD/German] - Geht weg :( - YouTube' ( 16 minutes 28 seconds ) Viewed 5.372

package require http 2.4
bind PUBM - * mu
proc mu {nick uhost hand chan text} {
    set web(page) http://www.youtube.com
    set watch [regexp -nocase -- {\/watch\?v\=([^\s]{11})} $text youtubeid]
    if { $watch == 0 } {
        set watch [regexp -nocase -- {youtu\.be\/([^\s]{11})} $text a youtubeid]
        if { $watch == 0 } return
        set youtubeid "/watch?v=$youtubeid"
    }

    set logoo "\002\00301,00You\00300,04Tube\002\017"

    if {$watch && $youtubeid != ""} {
        putlog "$web(page)$youtubeid"
        set agent "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"     
        set t [::http::config -useragent $agent]
        set t [::http::geturl "$web(page)$youtubeid" -timeout 30000]
        set data [::http::data $t]
        ::http::cleanup $t

        set l [regexp -all -inline -- {<title>(.*?)</title>.*?<span class="watch-view-count " >(.*?)</span>} $data]

        regexp {"length_seconds": (\d+),} $data "" length

        foreach {black a b c d e} $l {

            set a [string map -nocase {\&\#39; \x27 &amp; \x26 &quot; \x22} $a]
            set b [string map [list \n ""] $b]
            set c [string map [list \n ""] $c]
            set d [string map [list \n ""] $d]
            set e [string map -nocase {\&\#39; \x27 &amp; \x26 &quot; \x22} $e]

            regsub -all {<.*?>} $a {} a
            regsub -all {<.*?>} $b {} b
            regsub -all {<.*?>} $c {} c
            regsub -all {<.*?>} $d {} d
            regsub -all {<.*?>} $e {} e         

            putserv "PRIVMSG $chan :$a' ( [duration $length] ) Viewed $b"

                proc duration {s} {
                variable etube
                set hours [expr {$s / 3600}]
                set minutes [expr {($s / 60) % 60}]
                set seconds [expr {$s % 60}]
                set res ""

                if {$hours != 0} {append res "$hours hours"}                
                if {$minutes != 0} {append res " $minutes minutes"}
                if {$seconds != 0} {append res " $seconds seconds"}
                return $res

            }   
        }
    }
}

Comments

Sign in to comment.
Ieatscrayons   -  May 19, 2014

I've modified fordlawnmower's script with bits from this one to include length and format it to a human readable format, here's the pastebin for anyone that's interested (thanks RaZ and Fordlawnmower!!)

http://pastebin.com/LR41DzFs(OLD SEE EDIT NOTE)

EDIT: I've updated it to clean the titles of videos, using code from both scripts again; v2 is here:

http://pastebin.com/sFvHJnNd

fred``  -  Aug 31, 2014

Thanks for for your new Version, but it seems like it misses a lot of YT-Links in Channels. For me it works only of ever 3rd/4th-Link.

Sign in to comment

Nashman   -  Jul 30, 2012

GJ, but it's not working for me :(
Tcl error [mu]: can't read "youtubeid": no such variable

It worked once .. "Surviving Alone in Alaska' ( 52 minutes 4 seconds ) Viewed 130.058"
then it gave me that error and now it's not working anymore.
Any tips?
Thanks in advance!

Edit: it seems to work now. I had another youtube script running at the same time so that mightve been the issue.
It's still giving me the " Tcl error [mu]: can't read "youtubeid": no such variable" error in the partyline tho.
Any way to fix this? :)

RaZ  -  Apr 25, 2013

edit

set watch [regexp -nocase -- {\/watch\?v\=([^\s]{11})} $text youtubeid]
    if { $watch == 0 } {
        set watch [regexp -nocase -- {youtu\.be\/([^\s]{11})} $text a youtubeid]
        set youtubeid "/watch?v=$youtubeid"
    }

To

set watch [regexp -nocase -- {\/watch\?v\=([^\s]{11})} $text youtubeid]
    if { $watch == 0 } {
        set watch [regexp -nocase -- {youtu\.be\/([^\s]{11})} $text a youtubeid]
        if { $watch == 0 } return
        set youtubeid "/watch?v=$youtubeid"
    }
Sign in to comment

abhi25   -  Sep 20, 2011

great work!!!! iRoc keep it up!! very nice!

 Respond  
RaZ   -  Jul 14, 2011

Update again

 Respond  
RaZ   -  Apr 26, 2011

Update RegExp....

 Respond  
Sorasyn   -  Apr 02, 2011

Ah, I thought it suspicious that 2 snippets of the same language with similar purposes were submitted. In any case nice work to the both of you.

 Respond  
Jethro   -  Apr 02, 2011

This is different from ford's except for the regex trigger part:

[regexp -all -nocase {(\/watch\?v=[\d\w]{11})} $text youtubeid]

Anyway, you both have done a good work fetching the info from youtube. ^^

 Respond  
Sorasyn   -  Apr 02, 2011

Did you just take FordLawnMower's snippet and add to it?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.