larskarbo / napchart

Drag-and-drop time-planning library using HTML5 and the canvas element
https://napchart.com
GNU General Public License v3.0
140 stars 36 forks source link

RESTful API #22

Closed Teteros closed 7 years ago

Teteros commented 7 years ago

Any way to generate napchart schedules programically?

I've been thinking of making a discord chat bot for the reddit polyphasic group. A user could type a text representation of a schedule and the bot would output a napchart link. (for the graphical one) Looking at the code, at the moment it only seems possible to do so with a forked, self-hosted version?

If napchart computes the chart ID (http://napchart.com/XXXXX) based on the schedule times rather than it being randomly generated, pointing to a code behind it would help a lot.

EDIT: Found it! This may be more difficult to implement than I expected though, as it involves querring the SQL database. https://github.com/larskarbo/napchart/blob/8fe511e41cbb3357d0590c0d03a2fc405caa9a58/database.js#L92-L98

larskarbo commented 7 years ago

Hi,

It sounds like a cool idea and a good way to use napchart.

There is no official rest api for napchart yet, but you could always make this work a hacky way. Have a look at /public/js/server.js. To save a new chart, you can send a post request to (napchart.com)/post with the chart data and get the chartid back.

Was this what you were looking for or hve I misunderstood?

ncnlinh commented 7 years ago

If napchart computes the chart ID (http://napchart.com/XXXXX) based on the schedule times rather than it being randomly generated

This is actually a very cool feature to have!

larskarbo commented 7 years ago

@ncnlinh @teteros Do you mean having all the chart eata in the url?

ncnlinh commented 7 years ago

@larskarbo yes, it's like somehow hash the chart data into a unique code and that code would be used the url. This way we don't even need to store anything and it can be shareable.

Teteros commented 7 years ago

@ncnlinh It's what I was after when I imagined a RESTful api. Going about it the way you described would make napchart a much portable application. Hovewer, you would lose the logging and feedback features the database brings at the moment, so it's a compromise.

I've figured out the solution to my original problem (thanks @larskarbo for your hint!) which indeed involved simply sending a post request to http://napchart.com/post

Sample request turned out to be: (I've unfortunately had to use firefox's network inspector to figure out the format of the POST request my browser is sending when saving a schedule)

data: {"busy":[{"start":540,"end":1020}],"core":[{"start":0,"end":120},{"start":240,"end":360}],"nap":[{"start":459,"end":479},{"start":1200,"end":1220}]}

For a 'busy' period, a core. and two naps, times are stored as minutes. Test implementation in python for this: https://gist.github.com/Teteros/e6b5c289d89afcb8fc41c67b574812ba So this may can be closed.

Now a seperate, but different issue is to retrieve napchart's generated chart as a direct image, because it is stored by canvas atm. I'll look into that deeply and try to figure it out later.

Thanks again @larskarbo!

larskarbo commented 7 years ago

yes, it's like somehow hash the chart data into a unique code and that code would be used the url. This way we don't even need to store anything and it can be shareable.

@ncnlinh @teteros I have thought about this too. It would make charts more portable, and they would work even if the napchart database goes down. However, there are some downsides. First of all, the urls would be much longer (maybe something like napchart.com/n120_140n980_1000c1200_100b300_600). Secondly, using the database option allows for more data like text descriptions, different colors, changes, comments and other things that can be added to napchart in the future.

So there are some pros and cons, but I also imagine both solutions could be used. Maybe a short url (database) for the front end users and a hashed url option when using it more like a rest api?

larskarbo commented 7 years ago

Now a seperate, but different issue is to retrieve napchart's generated chart as a direct image, because it is stored by canvas atm. I'll look into that deeply and try to figure it out later.

@teteros This is very easy to do on the client side. There is a function in draw.js called getImage that does this.

However, it would be far more useful to have this feature server-side. The server side of napchart is node.js so it should be possible to use the same code client and server and generate and serve a chart from the server, but I have not looked into this yet

ze- commented 7 years ago

Ah this sounds like it's coming to encompass my own recent suggestion, which I'll repaste here:

Hi, this might be beyond the reasonable scope of your software, as it seems to generate the images client-side?, but I think it'd be neat if there was an image hotlink api, like a url which takes arguments to specify the schedule (either or both of a saved one's hash-reference or in a schedule spec format) and then just returns the image of it, perhaps at a specified scale and maybe even with a thumbnail-optimized variation available for smaller sizes.

I'll just leave that here and subscribe to this issue, unless it's thought I should create a new one for just for it? I also do like the idea of the identifier being something like a reversible hash of the data it refers to, just to throw in an extra 2¢ :)

larskarbo commented 7 years ago

@ze- This is a great idea and definetely something we should try to make. It would also be useful for the og:image tag so that the correct chart shows up when you share a napchart link on social media.

I think it is possible by using node-canvas and share the chart libraries with the server.

larskarbo commented 7 years ago

I also do like the idea of the identifier being something like a reversible hash of the data it refers to, just to throw in an extra 2¢

Any ideas on how to do this? Start a new issue for it, and let us discuss it there

Theleruby commented 7 years ago

Hi, just some updates on this issue...

On the subject of the use of REST API for creating napcharts for the /r/polyphasic discord server: I was able to implement this feature into the Nap God bot and it's working great :)

On the subject of server-side napchart image generation: For the time being, until this is implemented, I set up a web server which can capture napcharts and turn them into images. It's at ncimg.tinytimrob.com.

So if you want an image of, say, https://napchart.com/24r3h, then you can grab an image of it by going to https://ncimg.tinytimrob.com/24r3h and you get a nice image representation of the napchart:

napchart

This might be useful to anyone currently looking for this feature right now.

larskarbo commented 7 years ago

There now exists 3 api endpoints.

Create chart: /api/create with data as post

Get chart: /api/get?chartid=xxxxx receive data

Get image: /api/getImage?chartid=xxxxx&width=600&height=600&shape=circle receive image (shape is optional)