Discuss Scratch

ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

You can install packages on repl with the cube icon on the bar its right above the settings icon. Then install websocket-client. Here is the I dont type that slow. It slowed down for whatever reason.

Last edited by ilcheese2Test (April 8, 2021 18:21:06)

SansStudios
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

PizzasAreGreat wrote:

Chiroyce wrote:

PizzasAreGreat wrote:

I'm making GET requests to https://clouddata.scratch.mit.edu/logs?projectid=512276665&limit=1&offset=0 to detect when a user change cloud variables and sometimes there is a weird error.
Here is my code:
r = requests.get(f'https://clouddata.scratch.mit.edu/logs?projectid={PROJECT_ID}&limit=2&offset=0')
r = r.json()
The error is:
- snip -
Can you help me?
Sure! Just give me a minute ….

Here! So this should work
import requests
r = requests.get("https://clouddata.scratch.mit.edu/logs?projectid={PROJECT_ID}&limit=2&offset=0")
r = r.json()
No, it is not this problem. I did import the requests module I just forgot to include it in the post above. Here is my entire code: https://replit.com/@mdub/setting-scratch-cloud-variables-python#main.py . It is a strange glitch. It happens when a user changes the value of a cloud variable in the project but it does not happen every time.

Wouldn't it be easier to just wait until you receive data from the cloud via websockets?

You can use
ws = websocket.WebSocket()
self.ws.connect(self.WEBSOCKET_SERVER, cookie = cookie, origin = self.SERVER, enable_multithread = True) #setup example
# do handshake here
received_data = ws.recv() #after setting up a websocket.

Alternatively you can use a websocket application to call a function whenever you receive data. More info on websocket-client here.

Last edited by SansStudios (April 8, 2021 19:22:28)


scratchcloud
Click above to check it out! I've worked on it for over a year!























Just like sudden sadness comes hangin' on the breeze
ninjaMAR
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

I did @Raihan142857 code and I get no errors but the cloud variable never changes
https://replit.com/@ninjamar/scratchbridge#run.py

Also please snip
Chiroyce
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

ilcheese2Test wrote:

Could you put it in a try except block. And in the except block print the status code of the connection. Also where are you getting it is it when you set the cloud variable? Is this happening randomly or is it happening whenever your run your program.
Sure will try that..







April Fools' topics:
New Buildings in Scratch's headquarters
Give every Scratcher an M1 MacBook Air
Scratch should let users edit other Scratchers' projects
Make a statue for Jeffalo
Scratch Tech Tips™
Make a Chiroyce statue emoji


<img src=“x” onerror=“alert('XSS vulnerability discovered')”>

this is a test sentence
Chiroyce
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

ninjaMAR wrote:

I did @Raihan142857 code and I get no errors but the cloud variable never changes
https://replit.com/@ninjamar/scratchbridge#run.py

Also please snip
Make sure to use the correct username and password, it worked for me when I was using PyCharm on my machine.







April Fools' topics:
New Buildings in Scratch's headquarters
Give every Scratcher an M1 MacBook Air
Scratch should let users edit other Scratchers' projects
Make a statue for Jeffalo
Scratch Tech Tips™
Make a Chiroyce statue emoji


<img src=“x” onerror=“alert('XSS vulnerability discovered')”>

this is a test sentence
Raihan142857
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

Chiroyce wrote:

ninjaMAR wrote:

I did @Raihan142857 code and I get no errors but the cloud variable never changes
https://replit.com/@ninjamar/scratchbridge#run.py

Also please snip
Make sure to use the correct username and password, it worked for me when I was using PyCharm on my machine.
Also, if your password has a special character, you might have to escape it in the .env file

I use scratch.
GF: I'll dump you. BF: hex dump or binary dump?










ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

SansStudios wrote:

PizzasAreGreat wrote:

Chiroyce wrote:

PizzasAreGreat wrote:

