Advertisement
outsider

Youtube Eggdrop script

May 8th, 2012
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 4.82 KB | None | 0 0
  1. package require http
  2. bind pubm - *http://*youtu*/watch* pubm:youtube
  3. bind ctcp - * action:youtube
  4.  
  5. proc action:youtube {nick host hand dest keyword args} {
  6.         set args [lindex $args 0]
  7.         if {![validchan $dest]} {return}
  8.         if {$keyword == "ACTION" && [string match *http://*youtu*/watch* $args]} {pubm:youtube $nick $host $hand $dest $args}
  9. }
  10.  
  11. proc pubm:youtube {nick host hand chan args} {
  12.         set args [lindex $args 0]
  13.         while {[regexp -- {(http:\/\/.*youtub.*/watch.*)} $args -> url args]} {
  14.                 while {[regexp -- {v=(.*)&?} $url -> vid url]} {
  15.                         set vid [lindex [split $vid &] 0]
  16.  
  17.                         set gurl "http://gdata.youtube.com/feeds/api/videos/$vid"
  18.                         set search [http::formatQuery v 2 alt jsonc prettyprint true]
  19.                         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 "*/*"]
  20.                         set token [http::geturl "$gurl?$search"]
  21.                         set data [::http::data $token]
  22.                         http::cleanup $token
  23.                         set lines [split $data "\n"]
  24.                         set title ""
  25.                         set duration ""
  26.                         set viewCount ""
  27.                         foreach line $lines {
  28.                                 if {$title==""} {set title [lindex [regexp -all -nocase -inline {\"title\"\: \"(.*)\"} $line] 1]}
  29.                                if {$duration==""} {set duration [lindex [regexp -all -nocase -inline {\"duration\"\: ([0-9]+)} $line] 1]}
  30.                                if {$viewCount==""} {set viewCount [lindex [regexp -all -nocase -inline {\"viewCount\"\: ([0-9]+)} $line] 1]}
  31.                        }
  32.                        set title [yturldehex $title]
  33.                        putmsg $chan "\002\00301,00You\00300,04Tube\017\002 $title | Duration: [shortduration $duration] | $viewCount views"
  34.                }
  35.        }
  36. }
  37.  
  38. bind pub - !youtube pub:youtube
  39. proc pub:youtube {nick host hand chan args} {
  40.        set args [lindex $args 0]
  41.        if {$args == ""} {
  42.                putnotc $nick "Gebruik: \002!youtube <zoeksleutel>\002 om te zoeken"
  43.                return
  44.        }
  45.        set search [http::formatQuery v 2 alt jsonc q $args orderby viewCount max-results 5 prettyprint true]
  46.        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 "*/*"]
  47.        set token [http::geturl "http://gdata.youtube.com/feeds/api/videos?$search"]
  48.        set data [::http::data $token]
  49.        http::cleanup $token
  50.  
  51.        set totalitems [lindex [regexp -all -nocase -inline {\"totalItems\"\: ([0-9]+)} $data] 1]
  52.        putnotc $nick "\002\00301,00You\00300,04Tube\017 Er zijn $totalitems resultaten die voldoen aan de zoeksleutel \002$args\002"
  53.        set lines [split $data "\n"]
  54.        set results ""
  55.        foreach line $lines {
  56.                if {[regexp -all -nocase -inline {\"id\"\: \"(.*)\"} $line] != ""} {
  57.                        set id [lindex [regexp -all -nocase -inline {\"id\"\: \"(.*)\"} $line] 1]
  58.                        lappend results $id
  59.                }
  60.                if {[regexp -all -nocase -inline {\"title\"\: \"(.*)\"} $line] != ""} {
  61.                        set result($id,title) [yturldehex [lindex [regexp -all -nocase -inline {\"title\"\: \"(.*)\"} $line] 1]]
  62.                }
  63.                if {[regexp -all -nocase -inline {\"duration\"\: ([0-9]+)} $line] != ""} {
  64.                        set result($id,duration) [lindex [regexp -all -nocase -inline {\"duration\"\: ([0-9]+)} $line] 1]
  65.                }
  66.                if {[regexp -all -nocase -inline {\"viewCount\"\: ([0-9]+)} $line] != ""} {
  67.                        set result($id,viewCount) [lindex [regexp -all -nocase -inline {\"viewCount\"\: ([0-9]+)} $line] 1]
  68.                }
  69.        }
  70.        foreach res $results {
  71.                putnotc $nick "\002\00301,00You\00300,04Tube\017 \002$result($res,title)\002 | [shortduration $result($res,duration)] | $result($res,viewCount) views | http://www.youtube.com/watch?v=$res"
  72.        }
  73. }
  74.  
  75. proc yturldehex {string} {
  76.        regsub -all {[\[\]]} $string "" string
  77.        set string [subst [regsub -nocase -all {\&#([0-9]{2,4});} $string {[format %c \1]}]]
  78.        return [string map {&quot; \"} $string]
  79. }
  80.  
  81. proc shortduration {seconds} {
  82.        set hours [expr int(floor($seconds/3600))]
  83.        set minutes [expr int(floor(($seconds%3600)/60))]
  84.        set seconds [expr $seconds - ($hours*3600) - ($minutes*60)]
  85.        if {$hours<10} { set hours "0$hours" }
  86.        if {$minutes<10} { set minutes "0$minutes" }
  87.        if {$seconds < 10} { set seconds "0$seconds" }
  88.        return "$hours:$minutes:$seconds"
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement