This article documents something which is not associated with Scratch or the Scratch Foundation.

This tutorial teaches how to access the Scratch API using Python 3.

Note Warning: Using this to make bots that perform social actions (loving, following, commenting, etc.) violates the Terms of Use and may result in a ban. Please use this appropriately.
Note Warning: Some functions of these modules may require a Scratch username and password. There is a possibility that these modules could maliciously steal passwords and could take over Scratch accounts. Please exercise caution when entering passwords to untrusted sources. Never enter Scratch passwords in any module or library that is untrusted. Avoid proprietary libraries, and consider reviewing the source code of any modules you use.

Setup

To access Scratch using Python 3, an IDE for Python 3 is needed. Then, install the module "scratchattach", a popular library[1], made by TimMcCool to access Scratch.


Run the following command in your command prompt / shell:

pip install -U scratchattach

This should download the correct module to access Scratch. Lastly, make sure your device is connected to the internet.

Coding

Once it has been setup, the programming can begin.

Note Note: For the functions to display information, such as: session.connect_project(PROJECT_ID).title You may need to use the "print" function to display the information in your console. Like so: print(TEXT). For example, if you want to get the title of a project, you would use: print(session.connect_project(PROJECT_ID).title).
Note Warning: The following code requires a Scratch username and password. There is no guarantee that the modules mentioned here are safe. Once credentials are provided, these modules have full access to the corresponding Scratch account.

Logging in with username / password:[2]

import scratchattach as sa  # Import the scratchattach module

session = sa.login("username", "password") # Login with Scratch login information (replace with your username and password)

Examples:[2]

Cloud variables:

cloud = session.connect_cloud("project_id") # connect to the cloud

value = cloud.get_var("variable")
cloud.set_var("variable", "value") # the variable name is specified without the cloud emoji

Cloud events:

cloud = session.connect_cloud('project_id')
events = cloud.events()

@events.event
def on_set(activity):
    print("variable", activity.var, "was set to", activity.value)
events.start()

Follow users, love their projects and comment:

user = session.connect_user('username')
user.follow()

project = user.projects()[0]
project.love()
project.post_comment('Great project!')

All scratchattach features are documented in the documentation

External Links

References

Cookies help us deliver our services. By using our services, you agree to our use of cookies.