Sync with online DB

SoulSpirit
Posts: 17
Joined: Wed Aug 22, 2018 1:16 pm

Sync with online DB

Post by SoulSpirit »

Hi,
I'm developing a set of plugins for the MUD I play on, and sharing them to all the other players.
I'm also maintaining some online tables (made with Google Sheets) automatically updated by a form (made with Google Form) that any player can fill by copy/pasting the MUD output (for object identifications and such).

I'd like to develop a couple of new plugins for Mudlet in order to:
- trigger an HTTP/REST/ANYTHING request every time an object is identified by a player (to update the Google Sheets database)
- download data from an online database every now and then (possibly, my Google Sheet database converted in a LUA DB fashioned file or similar and published on Google Drive) to enrich the MUD output with data contained in that database (I.E. to show the player enemy immunities every time he faces it)

I looked at Mudlet's documentation, but I haven't found anything related to HTTP requests or files download/upload.
Any suggestion?

Thanks.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Sync with online DB

Post by Vadi »


SoulSpirit
Posts: 17
Joined: Wed Aug 22, 2018 1:16 pm

Re: Sync with online DB

Post by SoulSpirit »

Thanks for the advice.
I was experimenting with it but I've just noticed that "downloadFile()" doesn't follow redirects (HTTP 302).
This is a problem with Google Drive, since every download is subject to one or more redirects, and the real download link is not reliable!
Any idea?

Thanks.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Sync with online DB

Post by Vadi »

Are you using a recent Mudlet version? I think we enabled it to follow redirects.

SoulSpirit
Posts: 17
Joined: Wed Aug 22, 2018 1:16 pm

Re: Sync with online DB

Post by SoulSpirit »

I'm on the latest 3.12.0 version.

I solved this way, even if I don't like it very much:

Code: Select all

local handlerId = registerAnonymousEventHandler( "sysDownloadDone",
	function(_, filename)
		local f, s, result = io.open(filename)
		if f then result = f:read("*a"); io.close(f) end
		if result:find("The document has moved",1,true) ~= nil then
			local redirectUrl = result:match([[The document has moved <A HREF="(.+)">here</A>]])
			downloadFile( filename, redirectUrl )
		else
			killAnonymousEventHandler(handlerId)
		end
	end,
	true
)

SoulSpirit
Posts: 17
Joined: Wed Aug 22, 2018 1:16 pm

Re: Sync with online DB

Post by SoulSpirit »

Anything about calling an HTTP service to upload data?

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Sync with online DB

Post by Vadi »

With GET and parameters, yep. POST/PUT isn't supported out of the box.

SoulSpirit
Posts: 17
Joined: Wed Aug 22, 2018 1:16 pm

Re: Sync with online DB

Post by SoulSpirit »

Vadi wrote:
Sun Aug 26, 2018 1:26 pm
With GET and parameters, yep. POST/PUT isn't supported out of the box.
How? I only found openUrl(), but the docs says it opens a browser window.
Should I use downloadFile() ?

Thanks

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Sync with online DB

Post by Vadi »

Yep

SoulSpirit
Posts: 17
Joined: Wed Aug 22, 2018 1:16 pm

Re: Sync with online DB

Post by SoulSpirit »

SoulSpirit wrote:
Fri Aug 24, 2018 10:21 am

Code: Select all

local handlerId = registerAnonymousEventHandler( "sysDownloadDone",
	function(_, filename)
		local f, s, result = io.open(filename)
		if f then result = f:read("*a"); io.close(f) end
		if result:find("The document has moved",1,true) ~= nil then
			local redirectUrl = result:match([[The document has moved <A HREF="(.+)">here</A>]])
			downloadFile( filename, redirectUrl )
		else
			killAnonymousEventHandler(handlerId)
		end
	end,
	true
)
Hi,
the workaround I thought I found to follow HTTP 302 redirects isn't reliable and doesn't work very well...
I tried different things, like using the nested downloadFile within a tempTimer or registering a named event handler instead of an anonymous one, but in any case the inner downloadFile isn't executed and no error is generated (even registering a sysDownloadError event).

I'm now running Mudlet 3.13.0.

Any suggestion?

You can use this URL to test redirects:
https://script.google.com/macros/s/AKfy ... eocri/exec

Thanks

Post Reply