SHARE
TWEET

Untitled

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