Advertisement
Guest User

Untitled

a guest
Mar 11th, 2012
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 4.97 KB | None | 0 0
  1.  
  2. /*
  3.     $json
  4.     Written by Timi
  5.     http://timscripts.com/
  6.  
  7.     JSON, JavaScript Object Notation, is a popular format by which data is read and written.
  8.     Many sites and APIs, such as Google's AJAX APIs, use this format, as it easy to parse and
  9.     is widely supported.
  10.  
  11.     This snippet can read any value from valid JSON. It uses the MSScriptControl.ScriptControl object
  12.     to input the data where is can be parsed using JavaScript. Calls are made to this object to
  13.     retrieve the needed value.
  14.  
  15.     This script identifier also has the ability to get JSON data from a http://siteaddress.com
  16.     source. This can be done by specifying a URL beginning with http://. Files on your computer can
  17.     also be used. With URLs, the data will also be cached so multiple calls to the same data won't
  18.     require getting the data over again. The cache will be cleared every 5 mins, but it can be cleared
  19.     manually using /jsonclearcache. The URL will also be encoded automatically.
  20.  
  21.     Syntax:
  22.         $json(<valid JSON, file, or URL>,name/index,name/index2,...,name/indexN)[.count]
  23.         Note: When inputting JSON, it is recommended that a variable is used because JSON uses commas
  24.         to separate values. Also, URLs must begin with http://
  25.  
  26.         /clearjsoncache
  27.         Clears the JSON cache created when JSON is retrieved from URLs
  28.  
  29.     Examples:
  30.         var %json = {"mydata":"myvalue","mynumbers":[1,2,3,5],"mystuff":{"otherdata":{"2":"4","6":"blah"}}}
  31.  
  32.         $json(%json,mydata) == myvalue
  33.         $json(%json,mynumbers,2) == 3 ;the array is indexed from 0
  34.         $json(%json,mystuff,otherdata,"6") == blah      ;if a name is a number, quotes must be used as to not confuse
  35.                                     ;it with an array.
  36.         ----------
  37.         Google Web Search example at end.
  38.  
  39.  
  40. */
  41.  
  42. alias json {
  43.   if ($isid) {
  44.     ;name of the com interface declared so I don't have to type it over and over again :D
  45.     var %c = jsonidentifier,%x = 2,%str,%p,%v
  46.  
  47.     ;if the interface hasnt been open and initialized, do it.
  48.     if (!$com(%c)) {
  49.       .comopen %c MSScriptControl.ScriptControl
  50.       ;add two javascript functions for getting json from urls and files
  51.       noop $com(%c,language,4,bstr,jscript) $com(%c,addcode,1,bstr,function httpjson(url) $({,0) y=new ActiveXObject("Microsoft.XMLHTTP");y.open("GET",encodeURI(url),false);y.send();return y.responseText; $(},0))
  52.       noop $com(%c,addcode,1,bstr,function filejson (file) $({,0) x = new ActiveXObject("Scripting.FileSystemObject"); txt1 = x.OpenTextFile(file,1); txt2 = txt1.ReadAll(); txt1.Close(); return txt2; $(},0))
  53.       ;add function to securely evaluate json
  54.       noop $com(%c,addcode,1,bstr,function str2json (json) $({,0) return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(json.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + json + ')'); $(},0))
  55.       ;add a cache for urls
  56.       noop $com(%c,addcode,1,bstr,urlcache = {})
  57.     }
  58.     if (!$timer(jsonclearcache)) { .timerjsonclearcache -o 0 300 jsonclearcache }
  59.  
  60.     ;get the list of parameters
  61.     while (%x <= $0) {
  62.       %p = $($+($,%x),2)
  63.       if (%p == $null) { noop }
  64.       elseif (%p isnum || $qt($noqt(%p)) == %p) { %str = $+(%str,[,%p,]) }
  65.       else { %str = $+(%str,[",%p,"]) }
  66.       inc %x
  67.     }
  68.     if ($prop == count) { %str = %str $+ .length }
  69.  
  70.     ;check to see if source is file
  71.     if ($isfile($1)) {
  72.       if ($com(%c,eval,1,bstr,$+(str2json,$chr(40),filejson,$chr(40),$qt($replace($1,\,\\,;,\u003b)),$chr(41),$chr(41),%str))) { return $com(%c).result }
  73.     }
  74.     ;check to see if source is url
  75.     elseif (http://* iswm $1) {
  76.       ;if url is in cache, used cached data
  77.       if ($com(%c,eval,1,bstr,$+(str2json,$chr(40),urlcache[,$replace($qt($1),;,\u003b),],$chr(41),%str))) { return $com(%c).result }
  78.       ;otherwise, get data
  79.       elseif ($com(%c,executestatement,1,bstr,$+(urlcache[,$replace($qt($1),;,\u003b),]) = $+(httpjson,$chr(40),$qt($1),$chr(41)))) {
  80.         if ($com(%c,eval,1,bstr,$+(str2json,$chr(40),urlcache[,$replace($qt($1),;,\u003b),],$chr(41),%str))) { return $com(%c).result }
  81.       }
  82.     }
  83.     ;get data from inputted json
  84.     elseif ($com(%c,eval,1,bstr,$+(x=,$replace($1,;,\u003b),;,x,%str,;))) { return $com(%c).result }
  85.   }
  86. }
  87. alias jsonclearcache { if ($com(jsonidentifier)) { noop $com(jsonidentifier,executestatement,1,bstr,urlcache = {}) } }
  88. ;-------------------;
  89.  
  90. ;;;Basic Google Web Search Identifier;;;
  91. ;;;Only the first 8 results are retrieved;;;
  92. ;;;Syntax: $gws(<search params>,<result number>,<count|url|title|content>)
  93. ;;;Requires $json
  94. alias gws {
  95.   ;ensure a proper property (count, etc) is selected and result number is between 1 and 8
  96.   if (!$istok(count url title content,$3,32) && $2 !isnum 1-8) { return }
  97.  
  98.   var %url = http://ajax.googleapis.com/ajax/services/search/web?q= $+ $1 $+ &v=1.0&safe=active&rsz=large
  99.  
  100.   ;check to see if results were found
  101.   ;since results often come back with bolds, remove them
  102.   if ($json(%url,responseData,results,0)) { return $iif($3 == count,$json(%url,responseData,cursor,estimatedResultCount).http,$remove($json(%url,responseData,results,$calc($2 - 1),$3).http,<b>,</b>)) }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement