This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

How do I combine the results to one line on KYoutube script

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
spithash
Master
Posts: 248
Joined: Thu Jul 12, 2007 9:21 am
Location: Libera
Contact:

How do I combine the results to one line on KYoutube script

Post by spithash »

So I got this script from the archive, it is really a cool script. I modified it a bit to fit my needs and it works great, but my question is if I can combine the results from all the 3 lines of output to 1 line and split the results if it has to, let's say for example after 440 chars.

As I've seen it's currently using this:

Code: Select all

set lines [split $data "\n"]
I hope I explain things the right way here :)

Code: Select all

# KYoutube.tcl v1.4
#
# Author: Outsider
# Evolutions : Krypte (krypte@europnet.org)
#
# Example :
# <Krypte> http://www.youtube.com/watch?v=s7L2PVdrb_8
# <KBot> [Youtube] Title: Game Of Thrones "Official" Show Open (HBO) | Duration: 1min 42sec | Views: 11021898
#
# You can also search a video using !youtube argument
#
# Change Log v1.4 :
# Add activation on chan !yact (on|off) enables or disables script for active channel (flags "mno" only)
# Improvement of the duration format (code from JazZ)
# Translation in English

setudef flag yact

package require http
#bind pub mno|mno !yact YouTube-Activate
#bind pubm - *http://*youtu*/watch* pubm:youtube
bind ctcp - * action:youtube
bind pub - !youtube pub:youtube
bind pub - !yt pub:youtube

# procedure for activate KYoutube.tcl on a channel

proc YouTube-Activate {nick host hand chan text} {

	if {![channel get $chan yact] && $text == "on"} {
		catch {channel set $chan +yact}
		putserv "notice $nick :YouTube: enabled for $chan"
		putlog "YouTube: script enabled (by $nick for $chan)"
	} elseif {[channel get $chan yact] && $text == "off"} {
		catch {channel set $chan -yact}
		putserv "notice $nick :YouTube: disabled for $chan"
		putlog "YouTube: script disabled (by $nick for $chan)"
	} else {
		putserv "notice $nick :YouTube: !yact (on|off) enables or disables script for active channel"
	}

}

# procedure for matching on action, e.g * Krypte is watching http://www.youtube.com/watch?v=s7L2PVdrb_8

proc action:youtube {nick host hand dest keyword args} {
        	if {[channel get $dest yact]} {
        set args [lindex $args 0]
        if {![validchan $dest]} {return}
        if {$keyword == "ACTION" && [string match *http://*youtu*/watch* $args]} {pubm:youtube $nick $host $hand $dest $args}
}
}

# main youtube procedure on text

proc pubm:youtube {nick host hand chan args} {
        if {[channel get $chan yact]} {
        set args [lindex $args 0]
        while {[regexp -- {(http:\/\/.*youtub.*/watch.*)} $args -> url args]} {
                while {[regexp -- {v=(.*)&?} $url -> vid url]} {
                        set vid [lindex [split $vid &] 0]

                        set gurl "http://gdata.youtube.com/feeds/api/videos/$vid"
                        set search [http::formatQuery v 2 alt jsonc prettyprint true]
                        set token [http::config -useragent "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11" -accept "*/*"]
                        set token [http::geturl "$gurl?$search"]
                        set data [::http::data $token]
                        http::cleanup $token
                        set lines [split $data "\n"]
                        set title ""
                        set duration ""
                        set viewCount ""
                        foreach line $lines {
                                if {$title==""} {set title [lindex [regexp -all -nocase -inline {\"title\"\: \"(.*)\"} $line] 1]}
                                if {$duration==""} {set duration [lindex [regexp -all -nocase -inline {\"duration\"\: ([0-9]+)} $line] 1]}
                                if {$viewCount==""} {set viewCount [lindex [regexp -all -nocase -inline {\"viewCount\"\: ([0-9]+)} $line] 1]}
                        }
                        set title [yturldehex $title]
                        putmsg $chan "\[Youtube\] Title: \002$title\002 | Duration: \002[shortduration $duration]\002 | Views: \002$viewCount\002"
                }
        }
}
}

# Search procedure, e.g !youtube game of thrones

proc pub:youtube {nick host hand chan args} {
        if {[channel get $chan yact]} {
        set args [lindex $args 0]
        if {$args == ""} {
                putnotc $nick "Command to search: \002!youtube <argument>\002"
                return
        }
        set search [http::formatQuery v 2 alt jsonc q $args orderby viewCount max-results 3 prettyprint true]
        set token [http::config -useragent "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11" -accept "*/*"]
        set token [http::geturl "http://gdata.youtube.com/feeds/api/videos?$search"]
        set data [::http::data $token]
        http::cleanup $token

        set totalitems [lindex [regexp -all -nocase -inline {\"totalItems\"\: ([0-9]+)} $data] 1]
#        putnotc $nick "\[Youtube Search\] There is $totalitems results for your search on \002$args\002"
        set lines [split $data "\n"]
        set results ""
        foreach line $lines {
                if {[regexp -all -nocase -inline {\"id\"\: \"(.*)\"} $line] != ""} {
                        set id [lindex [regexp -all -nocase -inline {\"id\"\: \"(.*)\"} $line] 1]
                        lappend results $id
                }
                if {[regexp -all -nocase -inline {\"title\"\: \"(.*)\"} $line] != ""} {
                        set result($id,title) [yturldehex [lindex [regexp -all -nocase -inline {\"title\"\: \"(.*)\"} $line] 1]]
                }
                if {[regexp -all -nocase -inline {\"duration\"\: ([0-9]+)} $line] != ""} {
                        set result($id,duration) [lindex [regexp -all -nocase -inline {\"duration\"\: ([0-9]+)} $line] 1]
                }
                if {[regexp -all -nocase -inline {\"viewCount\"\: ([0-9]+)} $line] != ""} {
                        set result($id,viewCount) [lindex [regexp -all -nocase -inline {\"viewCount\"\: ([0-9]+)} $line] 1]
                }
        }
        foreach res $results {
                putmsg $chan "\[Youtube\] Title: \002$result($res,title)\002 | Duration: \002[shortduration $result($res,duration)]\002 | Views: \002$result($res,viewCount)\002 | Link: \002http://www.youtube.com/watch?v=$res\002"
        }
}
}

proc yturldehex {string} {
        regsub -all {[\[\]]} $string "" string
        set string [subst [regsub -nocase -all {\&#([0-9]{2,4});} $string {[format %c \1]}]]
        return [string map {" \"} $string]
}

# Format the duration from seconds to x hours y mins z sec

proc shortduration {seconds} {
            set hours [expr {$seconds / 3600}]
            set minutes [expr {($seconds / 60) % 60}]
            set seconds [expr {$seconds % 60}]
            set res ""
 
            if {$hours != 0} {append res "$hours hrs "}            
            if {$minutes != 0} {append res "$minutes\min "}
            if {$seconds != 0} {append res "$seconds\sec"}
            return $res
}

putlog "KYoutube.tcl v1.4 Loaded."
Also, I'm not sure if the developer of this script will watch this thread, but, is is easy to fetch and parse in the video results, the current video upload date? Just exactly as it's parsing the video views for example, or the duration.

Thanks.
Libera ##rtlsdr & ##re - Nick: spithash
Click here for troll.tcl
Post Reply