Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Mixsel
Wanderer


Joined: 02 Feb 2003
Posts: 99
Location: Seattle, Wa

PostPosted: Mon May 27, 2013 5:00 pm   

Can Cmudpro do this?
 
Heyas all,

I am trying to wrap my head around API's json and how to use CmudPro in order to play with GuildWars 2 recently released API's

Can I use Cmudpro to grab the files from https://api.guildwars2.com/v1/recipes.json and manipulate them with Cmudpro?

i'm currently taking a crash course via wiki and other sources to try and learn the basics hehe.

but anyways; is this possible?
_________________
Spin
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: Mon May 27, 2013 5:22 pm   
 
%url should be able to
_________________
Discord: Shalimarwildcat
Reply with quote
Daern
Sorcerer


Joined: 15 Apr 2011
Posts: 809

PostPosted: Mon May 27, 2013 5:51 pm   
 
While %url would work to pull the data from the site, you're then left with a bunch of JSON data in a string, and you'd need to write your own parser to do anything with it. I'd recommend using Lua for this instead of zScript, there are several existing JSON libraries for Lua that can do the heavy lifting for you.
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Thu Jun 06, 2013 4:08 am   
 
The following script will create a dbrecord: KEY = "recipes", VALUE = stringlist of values.
Code:

require( "luacurl" ) -- this is very slow so it will appear to hang.
require( "json" )
--
c = curl.new() -- instantiate.
--
function fetch( url )

   local tab = {}
   
   local function get( _, buffer )
   
            table.insert( tab, buffer ) -- read a chunk of data.

            return #buffer -- return size of chuck for progress display.
          
        end

   local function prog( _, dltotal, dlnow )
   
            print( dltotal, dlnow ) -- display download progress.
          
         end

   c:setopt( curl.OPT_WRITEFUNCTION, get ) -- set read function

   c:setopt( curl.OPT_PROGRESSFUNCTION, prog ) -- set progress display function

   c:setopt( curl.OPT_NOPROGRESS, false )

-- the following 3 lines of code are required if you are fetching using https:

   c:setopt( curl.OPT_SSLENGINE_DEFAULT, false )

   c:setopt( curl.OPT_SSL_VERIFYPEER, false )

   c:setopt( curl.OPT_SSL_VERIFYHOST, 0 )
--

   c:setopt( curl.OPT_HTTPHEADER, "Connection: Keep-Alive", "Accept-Language: en-us" )

   c:setopt( curl.OPT_URL, url ) -- set web page locator.

   c:setopt( curl.OPT_CONNECTTIMEOUT, 15 )

   c:perform()

   return table.concat( tab )

end

zs.var.result = json.decode( fetch( "https://api.guildwars2.com/v1/recipes.json" ) ) -- fetch data from network.
_________________
Sic itur ad astra.
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Fri Jun 07, 2013 3:38 am   
 
As it turns out, there is no need to use the lua json services. The CMUD %json function does the job even better because it doesn't force a null value as the first entry of the value string list. We can remove the require( "json" ) and change the function call as follows:
From this:
Code:

zs.var.result = json.decode( fetch( "https://api.guildwars2.com/v1/recipes.json" ) )

To this:
Code:

zs.var.result = zs.func.json( fetch( "https://api.guildwars2.com/v1/recipes.json" ) )
_________________
Sic itur ad astra.
Reply with quote
Tarn
GURU


Joined: 10 Oct 2000
Posts: 867
Location: USA

PostPosted: Sun Jun 16, 2013 11:23 pm   
 
Anaristos wrote:
As it turns out, there is no need to use the lua json services. The CMUD %json function does the job even better because it doesn't force a null value as the first entry of the value string list. We can remove the require( "json" ) and change the function call as follows:
From this:
Code:

zs.var.result = json.decode( fetch( "https://api.guildwars2.com/v1/recipes.json" ) )

To this:
Code:

zs.var.result = zs.func.json( fetch( "https://api.guildwars2.com/v1/recipes.json" ) )


I was going to say that %url can handle the download and you wouldn't need lua at all, but CMUD seems to be getting stuck on the %url call because of either https instead of http or because the link redirects- I'm not sure which but think it's probably the second.
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Mon Jun 17, 2013 6:06 am   
 
I did not use %url to read the data. The code I posted is complete. If you put it into a text file with extention .lua and execute it from the CMUD Lua command line using dofile, a variable will be created (result) containing a db record representing the imported json string.
I repeat the code here:
Code:

-- Code edited to use the CMUD %json function instead of Lua's json API.

require( "luacurl" ) -- this is very slow so it will appear to hang.
--
c = curl.new() -- instantiate.
--
function fetch( url )

   local tab = {}
   
   local function get( _, buffer )
   
            table.insert( tab, buffer )
             return #buffer
          
        end

   local function prog( _, dltotal, dlnow )
   
            print( dltotal, dlnow )
          
         end

   c:setopt( curl.OPT_WRITEFUNCTION, get )

   c:setopt( curl.OPT_PROGRESSFUNCTION, prog )

   c:setopt( curl.OPT_NOPROGRESS, false )

-- the following 3 lines of code are required if you are fetching using https:

   c:setopt( curl.OPT_SSLENGINE_DEFAULT, false )

   c:setopt( curl.OPT_SSL_VERIFYPEER, false )

   c:setopt( curl.OPT_SSL_VERIFYHOST, 0 )
--

   c:setopt( curl.OPT_HTTPHEADER, "Connection: Keep-Alive", "Accept-Language: en-us" )

   c:setopt( curl.OPT_URL, url )

   c:setopt( curl.OPT_CONNECTTIMEOUT, 15 )

   c:perform()

   return table.concat( tab )

end

zs.var.result = zs.func.json( fetch( "https://api.guildwars2.com/v1/recipes.json" ) )

Note that this code already takes care of the security concerns when reading https.

The only real system requirement is that C:\Program Files (x86)\Lua\5.1\clibs be part of the LUA_CPATH system string.

Also note that luacurl is very slow. So the data fetch from the network is going to take a noticeable amount of time. You will know that the connection has been established when lines indicating the progress of the data read will appear on your screen.

EDIT: When it comes to scripting the CMUD client, it is not written in stone that you must use zcript. If that were the case Zugg would not have provided a set of languages to chose from.
_________________
Sic itur ad astra.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net