I'm making GET requests to https://clouddata.scratch.mit.edu/logs?projectid=512276665&limit=1&offset=0 to detect when a user change cloud variables and sometimes there is a weird error.
Here is my code:
r = requests.get(f'https://clouddata.scratch.mit.edu/logs?projectid={PROJECT_ID}&limit=2&offset=0')
r = r.json()
The error is:
- snip -
Can you help me?
Sure! Just give me a minute ….

Here! So this should work
import requests
r = requests.get("https://clouddata.scratch.mit.edu/logs?projectid={PROJECT_ID}&limit=2&offset=0")
r = r.json()
No, it is not this problem. I did import the requests module I just forgot to include it in the post above. Here is my entire code: https://replit.com/@mdub/setting-scratch-cloud-variables-python#main.py . It is a strange glitch. It happens when a user changes the value of a cloud variable in the project but it does not happen every time.

Wouldn't it be easier to just wait until you receive data from the cloud via websockets?

You can use
ws = websocket.WebSocket()
self.ws.connect(self.WEBSOCKET_SERVER, cookie = cookie, origin = self.SERVER, enable_multithread = True) #setup example
# do handshake here
received_data = ws.recv() #after setting up a websocket.

Alternatively you can use a websocket application to call a function whenever you receive data. More info on websocket-client here.
They are using clouddata.scratch.mit.edu to get changes. Instead of setting the limit to 2 what if you set the limit to a big number and get the first and second results. Also snip bad.

Last edited by ilcheese2Test (April 9, 2021 20:29:17)

SansStudios
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

ilcheese2Test wrote:

SansStudios wrote:

They are using clouddata.scratch.mit.edu to get changes. Instead of setting the limit to 2 what if you set the limit to a big number and get the first and second results. Also snip bad.

“clouddata.scratch.mit.edu” is what the websockets use. Since websockets are built in, it's much more intuitive to just use the built in ws.recv() (At least thats my opinion). If you have like 8 variables, and all of them are changed at the same time, and you only look at the first 2, you are guaranteed to lose information.

ilcheese2 has a repo that has receiving data built in. Their github is a good reference on this.

Also, what's “snip bad”?

scratchcloud
Click above to check it out! I've worked on it for over a year!























Just like sudden sadness comes hangin' on the breeze
ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

SansStudios wrote:

ilcheese2Test wrote:

SansStudios wrote:

They are using clouddata.scratch.mit.edu to get changes. Instead of setting the limit to 2 what if you set the limit to a big number and get the first and second results. Also snip bad.

“clouddata.scratch.mit.edu” is what the websockets use. Since websockets are built in, it's much more intuitive to just use the built in ws.recv() (At least thats my opinion). If you have like 8 variables, and all of them are changed at the same time, and you only look at the first 2, you are guaranteed to lose information.

ilcheese2 has a repo that has receiving data built in. Their github is a good reference on this.

Also, what's “snip bad”?
I am ilcheese2. Thanks for mentioning mine. It was supposed to be like polyedges api.

ninjaMAR wrote:

I did @Raihan142857 code and I get no errors but the cloud variable never changes
https://replit.com/@ninjamar/scratchbridge#run.py

Also please snip
That is snip. Also why does everyone use replit.

Last edited by ilcheese2Test (April 9, 2021 21:08:04)

Raihan142857
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

ilcheese2Test wrote:

SansStudios wrote:

ilcheese2Test wrote:

SansStudios wrote:

They are using clouddata.scratch.mit.edu to get changes. Instead of setting the limit to 2 what if you set the limit to a big number and get the first and second results. Also snip bad.

“clouddata.scratch.mit.edu” is what the websockets use. Since websockets are built in, it's much more intuitive to just use the built in ws.recv() (At least thats my opinion). If you have like 8 variables, and all of them are changed at the same time, and you only look at the first 2, you are guaranteed to lose information.

ilcheese2 has a repo that has receiving data built in. Their github is a good reference on this.

Also, what's “snip bad”?
I am ilcheese2. Thanks for mentioning mine. It was supposed to be like polyedges api.

ninjaMAR wrote:

I did @Raihan142857 code and I get no errors but the cloud variable never changes
https://replit.com/@ninjamar/scratchbridge#run.py

