Advertisement
Guest User

Twitch Stream Status Checker

a guest
Oct 13th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 14.70 KB | None | 0 0
  1. ON !*:TEXT:*:#: {
  2.   tokenize 32 $strip($1-,burci)
  3.   if ($1 == !tstream) {
  4.     if (!$2) { .msg $chan [ $+ $nick $+ ]: Error, Not enough parameteres, try again and enter the username to search! | return }
  5.     twitch_stream $nick $chan $2-
  6.   }
  7. }
  8.  
  9. alias twitch_stream {
  10.   if (!$1-) { return }
  11.   var %u = https://api.twitch.tv/kraken/streams/ $+ $3
  12.   var %v = twc_ $+ $ticks
  13.   JSONOpen -ud %v %u
  14.   if (%JSONError) { .notice $1 [ $+ $1 $+ ]: Error, There was an problem while trying to connect to the twitch server, try again later! | return }
  15.   var %id = $json(%v,stream,_id)
  16.   var %viewers = $bytes($json(%v,stream,viewers),bd)
  17.   var %crdate = $replace($remove($json(%v,stream,created_at),$chr(122)),$chr(116),$chr(32))
  18.   var %ctime = $ctime(%crdate)
  19.   if (%ctime) { var %timeago = $timeago($calc($ctime - %ctime)) | var %crdate = $date(%ctime,ddd ddoo mmm yyyy HH:nn:ss) }
  20.   if (!%id) { .msg $2 [ $+ $1 $+ ]: The $qt( $+ $3 $+ ) stream channel is currently 4OFFLINE. }
  21.   elseif (%id) { .msg $2 0,6Twitch: -> Stream: 2 $+ $3 $+  -=- Status: 3Online -=- Viewer(s): 9 $+ $iif(%viewers,$v1,N/A) $+  -=- Created on: 10 $+ $iif(%crdate,$v1,N/A) $+  (7 $+ $iif(%timeago,$v1,N/A) $+ ) }
  22.   JSONClose %v
  23. }
  24.  
  25. alias timeago {
  26.   if (!$1) { return 0 }
  27.   if ($1 <= 59) { Return $1 seconds ago }
  28.   if (($1 <= 3599) && ($1 > 59)) { return $floor($calc($1 / 60)) minutes ago }
  29.   if (($1 <= 86399) && ($1 > 3599)) { return $round($calc($1 / 3600),0) hours ago }
  30.   if (($1 <= 2592000) && ($1 > 86399)) { return $floor($calc($1 / 86400)) $iif($floor($calc($1 / 86400)) > 1,days,day) $+ , $round($calc($calc($1 - $calc($floor($calc($1 / 86400))*86400)) / 3600),0) hour(s) ago }
  31.   if (($1 <= 31540000) && ($1 > 2592000)) { return $floor($calc($1 / 2592000)) $iif($floor($calc($1 / 2592000)) > 1,months,month) $+ , $round($calc($calc($1 - $calc($floor($calc($1 / 2592000))*2592000)) / 86400),0) day(s) ago }
  32.   if ($1 > 31104000) { return $floor($calc($1 / 31104000)) $iif($floor($calc($1 / 31104000)) > 1,years,year) $+ , $round($calc($calc($1 - $calc($floor($calc($1 / 31104000))*31104000)) / 2592000),0) month(s) ago }
  33. }
  34.  
  35. alias JSONOpen {
  36.   var %switches = -
  37.   if (-* iswm $1) {
  38.     %switches = $1
  39.     tokenize 32 $2-
  40.   }
  41.   var %com = JSONHandler:: $+ $1, %error, %file, %rem
  42.   var %init = function init(a,b){errortext="";data="";url=b ? b : "";method="GET";headers=[];parsedJSON={};status=a;fso=new ActiveXObject("Scripting.FileSystemObject")}
  43.   var %read = function readFile(filename){var ado=new ActiveXObject("ADODB.Stream");ado.CharSet="utf-8";ado.Open();ado.LoadFromFile(filename);if(!ado.EOF){data=ado.ReadText();}ado.close()}
  44.   var %json = "object"!==typeof JSON&&(JSON={});(function(){function m(a){return 10>a?"0"+a:a}function t(a){p.lastIndex=0;return p.test(a)?'"'+a.replace(p,function(a){var c=u[a];return"string"===typeof c?c:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function q(a,l){var c,d,h,r,g=e,f,b=l[a];b&&"object"===typeof b&&"function"===typeof b.toJSON&&(b=b.toJSON(a));"function"===typeof k&&(b=k.call(l,a,b));switch(typeof b){case "string":return t(b);case "number":return isFinite(b)?String(b):"null";case "boolean":case "null":return String(b);case "object":if(!b)return"null";e+=n;f=[];if("[object Array]"===Object.prototype.toString.apply(b)){r=b.length;for(c=0;c<r;c+=1)f[c]=q(c,b)||"null";h=0===f.length?"[]":e?"[\n"+e+f.join(",\n"+e)+"\n"+g+"]":"["+f.join(",")+"]";e=g;return h}if(k&&"object"===typeof k)for(r=k.length,c=0;c<r;c+=1)"string"===typeof k[c]&&(d=k[c],(h=q(d,b))&&f.push(t(d)+(e?": ":":")+h));else for(d in b)Object.prototype.hasOwnProperty.call(b,d)&&(h=q(d,b))&&f.push(t(d)+(e?": ":":")+h);h=0===f.length?"{}":e?"{\n"+e+f.join(",\n"+e)+"\n"+g+"}":"{"+f.join(",")+"}";e=g;return h}}"function"!==typeof Date.prototype.toJSON&&(Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+m(this.getUTCMonth()+1)+"-"+m(this.getUTCDate())+"T"+m(this.getUTCHours())+":"+m(this.getUTCMinutes())+":"+m(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()});var s,p,e,n,u,k;"function"!==typeof JSON.stringify&&(p=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,u={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},JSON.stringify=function(a,l,c){var d;n=e="";if("number"===typeof c)for(d=0;d<c;d+=1)n+=" ";else"string"===typeof c&&(n=c);if((k=l)&&"function"!==typeof l&&("object"!==typeof l||"number"!==typeof l.length))throw Error("JSON.stringify");return q("",{"":a})});"function"!==typeof JSON.parse&&(s=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,JSON.parse=function(a,e){function c(a,d){var g,f,b=a[d];if(b&&"object"===typeof b)for(g in b)Object.prototype.hasOwnProperty.call(b,g)&&(f=c(b,g),void 0!==f?b[g]=f:delete b[g]);return e.call(a,d,b)}var d;a=String(a);s.lastIndex=0;s.test(a)&&(a=a.replace(s,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return d=eval("("+a+")"),"function"===typeof e?c({"":d},""):d;throw new SyntaxError("JSON.parse");})})();
  45.   var %xhr = function xhr(){var a,b=['MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.3.0','Microsoft.XMLHTTP'],c;for(a=0;a<b.length;a++){try{c=new ActiveXObject(b[a]);break}catch(e){}}if(typeof c==="undefined"){errortext = "Unable to locate an XMLHTTP object for use"}else{c.open(method,url,false);for(a=0;a<headers.length;a++){c.setRequestHeader(headers[a][0],headers[a][1])}c.send(data);if(c.status!==200){errortext="Unable to retrieve data - Server Error: " + c.status}else{status="Parsed";try{parsedJSON=JSON.parse(c.responseText)}catch(ee){errortext="Unable to parse Retrieved Data"}}}}
  46.   if (!$regex(%switches, /^-[ufbwd]*$/)) { %error = Invalid switches specified }
  47.   elseif ($regex(%switches, /([ufbwd])\1/)) { %error = Duplicate switch specified ( $+ $regml(1) $+ ) }
  48.   elseif ($regex(%switches, /([ufb])/g) > 1) { %error = Conflicting switches specified (- $+ $regml(1) $+ , - $+ $regml(2) $+ $iif($regml(3), $chr(44) - $+ $v1) $+ ) }
  49.   elseif (w isin %switches && u !isin %switches) { %error = Invalid switch (-w is only for use with -u) }
  50.   elseif ($0 < 2) { %error = Required parameters missing }
  51.   elseif (!$regex($1,/^[a-z\d_.-]+$/i)) { %error = Invalid name specified (Can only contant letters, numbers, _, ., and -) }
  52.   elseif ($com(%com)) { %error = Specified name in use }
  53.   elseif (u isin %switches && $0 > 2) { %error = Invalid URL specified. (Contains Spaces: $2-) }
  54.   elseif (f isin %switches && !$isfile($2-)) { %error = File not found: $2- }
  55.   elseif (b isin %switches && &* !iswm $2) { %error = Invalid binary variable specified (No '&' Prefix) }
  56.   elseif (b isin %switches && $0 > 2) { %error = Invalid binary variable specified (Contains Spaces: $2-) }
  57.   elseif (b isin %switches && $bvar($2, 0) < 1) { %error = Invalid binary variable specified (No content) }
  58.   elseif (!$JSON::ComOpen(%com, MSScriptControl.ScriptControl) || $comerr) { %error = Unable to create an instance of MSScriptControl.ScriptControl }
  59.   elseif (!$com(%com, language, 4, bstr, jscript) || $comerr) { %error = Unable to set ScriptControl's language to Javascript }
  60.   elseif (!$com(%com, addcode, 1, bstr, %init) || $comerr) { %error = Unable to add init() to ScriptControl }
  61.   elseif (!$com(%com, addcode, 1, bstr, %xhr) || $comerr) { %error = Unable to add xhr() to ScriptControl }
  62.   elseif (!$com(%com, addcode, 1, bstr, %read) || $comerr) { %error = Unable to add readFile() to ScriptControl }
  63.   elseif (!$com(%com, ExecuteStatement, 1, bstr, %JSON) || $comerr) { %error = Unable to Initalize the JSON parser object }
  64.   elseif (!$com(%com, ExecuteStatement, 1, bstr, $iif(u isin %switches, $+(init,$chr(40), "XHR", $chr(44), $qt($JSON::Escape($2)),$chr(41)), $+(init,$chr(40), "Parsed", $chr(44), "", $chr(41)))) || $comerr) { %error = Unable to initalize variables for ScriptControl }
  65.   else {
  66.     if (u isin %switches) {
  67.       if (w !isin %switches) {
  68.         var %i = $com(%com, ExecuteStatement, 1, bstr, $+(xhr,$chr(40),$chr(41)))
  69.         if (!%i || $comerr) { %error = Unable to retrieve data from $2 :: $com(%com).error :: $com(%com).errortext }
  70.         elseif (!$com(%com, eval, 1, bstr, errortext) || $com(%com).result) { %error = Unable to retrieve data $+ $iif($v1,: $v1,) }
  71.       }
  72.     }
  73.     else {
  74.       %file = $JSON::File($1)
  75.       if (f isin %switches) {
  76.         %file = $longfn($2)
  77.       }
  78.       elseif (b isin %switches) {
  79.         bwrite $qt(%file) 0 -1 $2
  80.         %rem = $true
  81.       }
  82.       else {
  83.         write -n $qt(%file) $2-
  84.         %rem = $true
  85.       }
  86.       if (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(readFile, $qt($JSON::Escape(%file)))) || $comerr) { %error = Unable to read contents of data-passing file }
  87.       elseif (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(parsedJSON=JSON.parse,data)) || $comerr) { %error = Unable to parse data into valid JSON }
  88.     }
  89.     if (d isin %switches) { $+(.timer,%com) 1 0 JSONClose $1 }
  90.   }
  91.   :error
  92.   %error = $iif($error, $v1, %error)
  93.   if (%rem && %file && $isfile(%file)) { .remove $qt(%file) }
  94.   if (%error) {
  95.     if ($timer(%com)) { $+(.timer,%com) off }
  96.     if ($com(%com)) {
  97.       set -eu0 $+(%,%com,::error) %error
  98.       .comclose $v1
  99.     }
  100.     else { set -eu0 %JSONError %error }
  101.   }
  102. }
  103. alias JSONURLOption {
  104.   var %com = JSONHandler:: $+ $1, %error, %head, %value, %x = 2
  105.  
  106.   if (!$com(%com)) { return }
  107.   unset % [ $+ [ %com ] $+ ] ::error
  108.   if ($0 < 3) { %error = Missing parameters }
  109.   elseif (!$com(%com, eval, 1, bstr, status) || $com(%com).result != XHR) { %error = HTTP Request already completed or wasn't specified }
  110.   elseif ($2 == method) {
  111.     if (!$regex($3-, /^(?:GET|POST|PUT|DELETE)$/i)) { %error = Invalid HTTP Request method Specified: $3- }
  112.     elseif (!$com(%com, ExecuteStatement, 1, bstr, method=" $+ $3 $+ ") || $comerr) { %error = Unable to set HTTP Request method }
  113.   }
  114.   else {
  115.     if (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(headers.push,[ $+ $qt($JSON::Escape($2)), $qt($JSON::Escape($3-)) $+ ])) || $comerr) { %error = Unable to set HTTP Header: %head $+ : $+ %value }
  116.   }
  117.   :error
  118.   %error = $iif($error, $v1, %error)
  119.   if (%error) {
  120.     reseterror
  121.     set -e $+(%,%com,::Error) %error
  122.   }
  123. }
  124. alias JSONGet {
  125.   var %switches -
  126.   if (-* iswm $1) {
  127.     %switches = $1
  128.     tokenize 32 $2-
  129.   }
  130.   var %com = JSONHandler:: $+ $1
  131.   var %file = $JSON::File($1)
  132.   var %error
  133.   var %rem
  134.   if ($com(%com)) {
  135.     if ($com(%com, eval, 1, bstr, status) && $com(%com).result !== XHR) { %error = HTTP Request already completed or wasn't specified }
  136.     elseif (!$regex(%switches, /^-[bf]*$/)) { %error = Invalid switches specified }
  137.     elseif ($regex(%switches, /([bf])\1/)) { %error = Duplicate switch specified ( $+ $regml(1) $+ ) }
  138.     elseif ($regex(%switches, /([bf])/g) > 1) { %error = Conflicting switches specified (- $+ $regml(1) $+ , - $+ $regml(2) $+ ) }
  139.     elseif (b isin %switches && &* !iswm $2) { %error = Invalid binary variable specified (No '&' Prefix) }
  140.     elseif (b isin %switches && $0 > 2) { %error = Invalid binary variable specified (Contains Spaces: $2-) }
  141.     elseif (f isin %switches && !$isfile($2-)) { %error = File not found: $2- }
  142.     else {
  143.       if (b isin %switches && $bvar($2,0)) {
  144.         bwrite $qt(%file) 0 -1 $2
  145.         %rem = $true
  146.       }
  147.       elseif (f isin %switches) { %file = $2- }      
  148.       elseif ($2-) {
  149.         write -n $qt(%file) $2-
  150.         %rem = $true
  151.       }
  152.       %file = $longfn(%file)
  153.       if ($isfile(%file) && (!$com(%com, ExecuteStatement, 1, bstr, $JSON::Funct(readFile,$qt($JSON::Escape(%file)))) || $comerr)) { %error = Unable to pass data to JSON Handler }
  154.       elseif (!$com(%com, ExecuteStatement, 1, bstr, xhr $+ $chr(40) $+ $chr(41)) || $comerr) { %error = Unable to retrieve data from specified URL :: $com(%com).error :: $com(%com).errortext }
  155.       elseif (!$com(%com, eval, 1, bstr, errortext) || $com(%com).result) { %error = $v1 }
  156.       if (%rem && $isfile(%file)) { .remove %file }
  157.     }
  158.   }
  159.   :error
  160.   %error = $iif($error, $v1, %error)
  161.   if (%error) {
  162.     reseterror
  163.     set -eu0 $+(%,%com,::Error) %error
  164.   }
  165. }
  166. alias JSON {
  167.   var %com, %x = 1, %i = 0, %get = parsedJSON, %tok, %res
  168.   if ($regex($1,/^\d+$/) && $0 === 1) {
  169.     while ($com(%x)) {
  170.       if (JSONHandler::* iswm $v1) {
  171.         %com = $v2
  172.         inc %i
  173.         if (%i === $1) { return %com }
  174.       }
  175.       inc %x
  176.     }
  177.     return $iif($1 == 0, %i)
  178.   }
  179.   elseif ($regex($1, /^JSONHandler::CHILD::([^:]+)::(.*)$/)) {
  180.     %com = $regml(1)
  181.     %get = $regml(2)
  182.   }
  183.   else { %com = JSONHandler:: $+ $1 }
  184.   if ($com(%com)) {
  185.     if ($0 == 1) {
  186.       if ($prop == isChild) { return $iif($regex($1, /^JSONHandler::CHILD::([^:]+)::(.*)$/), $true, $false) }
  187.       elseif ($prop == error) { return $iif($(,$+(%,%com,::error)), $true,$false) }
  188.       elseif ($prop == errortext) { return $(,$+(%,%com,::error)) }
  189.       elseif ($com($1)) { return $1 }
  190.     }
  191.     elseif (!$com(%com, eval, 1, bstr, status) || $com(%com).result != parsed) { set -eu0 $+(%,%com,::error) JSON Handler waiting for HTTP Request }
  192.     else {
  193.       %x = 2
  194.       while (%x <= $0) {
  195.         %tok = $(,$ $+ %x)
  196.         if (!$regex(%tok, /^\d+$/)) {
  197.           %tok = $qt($replace(%tok,\,\\,",\"))
  198.         }
  199.         %get = $+(%get,[,%tok,])
  200.         inc %x
  201.       }
  202.       if (!$com(%com, eval, 1, bstr, %get) || $comerr) { set -e $+(%,%com,::error) Invalid Item|index specified }
  203.       else {
  204.         %res = $com(%com).result
  205.         if (%res == [object]) { return JSONHandler::CHILD:: $+ $1 $+ :: $+ %get }
  206.         else { return %res }
  207.       }
  208.     }
  209.   }
  210. }
  211. alias JSONClose {
  212.   var %com = JSONHandler:: $+ $1
  213.   unset % [ $+ [ %com ] $+ ] ::*
  214.   if ($com(%com)) { .comclose $v1 }
  215.   if ($timer(%com)) { $+(.timer,%com) off }
  216. }
  217. alias JSONList {
  218.   var %x = 1, %i = 0
  219.   while ($com(%x)) {
  220.     if (JSONHandler::* iswm $v1) {
  221.       inc %i
  222.       echo $color(info text) -a * # $+ %i : $regsubex($v2, /^JSONHandler::/,)
  223.     }
  224.     inc %x
  225.   }
  226.   if (!%i) { echo $color(info text) -a * No active JSON handlers }  
  227. }
  228. alias JSON::ComOpen { .comopen $1- | if ($com($1) && !$comerr) { return $true } | :error | reseterror | if ($com($1)) { .comclose $1 } }
  229. alias JSON::Escape { return $replace($1,\,\\,",\") }
  230. alias JSON::File { var %a = 1 | while ($isfile(JSON $+ $1 $+ %a $+ .json)) { inc %a } | return $+(JSON, $1, %a, .json) }
  231. alias JSON::Funct { var %a = $1 $+ $chr(40), %i = 1 | while (%i < $0) { inc %i | %a = $+(%a,$iif(%i > 2,$chr(44)),$(, $ $+ %i)) } | return %a $+ $chr(41) }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement