Advertisement
knarzling

youtube.tcl

Nov 7th, 2014
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 5.49 KB | None | 0 0
  1. ##############################################################################################
  2. ##  ##     Youtube.tcl for eggdrop by Ford_Lawnmower irc.geekshed.net #Script-Help      ##  ##
  3. ##############################################################################################
  4. ## To use this script you must set channel flag +youtubeURL (ie .chanset #chan +youtubeURL) ##
  5. ##############################################################################################
  6. package require tls
  7. ##############################################################################################
  8. ##  ##                             Start Setup.                                         ##  ##
  9. ##############################################################################################
  10. proc youtubesurl {nick host hand chan text} {
  11.   if {[lsearch -exact [channel info $chan] +youtubeURL] != -1} {
  12. ## Change the characters between the "" below to change the logo shown with each result.    ##
  13.     set youtubelogo "\002\00301,00You\00300,04Tube\002\017"
  14. ## Change the format codes between the "" below to change the color/state of the text.      ##
  15.     set textf "\002\017"
  16.     set tagcolor "\002"
  17.     set hostlang "en"
  18. ##############################################################################################
  19. ##  ##                           End Setup.                                              ## ##
  20. ##############################################################################################
  21.     set youtubecheck [regexp -all -nocase {(?:\/watch\?v=|youtu\.be\/)([\d\w-]{11})} $text match youtubeid]
  22.     if {$youtubeid != ""} {
  23.       set youtubesite gdata.youtube.com
  24.       set youtubeurl "/feeds/api/videos/${youtubeid}?v=2&hl=${hostlang}"
  25.       set youtubeviews ""
  26.       set youtubedesc ""
  27.       set youtubeuser ""
  28.       set youtubelikes ""
  29.       set youtubedislikes ""
  30.       set youtubedate ""
  31.       set youtubefound ""
  32.       set youtubeduration ""
  33.       set youtubecategory ""
  34.       if {[catch {set youtubesock [tls::socket $youtubesite 443]} sockerr]} {
  35.         putlog "$youtubesite $youtubeurl $sockerr error"
  36.         return 0
  37.       } else {
  38.         puts $youtubesock "GET $youtubeurl HTTP/1.0"
  39.         puts $youtubesock "Host: $youtubesite"
  40.         puts $youtubesock ""
  41.         flush $youtubesock
  42.         while {![eof $youtubesock]} {
  43.           set youtubevar " [gets $youtubesock] "
  44.           #title
  45.           if {[regexp -nocase {<title>(.*)<\/title>} $youtubevar match youtubedesc]} {
  46.             set youtubedesc "${tagcolor}Title: ${textf}${youtubedesc}"
  47.           }
  48.           #uploader
  49.           if {[regexp -nocase {<name>(.*)<\/name>} $youtubevar match youtubeuser]} {
  50.             set youtubeuser "${tagcolor}Uploader: ${textf}${youtubeuser}"
  51.           }
  52.           #views
  53.           if {[regexp -nocase {viewCount='([^']*)} $youtubevar match youtubeviews]} {
  54.             set youtubeviews "${tagcolor}Views: ${textf}[ytaddcomma ${youtubeviews}]"
  55.           }
  56.           #likes
  57.           if {[regexp -nocase {numLikes='([^']*)'} $youtubevar match youtubelikes]} {
  58.            set youtubelikes "${tagcolor}Likes: ${textf}[ytaddcomma ${youtubelikes}]"
  59.          }
  60.          #dislikes
  61.          if {[regexp -nocase {numDislikes='([^']*)'} $youtubevar match youtubedislikes]} {
  62.             set youtubedislikes "${tagcolor}Dislikes: ${textf}[ytaddcomma ${youtubedislikes}]"
  63.           }
  64.           #duration
  65.           if {[regexp -nocase {seconds='([^']*)} $youtubevar match youtubeduration]} {
  66.             set youtubeduration "${tagcolor}Duration: ${textf} [ytfixduration $youtubeduration]"
  67.           }
  68.           #.cat'>,<
  69.           if {[regexp -nocase {\.cat'>([^<]*)<} $youtubevar match youtubecategory]} {
  70.            set youtubecategory "${tagcolor}Type: ${textf}${youtubecategory}"
  71.          }
  72.          #date + Output
  73.          if {[regexp {<published>([^T]*)T} $youtubevar match youtubedate]} {
  74.            set youtubedate "${tagcolor}Uploaded: ${textf}[string map -nocase {"uploaded on" ""} ${youtubedate}]"          
  75.          }
  76.          if {[regexp {<\/entry>} $youtubevar 1] != 0} {
  77.            putserv "PRIVMSG $chan :[yturldehex "${youtubelogo} ${youtubedesc} ${youtubecategory} ${youtubeviews} ${youtubelikes} \
  78.            ${youtubedislikes} ${youtubeuser} ${youtubedate} ${youtubeduration}"]"  
  79.            close $youtubesock
  80.            return 0          }
  81.          }
  82.        close $youtubesock
  83.        return 0
  84.      }
  85.    }
  86.  }
  87. }
  88. proc ytfixduration {secs} {
  89.  set h [expr {$secs/3600}]
  90.  incr secs [expr {$h*-3600}]
  91.  set m [expr {$secs/60}]
  92.  set s [expr {$secs%60}]
  93.  if {$h} {
  94.    set h "$h hours"
  95.  } else {
  96.    set h ""
  97.  }
  98.  if {$m} {
  99.    set m "$m minutes"
  100.  } else {
  101.    set m ""
  102.  }
  103.  if {$s} {
  104.    set s "$s seconds"
  105.  } else {
  106.    set s ""
  107.  }
  108.  return "$h $m $s"
  109. }
  110. proc ytaddcomma {number} {regsub -all {\d(?=(\d{3})+($|\.))} $number {\0,}}
  111. proc yturldehex {string} {
  112.  regsub -all {[\[\]]} $string "" string
  113.  set string [subst [regsub -nocase -all {\&#([0-9]{2});} $string {[format %c \1]}]]
  114.  return [encoding convertfrom utf-8 [string map {&quot; \" \xa0 "," &amp; \&} $string]]
  115. }
  116. proc yturlencode {instring} {
  117.  return [subst [regsub -nocase -all {([^a-z0-9])} $instring {%[format %x [scan "\\&" %c]]}]]
  118. }
  119. bind pubm -|- "*youtube.*watch?v=*" youtubesurl
  120. bind pubm -|- "*youtu.be/*" youtubesurl
  121. setudef flag youtubeURL
  122. putlog "\002*Loaded* \00301,00You\00300,04Tube\002\017 \002URL check api version by Ford_Lawnmower irc.GeekShed.net #Script-Help"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement