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.

[YouTitle] tcl error + small changes

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
N
Nash
Voice
Posts: 6
Joined: Sun Jul 29, 2012 7:14 pm

[YouTitle] tcl error + small changes

Post by Nash »

Greetings :)

I've found this youtube script and it's working pretty well so far:

Code: Select all

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]
		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 -- {<meta name="title" content="(.*?)">.*?<span class="watch-view-count">.*?<strong>(.*?)</strong>} $data]
 
		regexp {"length_seconds": (\d+),} $data "" length
 
		foreach {black a b c d e} $l {
 
			set a [string map -nocase {\&\#39; \x27 & \x26 " \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 & \x26 " \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 :YouTube: $a ( [duration $length] ) Viewed $b"
			putserv "PRIVMSG $chan :YouTube: $a - Duration: [duration $length] - Views: $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
			}	
		}
	}
}
 
putlog "_____ YouTitle loaded..."
However, when it's loaded it gives me this error message in the partyline:
"Tcl error [mu]: can't read "youtubeid": no such variable"
The script works so far, but i would like to get rid off this error message.
EDIT: it appears that this error msg comes up everytime someone writes something in the channel. :shock:

Also, currently the script displays the youtube info like this:
YouTube: Bill Hicks salvation (FULL) - Duration: 1 hour 54 minutes 36 seconds - Views: 34.405

1.) Would it be possible to change the format of the duration to 1:54:36 or something simliar?
2.) Would it be possible to add likes /dislikes / author to the script?


Disclaimer: i've absolutely zero scripting knowledge so adding this by myself is practially impossible.
All i can do really is change the appearance :oops:


Anyways, i'm highly grateful for every help i can get!!
Thanks in advance! :)
M
McGuyver
Voice
Posts: 14
Joined: Wed Aug 31, 2011 1:27 pm

Post by McGuyver »

I suggest adding

Code: Select all

set youtubeid ""
right under the "proc mu" line
N
Nash
Voice
Posts: 6
Joined: Sun Jul 29, 2012 7:14 pm

Post by Nash »

Hey McGuyver, thanks for the tip! It worked! :)
Any idea on the 2.) maybe? ;)
d
doggo
Halfop
Posts: 97
Joined: Tue Jan 05, 2010 7:53 am
Contact:

Post by doggo »

try this, i made it a while ago for the bot in my websites irc channel, i hate clicking links when i dont know what the content will be! it should do everything you said you wanted already.. if not its easy enough to change a few bits here and there ;)

heres what it looks like http://i.imgur.com/FQRHu.png

Code: Select all

# youtube.tcl 
# by doggo #omgwtfnzbs@EFNET 
######################### 
package require http

bind pubm -|- * youtube:match 

proc youtube:match {nick uhost hand chan text} { 
	
	if {[regexp -nocase {http://.*youtube.com/watch\?(.*)v=([A-Za-z0-9_\-]+)} $text match fluff youtubeid]} {
	} elseif {[regexp -nocase {http://.*youtu.be/([A-Za-z0-9_\-]+)} $text match youtubeid]} {
	} else { return }

	set youtube_query "http://www.youtube.com/watch?v=$youtubeid"
	set html [youtube:getdata $youtube_query]

	regexp -nocase {<meta name="title" content="(.*?)">} $html match title 
	regexp -nocase {<span class="watch-view-count">.*?<strong>(.*?)</strong>} $html match views
	regexp -nocase {<span class="likes">(.*?)</span> likes, <span class="dislikes">(.*?)</span>} $html match likes dislikes
	regexp -nocase {<span class="comments-section-stat">\((.*?)\)</span>} $html match comments
	regexp {"length_seconds": ([0-9]+),} $html match length

	set title [youtube:htmlcodes $title]
	if {$title==0} {return}

	if {$length > 3600} {
	set length [clock format $length -format %H:%M:%S]
	} else {
	set length [clock format $length -format %M:%S]
	}

	if {$comments == 1} {
	set com "comment"
	} else {
	set com "comments"
	}

	set logo "\002\0031,00 You\0031,00 \00300,04 Tube \00300,04\003\002\00314"
	set views "($views views)"
	set likes "$likes likes,"
	set dislikes "$dislikes dislikes"
	set comments "& $comments $com"

	puthelp "privmsg $chan :$logo \"$title\" $length $views $likes $dislikes $comments"
	
}

proc youtube:getdata {data} {

	::http::config -useragent "Mozilla/5.0 (X11; U; Linux i686; ru-RU; rv:1.8.1) Gecko/2006101023 Firefox/2.0"
	set url [::http::geturl $data -timeout 5000]
	set html [::http::data $url]
	::http::cleanup $html

	return $html

}

# borrowed from an old imdb script
proc youtube:htmlcodes {content} { 
	if {$content == ""} { 
	return 0 
	} 
	if {![string match *&* $content]} { 
	return $content 
	} 
	set escapes { 
	  \x20 " \x22 & \x26 &apos; \x27 – \x2D 
	< \x3C > \x3E ˜ \x7E € \x80 ¡ \xA1 
	¢ \xA2 £ \xA3 ¤ \xA4 ¥ \xA5 ¦ \xA6 
	§ \xA7 ¨ \xA8 © \xA9 ª \xAA « \xAB 
	¬ \xAC ­ \xAD ® \xAE &hibar; \xAF ° \xB0 
	± \xB1 ² \xB2 ³ \xB3 ´ \xB4 µ \xB5 
	¶ \xB6 · \xB7 ¸ \xB8 ¹ \xB9 º \xBA 
	» \xBB ¼ \xBC ½ \xBD ¾ \xBE ¿ \xBF 
	À \xC0 Á \xC1 Â \xC2 Ã \xC3 Ä \xC4 
	Å \xC5 Æ \xC6 Ç \xC7 È \xC8 É \xC9 
	Ê \xCA Ë \xCB Ì \xCC Í \xCD Î \xCE 
	Ï \xCF Ð \xD0 Ñ \xD1 Ò \xD2 Ó \xD3 
	Ô \xD4 Õ \xD5 Ö \xD6 × \xD7 Ø \xD8 
	Ù \xD9 Ú \xDA Û \xDB Ü \xDC Ý \xDD 
	Þ \xDE ß \xDF à \xE0 á \xE1 â \xE2 
	ã \xE3 ä \xE4 å \xE5 æ \xE6 ç \xE7 
	è \xE8 é \xE9 ê \xEA ë \xEB ì \xEC 
	í \xED î \xEE ï \xEF ð \xF0 ñ \xF1 
	ò \xF2 ó \xF3 ô \xF4 õ \xF5 ö \xF6 
	÷ \xF7 ø \xF8 ù \xF9 ú \xFA û \xFB 
	ü \xFC ý \xFD þ \xFE ÿ \xFF 
	} 
	set content [string map $escapes $content]
	set content [string map [list "\]" "\\\]" "\[" "\\\[" "\$" "\\\$" "\\" "\\\\"] $content]
	regsub -all -- {&#([[:digit:]]{1,5});} $content {[format %c [string trimleft "\1" "0"]]} content 
	regsub -all -- {&#x([[:xdigit:]]{1,4});} $content {[format %c [scan "\1" %x]]} content
	regsub -all -- {&#?[[:alnum:]]{2,7};} $content "?" content 
	return [subst $content]
}
N
Nash
Voice
Posts: 6
Joined: Sun Jul 29, 2012 7:14 pm

Post by Nash »

Hey, thanks for your reply aswell!
I'm good on this issue now. Thanks alot tho!! :)
Post Reply