r00t-err0r

youtube-modified

Aug 4th, 2012
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 5.61 KB | None | 0 0
  1. set youtube(timeout)            "30000"
  2.  
  3. set youtube(oembed_location)    "http://www.youtube.com/oembed"
  4.  
  5. set youtube(tiny_url)           0
  6.  
  7. set youtube(response_format) "\002(\0031,0You\0030,4Tube\0031,0\003) \037Title\037:\002 \0037\"%title%\"\003"
  8.  
  9. bind pubm - * public_youtube
  10.  
  11. set youtube(pattern) {http://.*youtube.*/watch\?(.*)v=([A-Za-z0-9_\-]+)}
  12.  
  13. set youtube(maximum_redirects)  2
  14.  
  15. set youtube(maximum_title_length) 256
  16.  
  17. set izuzmi [list "#bebe" "#mirchat"]
  18. ###############################################################################
  19.  
  20.  
  21.  
  22. package require http
  23.  
  24.  
  25.  
  26. set gTheScriptVersion "0.5"
  27.  
  28.  
  29.  
  30. proc note {msg} {
  31.  
  32.   putlog "% $msg"
  33.  
  34. }
  35.  
  36.  
  37.  
  38. ###############################################################################
  39.  
  40.  
  41.  
  42. proc make_tinyurl {url} {
  43.  
  44.  if {[info exists url] && [string length $url]} {
  45.  
  46.   if {[regexp {http://tinyurl\.com/\w+} $url]} {
  47.  
  48.    set http [::http::geturl $url -timeout 9000]
  49.  
  50.    upvar #0 $http state ; array set meta $state(meta)
  51.  
  52.    ::http::cleanup $http ; return $meta(Location)
  53.  
  54.   } else {
  55.  
  56.    set http [::http::geturl "http://tinyurl.com/create.php" \
  57.  
  58.      -query [::http::formatQuery "url" $url] -timeout 9000]
  59.  
  60.    set data [split [::http::data $http] \n] ; ::http::cleanup $http
  61.  
  62.    for {set index [llength $data]} {$index >= 0} {incr index -1} {
  63.  
  64.     if {[regexp {href="http://tinyurl\.com/\w+"} [lindex $data $index] url]} {
  65.  
  66.      return [string map { {href=} "" \" "" } $url]
  67.  
  68. }}}}
  69.  
  70. error "failed to get tiny url."
  71.  
  72. }
  73.  
  74.  
  75.  
  76. ###############################################################################
  77.  
  78.  
  79.  
  80. proc flat_json_decoder {info_array_name json_blob} {
  81.  
  82.   upvar 1 $info_array_name info_array
  83.  
  84.   # 0 looking for key, 1 inside key, 2 looking for value, 3 inside value
  85.  
  86.   set kvmode 0
  87.  
  88.   set cl 0
  89.  
  90.   set i 1
  91.  
  92.   set length [string length $json_blob]
  93.  
  94.   while { $i < $length } {
  95.  
  96.      set c [string index $json_blob $i]
  97.  
  98.      if { [string equal $c "\""] && [string equal $cl "\\"] == 0 } {
  99.  
  100.          if { $kvmode == 0 } {
  101.  
  102.             set kvmode 1
  103.  
  104.             set start [expr $i + 1]
  105.  
  106.          } elseif { $kvmode == 1 } {
  107.  
  108.             set kvmode 2
  109.  
  110.             set name [string range $json_blob $start [expr $i - 1]]
  111.  
  112.          } elseif { $kvmode == 2 } {
  113.  
  114.             set kvmode 3
  115.  
  116.             set start [expr $i + 1]
  117.  
  118.          } elseif { $kvmode == 3 } {
  119.  
  120.             set kvmode 0
  121.  
  122.             set info_array($name) [string range $json_blob $start [expr $i - 1]]
  123.  
  124.          }
  125.  
  126.       }
  127.  
  128.       set cl $c
  129.  
  130.       incr i 1
  131.  
  132.    }
  133.  
  134. }
  135.  
  136.  
  137.  
  138. proc filter_title {blob} {
  139.  
  140.    # Try and convert escaped unicode
  141.  
  142.    set blob [subst -nocommands -novariables $blob]
  143.  
  144.    set blob [string trim $blob]
  145.  
  146.    set blob
  147.  
  148. }
  149.  
  150.  
  151.  
  152. proc extract_title {json_blob} {
  153.  
  154.    global youtube
  155.  
  156.    array set info_array {}
  157.  
  158.    flat_json_decoder info_array $json_blob
  159.  
  160.    if { [info exists info_array(title)] } {
  161.  
  162.       set title [filter_title $info_array(title)]
  163.  
  164.    } else {
  165.  
  166.       error "Failed to find title.  JSON decoding failure?"
  167.  
  168.    }
  169.  
  170.    if { [string length $title] > $youtube(maximum_title_length) - 1 } {
  171.  
  172.       set title [string range $title 0 $youtube(maximum_title_length)]"..."
  173.  
  174.    } elseif { [string length $title] == 0 } {
  175.  
  176.       set title "No usable title."
  177.  
  178.    }
  179.  
  180.    return $title
  181.  
  182. }
  183.  
  184.  
  185.  
  186. ###############################################################################
  187.  
  188.  
  189.  
  190. proc fetch_title {youtube_uri {recursion_count 0}} {
  191.  
  192.     global youtube
  193.  
  194.     if { $recursion_count > $youtube(maximum_redirects) } {
  195.  
  196.         error "maximum recursion met."
  197.  
  198.     }
  199.  
  200.     set query [http::formatQuery url $youtube_uri]
  201.  
  202.     set response [http::geturl "$youtube(oembed_location)?$query" -timeout $youtube(timeout)]
  203.  
  204.     upvar #0 $response state
  205.  
  206.     foreach {name value} $state(meta) {
  207.  
  208.         if {[regexp -nocase ^location$ $name]} {
  209.  
  210.             return [fetch_title $value [incr recursion_count]]
  211.  
  212.         }
  213.  
  214.     }
  215.  
  216.     if [expr [http::ncode $response] == 401] {
  217.  
  218.         error "Location contained restricted embed data."
  219.  
  220.     } else {
  221.  
  222.         set response_body [http::data $response]
  223.  
  224.         http::cleanup $response
  225.  
  226.         return [extract_title $response_body]
  227.  
  228.     }
  229.  
  230. }
  231.  
  232.  
  233.  
  234. proc public_youtube {nick userhost handle channel args} {
  235.  
  236.     global youtube botnick
  237.     global izuzmi
  238.     set ytnocolor "(\037Y\037ou\037T\037ube) \037T\037itle:\002 \"%title%\"\002"
  239.     if {[regexp -nocase -- $youtube(pattern) $args match fluff video_id]} {
  240.  
  241.         note "Fetching title for $match."
  242.  
  243.         if {[catch {set title [fetch_title $match]} error]} {
  244.  
  245.             note "Failed to fetch title: $error"
  246.  
  247.         } else {
  248.  
  249.             set tinyurl $match
  250.  
  251.             if { $youtube(tiny_url) == 1 && \
  252.  
  253.               [catch {set tinyurl [make_tinyurl $match]}]} {
  254.  
  255.                note "Failed to make tiny url for $match."
  256.  
  257.             }
  258.  
  259.             set tokens [list %botnick% $botnick %post_nickname% \
  260.                 $nick %title% "[ encoding convertto utf-8 $title]" %youtube_url% \
  261.                 "$match" %tinyurl% "$tinyurl"]
  262. if {[string match *c* [getchanmode $channel]]} {
  263. if {[string match -nocase *$channel* $izuzmi] != "1"} {
  264.             set result [string map $tokens $ytnocolor]
  265.             putserv "PRIVMSG $channel :$result" }
  266. } else {
  267. if {[string match -nocase *$channel* $izuzmi] != "1"} {
  268.             set result [string map $tokens $youtube(response_format)]
  269.             putserv "PRIVMSG $channel :$result" }
  270. }
  271.         }
  272.  
  273.     }
  274.  
  275. }
  276.  
  277.  
  278.  
  279. ###############################################################################
  280.  
  281.  
  282.  
  283. note "youtube_title$gTheScriptVersion: loaded";
Advertisement
Add Comment
Please, Sign In to add comment