Difference between revisions of "YouTube parser"
From Scriptwiki
(Created page with "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 i...") |
(No difference)
|
Revision as of 19:13, 10 July 2013
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=|\/\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 %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 $sockname title $regsubex(%data,/(.*<title>|</title>.*)/g,) } if (*<yt:duration seconds='*'/>* iswm %data) { hadd -m $sockname dur $duration($regsubex(%data,/(.*seconds='|'/>.*)/g,),3) } if (*<yt:statistics favoriteCount='*' viewCount='*'/>* iswm %data) { hadd -m $sockname views $regsubex(%data,/(.*viewCount='|'/>)/g,) } if (*<yt:rating numDislikes='*' numLikes='*'/>* iswm %data) { hadd -m $sockname likes $regsubex(%data,/(.*numLikes='|'/>.*)/g,) | hadd -m $sockname dislikes $regsubex(%data,/(.*numDislikes='|'.*)/g,) } if (*<name>*</name>* iswm %data) { hadd -m $sockname uploader $regsubex(%data,/(.*<name>|</name>.*)/g,) } if (*<yt:uploaded>*</yt:uploaded>* iswm %data) { hadd -m $sockname uploaded $left($regsubex(%data,/(.*<yt:uploaded>|</yt:uploaded>.*)/g,),10) } ; if we have seven items in our hash table, we have all data we need if $hget($sockname,0).item == 7 { var %t = $sockname msg $gettok($sock($sockname).mark,2,32) ' $+ $hget(%t,title) $+ ' by $hget(%t,uploader) $+ , $hget(%t,dur) $+ , $hget(%t,views) views, rating: $round($calc($hget(%t,likes) / ($hget(%t,likes) + $hget(%t,dislikes)) * 100),1) $+ % ( $+ $hget(%t,likes) $+ / $+ $hget(%t,dislikes) $+ ) sockclose %t hfree %t return } sockread %data } }
Contributed by Jay2k1 |