; YouTube v5.2 - (2/20/21) by entropy 2021
; You will need a GOOGLE API (Youtube) Key for the youtube search to work.
; Get one here: https://developers.google.com/youtube/v3/getting-started
; For channels, you can use "#" as all channels, or a list of channels: #test,#test2,#test3 etc.
; ============================ YouTube Events
#youtube on
on *:input:%yt_chans:{ if (/* !iswm $1 && $server) { triggeryt # $strip($1-) } }
on *:text:*:%yt_chans:{ triggeryt # $strip($1-) }
on *:action:*:%yt_chans:{ triggeryt # $strip($1-) }
#youtube end
; ============================ YouTube Menus
menu channel,status,menubar {
-
YouTube v5.2
.YouTube Status $chr(9) $replace($group(#youtube).status,o,O) {
$iif($group(#youtube).status == on,.disable,.enable) #youtube
echo 4 -a * YouTube is now: $replace($group(#youtube).status,o,O)
}
.-
.Channels $chr(9) %yt_chans { %yt_chans = $input(Channels (sep by comma)?,5,Channels,%yt_chans) | if ($! != $null) { %yt_chans = $! } }
.-
.API Key $chr(9) %yt_key { var %a = $input(Input API Key?,5) | if ($! != $null) { %yt_key = $! } }
-
}
; ============================ Lookup Aliases
on *:unload:{
unset %yt_chans %yt_key
}
alias -l triggeryt {
if ($wildtok($2-,*https://www.youtube.com/watch?v=*,1,32)) { you $1 $gettok($v1,2,61) }
elseif ($wildtok($2-,*https://youtu.be/*,1,32)) { you $1 $gettok($v1,-1,47) }
elseif (?yt iswm $2 && $left($2,1) isin @!. && $3) { ytsearch $1 $3- }
}
alias -l you {
sockclose youtube
sockopen -e46 youtube youtube.com 443
sockmark youtube $replace($2-,$chr(32),+)
%_title = ""
%_length = ""
%_views = ""
%_likes = ""
%_dislikes = ""
%_chan = $1
%_nouser = ""
}
;============================= YouTube Play
on *:sockopen:youtube:{
if ($sockerr) { return }
sockwrite -nt $sockname GET https://www.youtube.com/watch?v= $+ $sock($sockname).mark HTTP/1.1
sockwrite -nt $sockname Host: youtube.com
sockwrite -nt $sockname Connection: Close
sockwrite -nt $sockname Accept-Language: en-us
sockwrite -nt $sockname Content-Type: text/html; charset=utf-8
sockwrite -nt $sockname $str($crlf,2)
}
on *:sockread:youtube:{
if ($sockerr) { return }
sockread -f &b
if (* views"* iswm $bvar(&b,1-).text) {
var %a = $bvar(&b,1-).text, %b = 1, %y
while ($gettok(%a,%b,123)) {
%y = $v1
if (*"status":"LOGIN_REQUIRED"* iswm %y) { %_nouser = $ctime }
if (*"text":"*"}]},"viewCount":* iswm %y) { %_title = $gettok(%y,3,34) }
if (*"simpleText":"* views"},"shortViewCount":* iswm %y && !%_views) { %_views = $gettok($gettok(%y,3,34),1,32) }
inc %b
}
}
if (*"label":"like this video along with * other people"}* iswm $bvar(&b,1-).text && %_likes == $null) {
var %y = $replace($bvar(&b,1-).text,:,$+($chr(32),:,$chr(32)),}},$+($chr(32),}})), %first = $findtok(%y,"like,1,32), %last = $findtok(%y,people",1,32), %all = $gettok($gettok(%y,$+(%first,-,%last),32),6,32)
if ($remove(%all,$chr(44)) isnum) { %_likes = %all }
}
if (*"label":"dislike this video along with * other people"}* iswm $bvar(&b,1-).text && %_dislikes == $null) {
var %y = $replace($bvar(&b,1-).text,:,$+($chr(32),:,$chr(32)),}},$+($chr(32),}})), %first = $findtok(%y,"dislike,1,32), %last = $findtok(%y,people",1,32), %all = $gettok($gettok(%y,$+(%first,-,%last),32),6,32)
if ($remove(%all,$chr(44)) isnum) { %_dislikes = %all }
}
if (*"lengthSeconds":"* iswm $bvar(&b,1-).text && !%_length) {
var %a = 1, %b, %c = $bvar(&b,1-).text
while ($gettok(%c,%a,44)) { %b = $v1 | if (*"lengthSeconds":"*"* iswm %b) { %_length = $duration($remove(%b,"lengthSeconds":","),3) | break } | inc %a }
}
}
on *:sockclose:youtube:{ endit }
alias -l endit {
if (!%_title) { msg %_chan [YouTube] $c No such video! }
elseif (%_nouser) { msg %_chan [YouTube] $c Video flagged as innopropriate for some users }
else { msg %_chan [YouTube] $c Title: $replace(%_title,',',",",&,&,\u0026,&) $c Views: %_views $c Length: $iif(%_length,%_length,NA) $c Up: $iif(%_likes != $null,%_likes,NA) $c Down: $iif(%_dislikes != $null,%_dislikes,NA) }
unset %_chan %_title %_views %_length %_likes %_dislikes %_nouser
}
alias -l nohtml { return $regsubex($1, /<[^>]+(?:>|$)|^[^<>]+>/g,) }
alias -l c { return $chr(9679) }
;============================= YouTube Search
alias -l ytsearch {
sockclose yout
sockopen -e46 yout googleapis.com 443
set -u5 %_chan $1
sockmark yout $replace($2-,$chr(32),+)
}
on *:sockopen:yout:{
if ($sockerr) { return }
sockwrite -nt $sockname GET $+(https://www.googleapis.com/youtube/v3/search?part=snippet&q=,$sock($sockname).mark,&key=,%yt_key) HTTP/1.1
sockwrite -nt $sockname Host: googleapis.com
sockwrite -nt $sockname Connection: Close
sockwrite -nt $sockname Accept-Language: en-us
sockwrite -nt $sockname Content-Type: text/html; charset=utf-8
sockwrite -nt $sockname $str($crlf,2)
}
on *:sockread:yout:{
if ($sockerr) { return }
var %a
sockread -f %a
tokenize 32 %a
if ($1) {
if (*"reason": "keyInvalid"* iswm $1- || !%yt_key) { msg %_chan [YouTube Search] $c Invalid Key! | sockclose $sockname | return }
if (*"title": "*",* iswm $1-) { set -u5 %_title $replace($remove($gettok($1-,2-,32),',$chr(44)),',',',',",",&,&,",$null,\u0026,&) }
if (*"url": "*",* iswm $1-) { set -u5 %_link $+(https://www.youtube.com/watch?v=,$gettok($gettok($1-,2,32),-2,47)) | msg %_chan [YouTube Search] $c %_title $c Link: %_link | sockclose $sockname }
}
}