Discuss Scratch

robowiko123
Scratcher
51 posts

Change cloud variables using Python

How can I access cloud variables through Python?
The /varserver API doesn't work.

Last edited by robowiko123 (Sept. 14, 2018 08:26:13)

MaverickUI
Scratcher
2 posts

Change cloud variables using Python

import json,urllib

url = "https://clouddata.scratch.mit.edu/logs?projectid=Your project id&limit=Your limit I reccomend around 10000&offset=0"

clouddata = urllib.request.urlopen(url).read()
clouddata = json.loads(clouddata)
for i in clouddata:
print(i)

Last edited by MaverickUI (Aug. 20, 2018 08:49:17)

robowiko123
Scratcher
51 posts

Change cloud variables using Python

Thanks.
robowiko123
Scratcher
51 posts

Change cloud variables using Python

But how do I change a cloud variable?
jokebookservice1
Scratcher
1000+ posts

Change cloud variables using Python

As an aside, in future, please don't change a topic title to "" once your issue is resolved. This makes it really hard to guess what the topic is about; and makes searching for it very difficult.

robowiko123
Scratcher
51 posts

Change cloud variables using Python

This is my first forum thread and I forgot how to change it back.
jokebookservice1
Scratcher
1000+ posts

Change cloud variables using Python

robowiko123 wrote:

This is my first forum thread and I forgot how to change it back.
You can just hit the “Edit” button at the bottom of your very post on this thread. It'll let you edit both your post body as well as the title.
smrman
Scratcher
1000+ posts

Change cloud variables using Python

robowiko123 wrote:

But how do I change a cloud variable?
You need to use Websocket, be logged in (cookies, token), on a page in the *.scratch.mit.edu/* domain, do a handshake with the cloud data server and send WS messages with the author, the name of the variable and the new value…

You can have a closer look in your “developers tools” window under the “networking” tab when viewing a project using cloud variables.

Here is a picture of the “network” tab from my browser on a cookie clicker project.

click on the picture to see it bigger…

Hope it helps.

Last edited by smrman (Sept. 14, 2018 13:33:03)

robowiko123
Scratcher
51 posts

Change cloud variables using Python

You need to use Websocket
Ok, I know how to do that in python.
be logged in (cookies, token), on a page in the *.scratch.mit.edu/* domain
I've logged in already.
do a handshake with the cloud data server
This is the part that I don't know how to do.
and send WS messages with the author, the name of the variable and the new value…
I think I know how to do that as well.
smrman
Scratcher
1000+ posts

Change cloud variables using Python

The thing is, you need cookie headers to open a connection with the Scratch servers.
Unfortunately, i think they are origin restricted.
(see CORS)
It means you can't create a connection from a page or a software that is not part of the scratch.mit.edu domain.
Also, i think you need in the headers a session id and a CSRF token (just a token preventing connection from another source than scratch.mit.edu domain).
I recently tried to mess around with XmlHttpRequest and some Js but just saw i needed to spoof origin to get it to work (yeah, i got disappointed too).


For the websocket handshake, it's just a simple serialized object with the “project-id”, “method” and “user” proprieties… nothing difficult.
You can see incoming (green) and outgoing (orange) WS traffic in my screenshot above…

I don't think I can help you more than just tell you to have a look the the networking tab too… it helps understand how everything works.
robowiko123
Scratcher
51 posts

Change cloud variables using Python

I tried to port the scratch-API node.js library to Python but I failed because I don't understand everything in that library.
If I would be able to port it over (i need to port it over because the Python version uses the varserver API) then I would be able to create projects that use Scratch's easy interface, and Python's many libraries.
ryadyl
Scratcher
23 posts

Change cloud variables using Python

smrman wrote:

robowiko123 wrote:

But how do I change a cloud variable?
You need to use Websocket, be logged in (cookies, token), on a page in the *.scratch.mit.edu/* domain, do a handshake with the cloud data server and send WS messages with the author, the name of the variable and the new value…

You can have a closer look in your “developers tools” window under the “networking” tab when viewing a project using cloud variables.

Here is a picture of the “network” tab from my browser on a cookie clicker project.

click on the picture to see it bigger…

Hope it helps.
What browser are you using?
apple502j
Scratcher
1000+ posts

Change cloud variables using Python

Chrome.
867158
Scratcher
29 posts

Change cloud variables using Python

I know this is a 2 month old post, but I know how to get the session id and the CSRF token:

(This works for chrome)

  1. Click on the button to the left of the https:// in the url bar
    Click on Cookies (blah cookies in use)
    Then expand scratch.mit.edu and look in the last few cookies
    Look at the contents of the cookie
smrman
Scratcher
1000+ posts

Change cloud variables using Python

Or you gan get one by making request to https://scratch.mit.edu/login
just like the browsers do, basicly
6eccd12
Scratcher
1 post

Change cloud variables using Python

Does anyone even look at this anymore? I have so many ideas for this, but I am just not capable of making something that can access and change cloud vars. Any help would be appreciated. Thanks!

Edit: I signed in to my other account when posting this, I am actually @867158

Last edited by 6eccd12 (Aug. 15, 2019 12:27:00)

867158
Scratcher
29 posts

Change cloud variables using Python

smrman wrote:

Or you gan get one by making request to https://scratch.mit.edu/login
just like the browsers do, basicly
I tried and I got the html of the scratch homepage

Edit: I looked in the network tab and It is actually https://scratch.mit.edu/accounts/login/

Last edited by 867158 (Aug. 15, 2019 18:02:05)

apple502j
Scratcher
1000+ posts

Change cloud variables using Python

You should never log into Scratch via 3rd-party tools tho
(At least wait for ST's policy)
867158
Scratcher
29 posts

Change cloud variables using Python

apple502j wrote:

You should never log into Scratch via 3rd-party tools tho
(At least wait for ST's policy)
Yah that seems like a good idea, I was just trying to get something to work because I am new to making html requests with python.

Ps: I am using the requests library in python
robowiko123
Scratcher
51 posts

Change cloud variables using Python

Thanks for all the help! I managed to create a simple “database access” Scratch project using Node.JS and Python.
https://scratch.mit.edu/projects/317357163/

Powered by DjangoBB