Also please snip
That is snip. Also why does everyone use replit.
Because we don't have our own computers and we are lazy so an online IDE is our only choice

I use scratch.
GF: I'll dump you. BF: hex dump or binary dump?










Classfied3D
Scratcher
29 posts

How do you set cloud variables in python using websockets?

I've found a soloution, involving python ‘talking’ to node.js.
https://replit.com/@Classfied3D/ScratchCloud-installer-V2
It doesn't use websockets, but websockets could look something like this:

import websockets
import asyncio

async def hello(websocket, path):
name = await websocket.recv()
print(f“< {name}”)

greeting = f“Hello {name}!”

await websocket.send(str({“username”: “Classfied3D”, “ProjId”: “513725601”, “method”: “set”, “name”: “☁ test”, “value”: “012345678”}))
print(f“> {greeting}”)

start_server = websockets.serve(hello, “wss/clouddata.scratch.mit.edu/”, 531)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I'm not really that great at node.js, but someone could read this: https://github.com/trumank/scratch-api/blob/master/scratchapi.js

Last edited by Classfied3D (April 10, 2021 16:36:38)


I'm a programmer and this is a signature lol. Checkout my profile.
Raihan142857
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

Classfied3D wrote:

I've found a soloution, involving python ‘talking’ to node.js.
https://replit.com/@Classfied3D/ScratchCloud-installer-V2
It doesn't use websockets, but websockets could look something like this:

import websockets
import asyncio

async def hello(websocket, path):
name = await websocket.recv()
print(f“< {name}”)

greeting = f“Hello {name}!”

await websocket.send(str({“username”: “Classfied3D”, “ProjId”: “513725601”, “method”: “set”, “name”: “☁ test”, “value”: “012345678”}))
print(f“> {greeting}”)

start_server = websockets.serve(hello, “wss/clouddata.scratch.mit.edu/”, 531)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I'm not really that great at node.js, but someone could read this: https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Read above, we solved the OP's problem with pure python.

I use scratch.
GF: I'll dump you. BF: hex dump or binary dump?










ninjaMAR
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

Raihan142857 wrote:

Chiroyce wrote:

ninjaMAR wrote:

I did @Raihan142857 code and I get no errors but the cloud variable never changes
https://replit.com/@ninjamar/scratchbridge#run.py

Also please snip
Make sure to use the correct username and password, it worked for me when I was using PyCharm on my machine.
Also, if your password has a special character, you might have to escape it in the .env file
I'm using the correct username and password
Raihan142857
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

ninjaMAR wrote:

Raihan142857 wrote:

Chiroyce wrote:

ninjaMAR wrote:

I did @Raihan142857 code and I get no errors but the cloud variable never changes
https://replit.com/@ninjamar/scratchbridge#run.py

Also please snip
Make sure to use the correct username and password, it worked for me when I was using PyCharm on my machine.
Also, if your password has a special character, you might have to escape it in the .env file
I'm using the correct username and password
Yes, but sometimes if it has a special character you have to escape it
For example, if my password is “FreeMoneyFor$1”
in the .env file I would need to put
username=Raihan142857
password=FreeMoneyFor\$1

I use scratch.
GF: I'll dump you. BF: hex dump or binary dump?










ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

Raihan142857 wrote:

Classfied3D wrote:

I've found a soloution, involving python ‘talking’ to node.js.
https://replit.com/@Classfied3D/ScratchCloud-installer-V2
It doesn't use websockets, but websockets could look something like this:

import websockets
import asyncio

async def hello(websocket, path):
name = await websocket.recv()
print(f“< {name}”)

greeting = f“Hello {name}!”

await websocket.send(str({“username”: “Classfied3D”, “ProjId”: “513725601”, “method”: “set”, “name”: “☁ test”, “value”: “012345678”}))
print(f“> {greeting}”)

start_server = websockets.serve(hello, “wss/clouddata.scratch.mit.edu/”, 531)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I'm not really that great at node.js, but someone could read this: https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Read above, we solved the OP's problem with pure python.
My solution is the best. Why are there so many copies of the same exact thing. Also what if you used input(). It might be annoying but it may work.

