Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » Process automation / API - understanding the capabilities
- bhontz
-
3 posts
Process automation / API - understanding the capabilities
Howdy Scratch community!
I have a shared project (GSOC Tile Game) that I am planning on using in a STEM expo table in an activity that will combine a Raspberry Pi photo booth and this scratch project. The “GSOC Tile Game” is an emulation of the old sliding tile games. For visitors to the table that have Scratch accounts, I'd like the following to happen:
1) Provide your Scratch username / password
2) Line yourself in front of the photobooth camera (monitor will depict the sliding tile game's aspect ratio and size) and press a button to snap your picture
3) I'd use the python PyGame module to size/split the picture into individual images that would be used as new costumes for the GSOC Tile Game sprites
4) Select the GSOC Tile Game public project from the table visitor's account
5) Remix this project to the visitor's Scratch account
6) Add costumes from the image files created in step 3 to 8 of the sprites within the project
7) Run the modified project on the table visitor's account, allowing the visitor to see their image on the “tiles” of the GSOC Tile Game. (potentially renaming the project here?)
Steps 1 - 7 above should run in some form of automation. I am familiar with using APIs but I haven't yet explored the Scratch API or its wrappers. Do the steps above seem feasible with the API, or would there be another automation path that would make better sense. I am familar with HTML / Python / Javascript coding (and Scratch of course).
Thanks in advance!!
I have a shared project (GSOC Tile Game) that I am planning on using in a STEM expo table in an activity that will combine a Raspberry Pi photo booth and this scratch project. The “GSOC Tile Game” is an emulation of the old sliding tile games. For visitors to the table that have Scratch accounts, I'd like the following to happen:
1) Provide your Scratch username / password
2) Line yourself in front of the photobooth camera (monitor will depict the sliding tile game's aspect ratio and size) and press a button to snap your picture
3) I'd use the python PyGame module to size/split the picture into individual images that would be used as new costumes for the GSOC Tile Game sprites
4) Select the GSOC Tile Game public project from the table visitor's account
5) Remix this project to the visitor's Scratch account
6) Add costumes from the image files created in step 3 to 8 of the sprites within the project
7) Run the modified project on the table visitor's account, allowing the visitor to see their image on the “tiles” of the GSOC Tile Game. (potentially renaming the project here?)
Steps 1 - 7 above should run in some form of automation. I am familiar with using APIs but I haven't yet explored the Scratch API or its wrappers. Do the steps above seem feasible with the API, or would there be another automation path that would make better sense. I am familar with HTML / Python / Javascript coding (and Scratch of course).
Thanks in advance!!
- chexbox
-
100+ posts
Process automation / API - understanding the capabilities
The API is mostly just for social aspects of the website.https://en.scratch-wiki.info/wiki/Scratch_API_(2.0)
For this project, It would be easiest to just use PyGame and the PiCamera Python module to save the image. If you wanted to save it as a sb2 (or sb3) you should download the original project as a ZIP, use python to replace the images for the costumes you want to change, and edit project.json to match the filenames of the images.
For this project, It would be easiest to just use PyGame and the PiCamera Python module to save the image. If you wanted to save it as a sb2 (or sb3) you should download the original project as a ZIP, use python to replace the images for the costumes you want to change, and edit project.json to match the filenames of the images.
- jokebookservice1
-
1000+ posts
Process automation / API - understanding the capabilities
To add to chexbox's answer,
“.sb2” files are just renamed “.zip” files. Once extracted, there's a “project.json” file which stores everything about the project except for the actual assets (images, audio).
In the project.json, each asset has a hash and a number associated with it. The number is its file name, and the hash is the MD5 hash of the file. I'm not sure if the MD5 hash actually has to be correct for Scratch to load the project. If not, things are simple.
1) Decompress or parse the ZIP
2) Modify each of the image files (named 0.png, 1.png, 2.png) into the corresponding slices)
3) Possibly modify the hashes in the “project.json” file
4) Recompress the ZIP
5) Rename it to an “.sb2” if you changed its extension
As checkbox said, if you have the original, it's mostly just changing the assets out.
“.sb2” files are just renamed “.zip” files. Once extracted, there's a “project.json” file which stores everything about the project except for the actual assets (images, audio).
In the project.json, each asset has a hash and a number associated with it. The number is its file name, and the hash is the MD5 hash of the file. I'm not sure if the MD5 hash actually has to be correct for Scratch to load the project. If not, things are simple.
1) Decompress or parse the ZIP
2) Modify each of the image files (named 0.png, 1.png, 2.png) into the corresponding slices)
3) Possibly modify the hashes in the “project.json” file
4) Recompress the ZIP
5) Rename it to an “.sb2” if you changed its extension
As checkbox said, if you have the original, it's mostly just changing the assets out.

- bhontz
-
3 posts
Process automation / API - understanding the capabilities
Thanks chexbox & jokebookservice1, appreciate the quick reply and directional nudge, I feel I understand your explaination regarding the sb2/json file modification.
Was hoping that the login / remix / download / upload aspect of what I had described could be automated as well, but from your replies it wouldn't appear to be so. I'll spend some time pursuing the API link that chexbox provided.

Was hoping that the login / remix / download / upload aspect of what I had described could be automated as well, but from your replies it wouldn't appear to be so. I'll spend some time pursuing the API link that chexbox provided.

- jokebookservice1
-
1000+ posts
Process automation / API - understanding the capabilities
Oh! Check out the "scratchapi"
It has some documentation, and can handle logging in, and could probably be helpful in uploading and downloading projects, if only by handling the authentication stuff for you.
pip install scratchapi
It has some documentation, and can handle logging in, and could probably be helpful in uploading and downloading projects, if only by handling the authentication stuff for you.

- bhontz
-
3 posts
Process automation / API - understanding the capabilities
will do ! Thanks! Really like it if I could automate the loading of the modified sp2 project in some way so the table visitor could just snap their pic at the photo booth and be immediately in the scratch environment presented with their personalized project (i.e. their image serving as the costumes for the GSOC Tile Game project)
- Paddle2See
-
1000+ posts
Process automation / API - understanding the capabilities
I get nervous when I see people writing code that asks for Scratch login credentials - that's a good way to phish somebody. Perhaps a safer approach would be to upload the modified project to a generic account on Scratch and then email a link to an email address supplied by the user so that they can remix it to their own account at some later time if they want.
Last edited by Paddle2See (July 28, 2018 01:35:13)
- chexbox
-
100+ posts
Process automation / API - understanding the capabilities
It could ask for a scratch username so they can receive a link on their profile. That way, there would be no need to collect an email address. I get nervous when I see people writing code that asks for Scratch login credentials - that's a good way to phish somebody. Perhaps a safer approach would be to upload the modified project to a generic account on Scratch and then email a link to an email address supplied by the user so that they can remix it to their own account at some later time if they want.
What I would recommend is to open the modified file with the offline scratch program, and send a link/file only if they want it saved.
- bobbybee
-
1000+ posts
Process automation / API - understanding the capabilities
There's a project upload API as well – IIRC poke around in the 1.4 source if you're interested – but… yeah, it's possible a good idea to check with the Scratch Team first…
- Discussion Forums
- » Advanced Topics
-
» Process automation / API - understanding the capabilities