Youtube url API Version for eggdrop

By FordLawnmower on Jan 20, 2015

I'm adding this api version because I simply don't have the time to maintain the normal scrap version.
youtube seems to make changes every week

Youtube URL Checker for your eggdrop.

To use:

  • Copy this code and paste to a new file in wordpad or w/e not notepad
  • Save with a unique filename with the extension .tcl
  • Copy/Upload the .tcl file to your eggdrops scripts directory.
  • Add a line like this to your eggdrop.conf -:
source scripts/youtubeurl.tcl
  • Rehash or restart the eggdrop.
  • (In the Party Line) Type .chanset #ChannelName +youtubeURL
  • +/- youtubeURL as needed.

That's it. The script will respond in channels with the +youtubeURL flag.
See the top of the script to change the logo or text color.

##############################################################################################
##  ##     Youtube.tcl for eggdrop by Ford_Lawnmower irc.geekshed.net #Script-Help      ##  ##
##############################################################################################
## To use this script you must set channel flag +youtubeURL (ie .chanset #chan +youtubeURL) ##
##############################################################################################
package require tls
##############################################################################################
##  ##                             Start Setup.                                         ##  ##
##############################################################################################
proc youtubesurl {nick host hand chan text} {
  if {[lsearch -exact [channel info $chan] +youtubeURL] != -1} {
## Change the characters between the "" below to change the logo shown with each result.    ##
    set youtubelogo "\002\00301,00You\00300,04Tube\002\017"
## Change the format codes between the "" below to change the color/state of the text.      ##
    set textf "\002\017"
    set tagcolor "\002"
    set hostlang "en"
##############################################################################################
##  ##                           End Setup.                                              ## ##
##############################################################################################
    set youtubecheck [regexp -all -nocase {(?:\/watch\?v=|youtu\.be\/)([\d\w-]{11})} $text match youtubeid]
    if {$youtubeid != ""} {
      set youtubesite gdata.youtube.com
      set youtubeurl "/feeds/api/videos/${youtubeid}?v=2&hl=${hostlang}"
      set youtubeviews ""
      set youtubedesc ""
      set youtubeuser ""
      set youtubelikes ""
      set youtubedislikes ""
      set youtubedate ""
      set youtubefound ""
      set youtubeduration ""
      set youtubecategory ""
      if {[catch {set youtubesock [tls::socket $youtubesite 443]} sockerr]} {
        putlog "$youtubesite $youtubeurl $sockerr error"
        return 0
      } else {
        puts $youtubesock "GET $youtubeurl HTTP/1.0"
        puts $youtubesock "Host: $youtubesite"
        puts $youtubesock ""
        flush $youtubesock
        while {![eof $youtubesock]} {
          set youtubevar " [gets $youtubesock] "
          #title
          if {[regexp -nocase {<title>(.*)<\/title>} $youtubevar match youtubedesc]} {
            set youtubedesc "${tagcolor}Title: ${textf}${youtubedesc}"
          }
          #uploader
          if {[regexp -nocase {<name>(.*)<\/name>} $youtubevar match youtubeuser]} {
            set youtubeuser "${tagcolor}Uploader: ${textf}${youtubeuser}"
          }
          #views 
          if {[regexp -nocase {viewCount='([^']*)} $youtubevar match youtubeviews]} {
            set youtubeviews "${tagcolor}Views: ${textf}[ytaddcomma ${youtubeviews}]"
          } 
          #likes
          if {[regexp -nocase {numLikes='([^']*)'} $youtubevar match youtubelikes]} {
            set youtubelikes "${tagcolor}Likes: ${textf}[ytaddcomma ${youtubelikes}]"
          }  
          #dislikes
          if {[regexp -nocase {numDislikes='([^']*)'} $youtubevar match youtubedislikes]} {
            set youtubedislikes "${tagcolor}Dislikes: ${textf}[ytaddcomma ${youtubedislikes}]"
          }
          #duration 
          if {[regexp -nocase {seconds='([^']*)} $youtubevar match youtubeduration]} {
            set youtubeduration "${tagcolor}Duration: ${textf} [ytfixduration $youtubeduration]"
          }
          #.cat'>,<
          if {[regexp -nocase {\.cat'>([^<]*)<} $youtubevar match youtubecategory]} {
            set youtubecategory "${tagcolor}Type: ${textf}${youtubecategory}"
          }
          #date + Output 
          if {[regexp {<published>([^T]*)T} $youtubevar match youtubedate]} {
            set youtubedate "${tagcolor}Uploaded: ${textf}[string map -nocase {"uploaded on" ""} ${youtubedate}]"            
          }
          if {[regexp {<\/entry>} $youtubevar 1] != 0} {
            putserv "PRIVMSG $chan :[yturldehex "${youtubelogo} ${youtubedesc} ${youtubecategory} ${youtubeviews} ${youtubelikes} \
            ${youtubedislikes} ${youtubeuser} ${youtubedate} ${youtubeduration}"]"   
            close $youtubesock
            return 0          } 
          }
        close $youtubesock
        return 0 
      }
    }
  }
}
proc ytfixduration {secs} {
  set h [expr {$secs/3600}]
  incr secs [expr {$h*-3600}]
  set m [expr {$secs/60}]
  set s [expr {$secs%60}]
  if {$h} { 
    set h "$h hours" 
  } else {
    set h ""
  }
  if {$m} { 
    set m "$m minutes" 
  } else {
    set m ""
  }
  if {$s} { 
    set s "$s seconds" 
  } else {
    set s ""
  }
  return "$h $m $s"
}
proc ytaddcomma {number} {regsub -all {\d(?=(\d{3})+($|\.))} $number {\0,}}
proc yturldehex {string} {
  regsub -all {[\[\]]} $string "" string
  set string [subst [regsub -nocase -all {\&#([0-9]{2});} $string {[format %c \1]}]]
  return [encoding convertfrom utf-8 [string map {&quot; \" \xa0 "," &amp; \&} $string]]
}
proc yturlencode {instring} {
  return [subst [regsub -nocase -all {([^a-z0-9])} $instring {%[format %x [scan "\\&" %c]]}]]
}
bind pubm -|- "*youtube.*watch?v=*" youtubesurl
bind pubm -|- "*youtu.be/*" youtubesurl
setudef flag youtubeURL
putlog "\002*Loaded* \00301,00You\00300,04Tube\002\017 \002URL check api version by Ford_Lawnmower irc.GeekShed.net #Script-Help"

Comments

Sign in to comment.
RonnieDeboer   -  Mar 14, 2019

hmmmm i don't know why api version doesn't work for me. what i have to do?

 Respond  
xplo   -  Jul 07, 2016

Is it me or it's broken.. again... thanks google.

<dcc chat> Tcl error [youtubesurl]: error flushing "sock11": software caused connection abort

i do have latest tcl, and tcl-tls installed.

 Respond  
iNfzx   -  Mar 01, 2016

api version doesn't work for me

 Respond  
TheWhistler   -  Jul 17, 2015

i'm looking for a eggdrop tcl that will autovoice and aurto-halfop in a CHANNEL

 Respond  
Caligula   -  May 30, 2015

It doesn't work anymore.
For all videos it responds always:

YouTube Title: https://youtube.com/devicesupport Type: Howto Views: 32,344,435 Uploader: YouTube Help Uploaded: 2015-04-17 Duration: 3 minutes 56 seconds

 Respond  
The_Darkfire   -  Apr 15, 2015

Thankyou! Grabbed this for our IRC server yesterday. We found it didn't read links obtained from embedded youtube videos, eg: https://www.youtube.com/watch?feature=player_embedded&v=

To fix this we changed line 21 to

set youtubecheck [regexp -all -nocase {(?:\?v=|\&v=|youtu\.be\/)([\d\w-]{11})} $text match youtubeid]

And added

 bind pubm -|- "*youtube.*&v=*" youtubesurl
Caligula  -  May 30, 2015

It not working anymore. Can you fix it? Thank you so much.

Sign in to comment

beyondthepale   -  Feb 21, 2015

Thanks loads

 Respond  
buffer   -  Jan 27, 2015

Thank you again for your work!

 Respond  
Hawkee   -  Jan 20, 2015

Always better to use an API rather than scraping.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.