Last edited by ilcheese2Test (April 10, 2021 21:44:28)

ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

Classfied3D wrote:

I've found a soloution, involving python ‘talking’ to node.js.
https://replit.com/@Classfied3D/ScratchCloud-installer-V2
It doesn't use websockets, but websockets could look something like this:

import websockets
import asyncio

async def hello(websocket, path):
name = await websocket.recv()
print(f“< {name}”)

greeting = f“Hello {name}!”

await websocket.send(str({“username”: “Classfied3D”, “ProjId”: “513725601”, “method”: “set”, “name”: “☁ test”, “value”: “012345678”}))
print(f“> {greeting}”)

start_server = websockets.serve(hello, “wss/clouddata.scratch.mit.edu/”, 531)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I'm not really that great at node.js, but someone could read this: https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Your way seems kind of lazy. Its not a very good workaround.

Last edited by ilcheese2Test (April 10, 2021 21:50:12)

ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

Raihan142857 wrote:

ilcheese2Test wrote:

SansStudios wrote:

ilcheese2Test wrote:

SansStudios wrote:

They are using clouddata.scratch.mit.edu to get changes. Instead of setting the limit to 2 what if you set the limit to a big number and get the first and second results. Also snip bad.

“clouddata.scratch.mit.edu” is what the websockets use. Since websockets are built in, it's much more intuitive to just use the built in ws.recv() (At least thats my opinion). If you have like 8 variables, and all of them are changed at the same time, and you only look at the first 2, you are guaranteed to lose information.

ilcheese2 has a repo that has receiving data built in. Their github is a good reference on this.

Also, what's “snip bad”?
I am ilcheese2. Thanks for mentioning mine. It was supposed to be like polyedges api.

ninjaMAR wrote:

I did @Raihan142857 code and I get no errors but the cloud variable never changes
https://replit.com/@ninjamar/scratchbridge#run.py

Also please snip
That is snip. Also why does everyone use replit.
Because we don't have our own computers and we are lazy so an online IDE is our only choice
Is this sarcasm?
Raihan142857
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

ilcheese2Test wrote:

Raihan142857 wrote:

ilcheese2Test wrote:

SansStudios wrote:

ilcheese2Test wrote:

SansStudios wrote:

They are using clouddata.scratch.mit.edu to get changes. Instead of setting the limit to 2 what if you set the limit to a big number and get the first and second results. Also snip bad.

“clouddata.scratch.mit.edu” is what the websockets use. Since websockets are built in, it's much more intuitive to just use the built in ws.recv() (At least thats my opinion). If you have like 8 variables, and all of them are changed at the same time, and you only look at the first 2, you are guaranteed to lose information.

ilcheese2 has a repo that has receiving data built in. Their github is a good reference on this.

Also, what's “snip bad”?
I am ilcheese2. Thanks for mentioning mine. It was supposed to be like polyedges api.

ninjaMAR wrote:

I did @Raihan142857 code and I get no errors but the cloud variable never changes
https://replit.com/@ninjamar/scratchbridge#run.py

Also please snip
That is snip. Also why does everyone use replit.
Because we don't have our own computers and we are lazy so an online IDE is our only choice
Is this sarcasm?
Sadly, no.

I use scratch.
GF: I'll dump you. BF: hex dump or binary dump?










ninjaMAR
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

removed

Last edited by ninjaMAR (April 11, 2021 16:17:13)

Chiroyce
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

 [removed] 

Last edited by Chiroyce (April 11, 2021 16:19:16)








April Fools' topics:
New Buildings in Scratch's headquarters
Give every Scratcher an M1 MacBook Air
Scratch should let users edit other Scratchers' projects
Make a statue for Jeffalo
Scratch Tech Tips™
Make a Chiroyce statue emoji


<img src=“x” onerror=“alert('XSS vulnerability discovered')”>

this is a test sentence

Powered by DjangoBB