Package: scratchcommunication
scratchcommunicationA python module for communicating with scratch projectsInstallationRun this in your commandlinepip install scratchcommunicationORAdd this at the top of your python scriptimportsubprocesssubprocess.run("pip install --upgrade scratchcommunication",shell=True,capture_output=True)Creating a sessionYou can use thescratchcommunication.session.Sessionclass to log into your account.For this, you either need a session id or a password.Login using session idimportscratchcommunicationsession=scratchcommunication.Session("YOUR_USERNAME",session_id="YOUR_SESSIONID")If you log in using your session id, you may not need your to supply your username.Login using passwordimportscratchcommunicationsession=scratchcommunication.Session.login("YOUR_USERNAME","YOUR_PASSWORD")I recommend using your session id instead of your password.Access account dataOnce you have logged into your account, you can access your account data.your_session_id=session.session_idyour_username=session.usernameemail=session.emailuser_id=session.idbanned=session.bannednew_scratcher=session.new_scratchermute_status=session.mute_statusCloud VariablesConnecting to Scratch Cloud VariablesYou can connect to a scratch projects cloud variables using yourscratchcommunication.session.Sessioncloud=session.create_cloudconnection(project_id="Your project id here",quickaccess=False,# (Optional) Allows you to use the cloud connection as a Mapping for the cloud variables.reconnect=True,# (Optional) Allows the cloud connection to reconnect if it disconnects.receive_from_websocket=True,# (Optional) Creates a thread which receives cloud updates and allows for events.warning_type=ErrorInEventHandler,# (Optional) Determines what type of Warning will be used if there is an error in the event handler.daemon_thread=False# (Optional) Determines if the thread used for events will be daemon)ORcloud=scratchcommunication.CloudConnection(project_id="Your project id here",session=session,**kwargs)Connecting to Turbowarp Cloud VariablesTo connect to Turbowarps Cloud Variables you can use thescratchcommunication.cloud.TwCloudConnectionclasstw_cloud=scratchcommunication.TwCloudConnection(project_id="Your project id here",username="player1000",# (Optional)quickaccess=False,reconnect=True,receive_from_websocket=True,warning_type=ErrorInEventHandler,cloud_host="wss://clouddata.turbowarp.org/",# (Optional) Changes the host used for cloud variables.accept_strs=False# (Optional) Allows you to set cloud variables to strings. Only works if cloud host allows it.)Or you could connect from your sessiontw_cloud=session.create_turbowarp_cloudconnection(project_id="Your project id here",username=None,# (Optional) Will default to your usernamequickaccess=False,reconnect=True,receive_from_websocket=True,warning_type=ErrorInEventHandler,cloud_host="wss://clouddata.turbowarp.org/",accept_strs=False)To shorten it a bit you can also usescratchcommunication.session.Session.create_tw_cloudconnectioninstead ofscratchcommunication.session.Session.create_turbowarp_cloudconnection.Working with cloud variablesTo set or get cloud variables you can usescratchcommunication.cloud.CloudConnection.set_variableandscratchcommunication.cloud.CloudConnection.get_variable.cloud.set_variable(name="HIGHSCORE",# A cloud symbol will be added automatically if there isn't onevalue=1000,name_literal=False# (Optional) Allows for setting a variable with a name without a cloud symbol)value=cloud.get_variable(name="HIGHSCORE",name_literal=False)Or if you enabled quickaccess you can use the object like a Mapping.cloud["HIGHSCORE"]=1000value=cloud["HIGHSCORE"]For cloud events you can use the decoratorscratchcommunication.cloud.CloudConnection.on@cloud.on("set")# Can be "set", "create", "delete", "connect" or "any"defon_set(event):print(event.project_id,event.name,# Variable name without cloud symbolevent.var,# Variable nameevent.value,event.type,# Event typeevent.project,# Cloud connectionevent.user,event.timestamp)And if you want, you can also emit events yourself. (Those events will not be broadcast.)cloud.emit_event(event="custom_event_1",**entries)# Orcloud.emit_event(event=scratchcommunication.Event(type="custom_event_1",**entries))And if you want to enable or disable quickaccess after you already created the connection, you can usescratchcommunication.cloud.CloudConnection.enable_quickaccessandscratchcommunication.cloud.CloudConnection.disable_quickaccesscloud.enable_quickaccess()cloud.disable_quickaccess()If you want to stop the background thread used for events and variable updates, you can usescratchcommunication.cloud.CloudConnection.stop_threadcloud.stop_thread()If you setdaemon_threadtoTruewhen creating the object, the background thread will terminate when your process ends.Cloud socketsCloud sockets (inspired by sockets) are connections from a scratch project to a python program where both sides can send data to one another and there is even the possibility to add security to the connection using RSA.Creating a cloud socketTo create a cloud socket you can usescratchcommunication.session.Session.create_cloud_socket.cloud_socket=session.create_cloud_socket(project_id="Your project id here",packet_size=220,# (Optional) I recommend leaving this value be if you only use Scratch and Turbowarp.cloudconnection_kwargs=None,# (Optional) Allows for adding keyword arguments for the Cloud Connection used in the cloud socket. Look at the documentation for Cloud Connection if you do not know which keyword arguments there aresecurity=None# (Optional) Allows for a secure connection. Recommended. Look at Cloud Socket Security for more info.)You can also use Turbowarp for cloud sockets.cloud_socket=session.create_turbowarp_cloud_socket(# session.create_tw_cloud_socket also works hereproject_id="Your project id here",packet_size=220,# (Optional) I recommend leaving this value be if you only use Scratch and Turbowarp.cloudconnection_kwargs=None,# (Optional) Allows for adding keyword arguments for the Cloud Connection used in the cloud socket. Look at the documentation for Cloud Connection if you do not know which keyword arguments there aresecurity=None# (Optional) Allows for a secure connection. Recommended. Look at Cloud Socket Security for more info.)Cloud Socket SecurityIn order for the cloud socket to work, you'll need to add the sprite from thisprojectto yours. Be sure to check that all the variables starting with the cloud symbol are cloud variables and that their names stay as they are.Using a cloud socketOnce you have created a cloud socket you have to start it usingscratchcommunication.cloud_socket.CloudSocket.listencloud_socket.listen()After you start the cloud socket you can wait for a new user usingscratchcommunication.cloud_socket.CloudSocket.acceptclient,client_username=cloud_socket.accept()When you have a client, you can send messages to them and receive messages.msg=client.recv()client.send("Hello!")Your messages will be public and easy to fake in both direction unless you activatesecurity.In order to stop the cloud socket from running anymore you can youscratchcommunication.cloud_socket.CloudSocket.stopcloud_socket.stop()Cloud Socket SecurityYou might want to be able to send private data that only the designated recipient can read, but that is impossible unless you use asymmetric encryption or something similar. Fortunately for you, the hard work has already been done.You will need to generate RSA keys for this. In order to do that, you can just usescratchcommunication.security.RSAKeys.create_new_keys.keys=scratchcommunication.security.RSAKeys.create_new_keys()After you have generated your keys, you will want to store and load them. For storing your keys, you need to usescratchcommunication.security.RSAKeys.keysto find the values to store.print(keys.keys)When you have stored the three integers displayed, you just need to load them whenever you start your cloud socket and put two of them in your project.Neverreveal all of the keys unless you want to regenerate them.keys=scratchcommunication.security.RSAKeys((value_1,value_2,value_3))print(keys.public_keys)# Put the resulting values into the corresponding variables in your Scratch project.To use the keys in your cloud socket, you just need to pass them in assecurity.secure_cloud_socket=session.create_cloud_socket(project_id="Your project id here",security=keys)ContactIf you have questions, want to propose new features or have found a bug, contact mehereMore coming soon