YouTube parser

From Scriptwiki
Jump to: navigation, search


As the description says, this script retrieves information about youtube videos from the YouTube API and posts them to the channel whenever a youtube link is posted. You can install it by opening the script editor (Alt+R), creating a new script file (File -> New) and then copy paste the script into it.

Note: Make sure to replace the channels (#foo,#bar) in the on TEXT event line with the channels you want the script to be active in.

/*
********************************************************************************************************
*
*  youtube.mrc by Jay2k1 @ QuakeNet, 2013
*
*  Whenever a youtube link is posted in the channels #foo or #bar, this script retrieves the video's details
*  from the youtube API and post them to the channel. It looks like this:
*
*  <Jay2k1> http://www.youtube.com/watch?v=oHg5SJYRHA0
*  <Bot> 'RickRoll' by cotter548, 00:03:34, 68213084 views, rating: 87% (259236/38751)
*
*********************************************************************************************************
*/

; on text event: regex check for messages containing youtube.com or youtu.be, match the URL
on $*:TEXT:/(youtu\.be\/.*?(\s|$)|youtube\.com\/.*?(\s|$))/:#foo,#bar:{

  ; use another regex to extract the video ID from the matched URL
  noop $regex($regml(1),/(\?v=|&v=|\/\d\/|\/embed\/|\/v\/|\.be\/)([a-zA-Z0-9\-\_]{11})/)

  ; call the youtube script with the channel and the video ID, if there is one
  youtube $$regml(2) $chan
}

; alias youtube: opens a socket connection to youtube
; expects: youtube video ID as $1, #channel as $2
; returns: -
alias -l youtube {
  ; create a unique socket name to allow for multiple connections at a time
  var %sock = youtube. $+ $ticks
  sockopen %sock gdata.youtube.com 80
  ; store video ID and channel in socket mark
  sockmark %sock $1-
}

on *:SOCKOPEN:youtube.*:{
  ; obligatory error check
  if $sockerr { return }

  ; send our request
  sockwrite -n $sockname GET /feeds/mobile/videos/ $+ $gettok($sock($sockname).mark,1,32) $+ ?v=2&prettyprint=true HTTP/1.1
  sockwrite -n $sockname Host: $sock($sockname).addr
  sockwrite -n $sockname $crlf
}

on *:SOCKREAD:youtube.*:{
  if $sockerr { return }

  ; receive answer to the request into a variable
  var %t = $sockname, %data
  sockread %data

  ; while there's bytes in the sockread buffer...
  while $sockbr {

    ; extract things of interest out of the source code into a hash table
    if (*<title>*</title>* iswm %data) { hadd -m %t title $regsubex(%data,/(.*<title>|</title>.*)/g,) }
    if (*<yt:duration seconds='*'/>* iswm %data) { hadd -m %t dur $duration($regsubex(%data,/(.*seconds='|'/>.*)/g,),3) }
    if (*<yt:statistics favoriteCount='*' viewCount='*'/>* iswm %data) { hadd -m %t views $regsubex(%data,/(.*viewCount='|'/>)/g,) }
    if (*<yt:rating numDislikes='*' numLikes='*'/>* iswm %data) { hadd -m %t likes $regsubex(%data,/(.*numLikes='|'/>.*)/g,) | hadd -m %t dislikes $regsubex(%data,/(.*numDislikes='|'.*)/g,) }
    if (*<name>*</name>* iswm %data) { hadd -m %t uploader $regsubex(%data,/(.*<name>|</name>.*)/g,) }
    if (*<yt:uploaded>*</yt:uploaded>* iswm %data) { hadd -m %t uploaded $left($regsubex(%data,/(.*<yt:uploaded>|</yt:uploaded>.*)/g,),10) }
    if (*accessControl action='rate' permission='denied'* iswm %data)  { hadd -m %t rating disabled or no votes }

    ; the </entry> tag marks the end of the data.
    if (*</entry>* iswm %data) || ($hget(%t,0).item == 7)  || (($hget(%t,0).item == 7) && ($hget(%t,rating) == disabled)) {
      var %ytmsg = ' $+ $hget(%t,title) $+ ' by $hget(%t,uploader) $+ $chr(44) $hget(%t,dur) $+ $chr(44) $hget(%t,views) views
      var %ytmsg = %ytmsg $+ , $iif($hget(%t,likes),rating: $round($calc($hget(%t,likes) / ($hget(%t,likes) + $hget(%t,dislikes)) * 100),1) $+ % ( $+ $hget(%t,likes) $+ / $+ $hget(%t,dislikes) $+ ),rating disabled or not rated yet)
      var %ytmsg = $replace(%ytmsg, ,$chr(160),",",',',<,<,>,>,&,&, £,$chr(163), €, $chr(8364), ©, $chr(169), &trade, $chr(8482), ®, $chr(174))                  
      msg $gettok($sock(%t).mark,2,32) %ytmsg

      sockclose %t
      hfree %t
      return
    }
    sockread %data
  }
}
Contributed by Jay2k1