Retreiving presets from remote webserver
I have a patch that tracks changes to about 100 integer values that define the state of my application. I keep these in a coll and a user can write the coll to disk to save a re-callable "preset".
I would like the ability to also store presets on a webserver so that everyone with internet access can dynamically load them. Ideally my patch could create a list of available presets and then let you select from the list at will and parse it on the fly, loading it into the coll and setting all the vars in the patch the same way that I do it when reading the file from disk.
The presets are now just text files made with the write command to the coll but I could re-engineer the whole thing to use JSON or XML or YAML. I spent some time looking at dict and I understand how I can serialize the data, send it over a network to a remote computer that can deserialize the data, but thats not what I’m looking for. So can anyone tell me a straightforward way to:
1. Read the file list of a directory on remote webserver (my patch will specify this directory internally).
2. Load the contents of a particular file in this directory into my patch in some parse-able format.
I also looked into jit.updl. I don’t want to actually download the file (and then have to open it) but it seems I can load it’s contents directly into my patch using the matrix attribute. If this is the way to do this then I will research further but it seems like a clumsy way to parse 100 key:value pairs!
I feel like I’m missing something easy and obvious thats been done hundreds of times before, but I couldn’t find anything on the forum. If anyone can steer me in the right direction I would greatly appreciate it!
I went through the same reflections when coding the Max Package Downloader : https://github.com/natcl/max_package_downloader
I was was also blocked at the jit.uldl step and the data loaded in a matrix. What I ended up doing is downloading the file, then loading it in the dict object, that worked fairly well. I then coded some file management in java (mxj) to rename and delete files.
If you want to avoid downloading the file, you could do the whole thing in java with the json library, that way you would need a single object to do this all. You can have a look at my Java code in the Max Package Downloader, you will find code that reads a json file from the web :
Actually I might have something ready made for you, back in a few minutes.
You can put in in your ~/Documents/Max/Packages folder
You can specify an url as argument and when banged it will output the content of the url out the first outlet. You can then pass that to a dict object using dict.deserialize (there’s a help file with example)
Couldn’t you just use [text] to load it and then parse it line by line? Assuming the [coll] file is formatted so that you can easily identify which values go to which number boxes, it should be straightforward. You’re only "sort of" downloading the file, probably is in a temp directory somewhere, but everything could be done with one click. This would mean you can keep using [coll].
Thanks for the reply and a solution! I got it to work after a bit of tinkering (I’ve never used Max packages before ;)). At first I pointed it to the straightforward .txt file made from the [coll] but that crashed Max. I then tried using a [dict] and pulling the data from the [coll] and exporting it as a .json file and that worked like a charm, though I’m still wondering why pulling from a [coll] makes each of my values (single integers) into single element arrays…
My only problem is that I was hoping not to require my users to have java installed. Since this distributed runtime also uses Jitter, I’m imaging potential users giving up before going through the trouble of installing quicktime *and* java, a 20 minute roadblock to seeing the app in action.
Can [text] load text content from a remote webserver? I tried to use a URL as an argument to the read message, but that would be too good to be true ;). I think you mis-read my question: I want to give all the users of my app access to pre-made "preset" files that I store on a webserver. Parsing the file once I open it is something I’m already successfully doing with local, disk-saved, preset text files.
I keep thinking there is a solution using PHP or something (been too long since I did any web programming but I know people if I know what to ask for). Someway to send a URL with a php query where the webserver returns the data (either the whole list of presets or the content of a specific preset) in a way that Max can read it…
Ah yes, I forgot about that. Not like accessing a disk drive directly. I’d say go with jit.uldl and either get the file and store it/open it with [text], or go with the matrix attribute. Probably more straightforward for parsing.
if you want to go the hard-core way, you may try with HTTP GET-ting the file from the server and parsing it on-the-fly. Or, to load the directory structure itself, you may as well use HTTP commands. See these threads:
Thanks for the information – I actually had downloaded your library and was beginning to explore them when I saw your email! An impressive set of tools. While it does seem a bit daunting I think that I can make things easier for myself by doing most of the work on the webserver side, so my GET returns something as minimal and parsable as passable (say that 3 times really fast). I must admit I don’t know anything about how webservers return content but I do know I can put a .txt file, a .json file and a .xml file in a web directory and point to the file with my browser and Chrome anyway displays the content of the file without any problem (for example: http://www.madbutter.com/presets/test.json) – even though its not a proper html file. I tried the same "url" using your [sadam.tcpClient] object and can’t make it return the text. I tried formatting the message to request the file a little differently but I’m just shooting in the dark. Clearly a GET message doesn’t work, the webserver just returns an error message.
I wish it was as easy as sending a "www.madbutter.com/presets/test.txt" (or .json or .xml ) message to some object and having it return the content of that file! Do you know of an easy way to do this using your objects? Or am I overthinking this? Maybe I will try to make a webpage that has the text I need squirelled away somewhere in a proper html file and try reading that and parsing it using your tcp object. But first I’m going to finish reading about Giovanni Pierluigi da Palestrina (I am easily distracted).
Thanks so much for your time Adam,
"I wish it was as easy as sending a "www.madbutter.com/presets/test.txt" (or .json or .xml ) message to some object and having it return the content of that file!"
well, jit.uldl with matrix output does exactly that :)
try this small coll-formatted file on my website, called "test_remote_coll.txt":
use this message to jit.uldl:
download http://www.third-space-mind.com/test_remote_coll.txt matrix
…then parse away…you should see
1, 50 85;
2, 40 95;
3, 74 74;
So it does mean downloading, but not to disk, so maybe that’s OK for your purposes. I can appreciate not wanting them stored locally. With jit.uldl it’ll vanish upon patch close.
Note that you might need some [sprintf] magic if there are spaces in your filenames. Best to just avoid that altogether and force saving without spaces (or special characters which will mess up the URL).
Thanks Seejayjames. That was super easy! Don’t know why I got scared off [jit.uldl] – I think I saw some example that had some hairy regexp processing of the returned data and assumed the worst. Your solution is perfect for my needs.
Great to hear that it’ll work!
Yep, [regexp] can be hairy for sure, but sometimes it’s the only thing that can do what you need. Plenty of help in the docs and online to figure out its highly logical, yet seemingly inscrutable ways, if you ever need to fiddle with it…So the best bet is to start with clean data that doesn’t need any extra trickery to parse. Sounds like you’re already there.