Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- set youtube(timeout) "30000"
- set youtube(oembed_location) "http://www.youtube.com/oembed"
- set youtube(tiny_url) 0
- set youtube(response_format) "\002(\0031,0You\0030,4Tube\0031,0\003) \037Title\037:\002 \0037\"%title%\"\003"
- bind pubm - * public_youtube
- set youtube(pattern) {http://.*youtube.*/watch\?(.*)v=([A-Za-z0-9_\-]+)}
- set youtube(maximum_redirects) 2
- set youtube(maximum_title_length) 256
- set izuzmi [list "#bebe" "#mirchat"]
- ###############################################################################
- package require http
- set gTheScriptVersion "0.5"
- proc note {msg} {
- putlog "% $msg"
- }
- ###############################################################################
- proc make_tinyurl {url} {
- if {[info exists url] && [string length $url]} {
- if {[regexp {http://tinyurl\.com/\w+} $url]} {
- set http [::http::geturl $url -timeout 9000]
- upvar #0 $http state ; array set meta $state(meta)
- ::http::cleanup $http ; return $meta(Location)
- } else {
- set http [::http::geturl "http://tinyurl.com/create.php" \
- -query [::http::formatQuery "url" $url] -timeout 9000]
- set data [split [::http::data $http] \n] ; ::http::cleanup $http
- for {set index [llength $data]} {$index >= 0} {incr index -1} {
- if {[regexp {href="http://tinyurl\.com/\w+"} [lindex $data $index] url]} {
- return [string map { {href=} "" \" "" } $url]
- }}}}
- error "failed to get tiny url."
- }
- ###############################################################################
- proc flat_json_decoder {info_array_name json_blob} {
- upvar 1 $info_array_name info_array
- # 0 looking for key, 1 inside key, 2 looking for value, 3 inside value
- set kvmode 0
- set cl 0
- set i 1
- set length [string length $json_blob]
- while { $i < $length } {
- set c [string index $json_blob $i]
- if { [string equal $c "\""] && [string equal $cl "\\"] == 0 } {
- if { $kvmode == 0 } {
- set kvmode 1
- set start [expr $i + 1]
- } elseif { $kvmode == 1 } {
- set kvmode 2
- set name [string range $json_blob $start [expr $i - 1]]
- } elseif { $kvmode == 2 } {
- set kvmode 3
- set start [expr $i + 1]
- } elseif { $kvmode == 3 } {
- set kvmode 0
- set info_array($name) [string range $json_blob $start [expr $i - 1]]
- }
- }
- set cl $c
- incr i 1
- }
- }
- proc filter_title {blob} {
- # Try and convert escaped unicode
- set blob [subst -nocommands -novariables $blob]
- set blob [string trim $blob]
- set blob
- }
- proc extract_title {json_blob} {
- global youtube
- array set info_array {}
- flat_json_decoder info_array $json_blob
- if { [info exists info_array(title)] } {
- set title [filter_title $info_array(title)]
- } else {
- error "Failed to find title. JSON decoding failure?"
- }
- if { [string length $title] > $youtube(maximum_title_length) - 1 } {
- set title [string range $title 0 $youtube(maximum_title_length)]"..."
- } elseif { [string length $title] == 0 } {
- set title "No usable title."
- }
- return $title
- }
- ###############################################################################
- proc fetch_title {youtube_uri {recursion_count 0}} {
- global youtube
- if { $recursion_count > $youtube(maximum_redirects) } {
- error "maximum recursion met."
- }
- set query [http::formatQuery url $youtube_uri]
- set response [http::geturl "$youtube(oembed_location)?$query" -timeout $youtube(timeout)]
- upvar #0 $response state
- foreach {name value} $state(meta) {
- if {[regexp -nocase ^location$ $name]} {
- return [fetch_title $value [incr recursion_count]]
- }
- }
- if [expr [http::ncode $response] == 401] {
- error "Location contained restricted embed data."
- } else {
- set response_body [http::data $response]
- http::cleanup $response
- return [extract_title $response_body]
- }
- }
- proc public_youtube {nick userhost handle channel args} {
- global youtube botnick
- global izuzmi
- set ytnocolor "(\037Y\037ou\037T\037ube) \037T\037itle:\002 \"%title%\"\002"
- if {[regexp -nocase -- $youtube(pattern) $args match fluff video_id]} {
- note "Fetching title for $match."
- if {[catch {set title [fetch_title $match]} error]} {
- note "Failed to fetch title: $error"
- } else {
- set tinyurl $match
- if { $youtube(tiny_url) == 1 && \
- [catch {set tinyurl [make_tinyurl $match]}]} {
- note "Failed to make tiny url for $match."
- }
- set tokens [list %botnick% $botnick %post_nickname% \
- $nick %title% "[ encoding convertto utf-8 $title]" %youtube_url% \
- "$match" %tinyurl% "$tinyurl"]
- if {[string match *c* [getchanmode $channel]]} {
- if {[string match -nocase *$channel* $izuzmi] != "1"} {
- set result [string map $tokens $ytnocolor]
- putserv "PRIVMSG $channel :$result" }
- } else {
- if {[string match -nocase *$channel* $izuzmi] != "1"} {
- set result [string map $tokens $youtube(response_format)]
- putserv "PRIVMSG $channel :$result" }
- }
- }
- }
- }
- ###############################################################################
- note "youtube_title$gTheScriptVersion: loaded";
Advertisement
Add Comment
Please, Sign In to add comment