Discuss Scratch

NFlex23
Scratcher
1000+ posts

Change Profile Picture with Python

I have a bunch of different variations of my PFP, and I would like the script to cycle through one every day or so automatically. Are there any (Python) libraries capable of setting a profile picture?



I don't think this would be too confusing for people since the only thing changing would be the background.

Last edited by NFlex23 (May 14, 2022 13:34:12)

coolcoder1213
Scratcher
100+ posts

Change Profile Picture with Python

In my Python library, CloudConnecter, there is a feature to GET the PFP but not set it.


Maybe I will add it.
MagicCrayon9342
Scratcher
1000+ posts

Change Profile Picture with Python

You will have to talk with the developers of these libraries for the feature. At the moment, the standard Scratch API does not allow for changing the user's details and PFP through the API. A third party API can't be created to do this either. You can talk with the Scratch Team for an API end point but that's the most you can do. BUT you can use a gecko webdriver or chromium webdriver to, automatically every day create a browser window, go to the page to change the pfp, and change it. You can automate it through Mac software or Microsoft Power Automate.

Quick Answer, look here skip the rest
There is no way to use any APIs or python to change the PFP.
NFlex23
Scratcher
1000+ posts

Change Profile Picture with Python

MagicCrayon9342 wrote:

(#3)
You will have to talk with the developers of these libraries for the feature. At the moment, the standard Scratch API does not allow for changing the user's details and PFP through the API. A third party API can't be created to do this either. You can talk with the Scratch Team for an API end point but that's the most you can do. BUT you can use a gecko webdriver or chromium webdriver to, automatically every day create a browser window, go to the page to change the pfp, and change it. You can automate it through Mac software or Microsoft Power Automate.

Quick Answer, look here skip the rest
There is no way to use any APIs or python to change the PFP.
No, there is a Scratch API endpoint for changing PFPs, but I wanted to use a library since I don't have experience with the Scratch API. Just inspect the page and switch to the network tab, then change your PFP and see which endpoint gets POSTed to.

Last edited by NFlex23 (May 14, 2022 14:20:49)

MagicCrayon9342
Scratcher
1000+ posts

Change Profile Picture with Python

NFlex23 wrote:

MagicCrayon9342 wrote:

(#3)
You will have to talk with the developers of these libraries for the feature. At the moment, the standard Scratch API does not allow for changing the user's details and PFP through the API. A third party API can't be created to do this either. You can talk with the Scratch Team for an API end point but that's the most you can do. BUT you can use a gecko webdriver or chromium webdriver to, automatically every day create a browser window, go to the page to change the pfp, and change it. You can automate it through Mac software or Microsoft Power Automate.

Quick Answer, look here skip the rest
There is no way to use any APIs or python to change the PFP.
No, there is a Scratch API endpoint for changing PFPs, but I wanted to use a library since I don't have experience with authenticating the Scratch API.
Really, there is? Well then just ask the library developers to implement. What is the endpoint exactly?
NFlex23
Scratcher
1000+ posts

Change Profile Picture with Python

MagicCrayon9342 wrote:

(#5)
Really, there is? Well then just ask the library developers to implement. What is the endpoint exactly?
I updated my post.
coolcoder1213
Scratcher
100+ posts

Change Profile Picture with Python

Well, on all of the Python libraries created, you have to be logged in as the owner of the project to change your about me and what I'm working on and the title of a project, ect. So you would have to be logged in as yourself but that would log you out when you tried to use your account unless you had breaks between changes.
NFlex23
Scratcher
1000+ posts

Change Profile Picture with Python

coolcoder1213 wrote:

(#7)
Well, on all of the Python libraries created, you have to be logged in as the owner of the project to change your about me and what I'm working on and the title of a project, ect. So you would have to be logged in as yourself but that would log you out when you tried to use your account unless you had breaks between changes.
True, but I was thinking of running it only every 1 to 3 days or so.
NFlex23
Scratcher
1000+ posts

Change Profile Picture with Python

Bump.
ScolderCreations
Scratcher
1000+ posts

Change Profile Picture with Python

If you can get the session token of your account through a python module that logs you in, you would probably have a way to use the endpoint, which would solve your problem. The only question is, which module would do that?
mybearworld
Scratcher
1000+ posts

Change Profile Picture with Python

ScolderCreations wrote:

(#10)
If you can get the session token of your account through a python module that logs you in, you would probably have a way to use the endpoint, which would solve your problem. The only question is, which module would do that?
Scratchclient can do that:
mybearworld
Scratcher
1000+ posts

Change Profile Picture with Python

I can't figure out how to do this though, as I… can't find where the image is in the POST
god286
Scratcher
1000+ posts

Change Profile Picture with Python

I haven't found the answer yet but I have a few “clues”…
$("input.hidden[type=\"file\"]").data("events").change[0]
maybe we can find how it works, it seems like it sends through $.ajax
EDIT:
I found the code where it is from!
Seems to be around Scratch.UserThumbnail, try Cmd/Ctrl + F it
https://cdn.scratch.mit.edu/scratchr2/static/__9209616f20ac7fc52efa806cd216a370__/js/base.js

Edit 2:
Can someone figure out how to go through this code? Beautifying it would be easier too

I know you want a library so once this is done maybe someone can PR it into a library

Last edited by god286 (May 15, 2022 23:52:04)

SansStudios
Scratcher
1000+ posts

Change Profile Picture with Python

Here's a small function specifically for scratchcloud. I won't be adding this to the main library (because I will never add any social interactions to the main library), but you're more than welcome to use it!

The post request looks really confusing, but it turns out that the image is just converted to bytes before sending. I'll comment the below code so that other library maintainers can add it to their own libraries If other python maintainers need help, I'd be more than happy to open a PR or two.

import scratchcloud
import aiohttp
async def post_change_icon(client: CloudClient, file: str):
    """Changes the current user's icon using the Client session. Returns a string with the cdn2 link to the new image.
    :param client: a CloudClient object.
    :type client: scratchcloud.CloudClient
    :param file: the path to the new profile picture
    :type file: str
    :rtype: str
    """
    # The site-api path for changing thumbnails (aka profile pictures)
    PATH = f'https://scratch.mit.edu/site-api/users/all/{client.username}/'
    # Open the file that will be changed in BYTES mode
    with open(file, 'rb') as img:
        # Make a new form. This ensures that the data sent is a "form" type
        form = aiohttp.FormData()
        # Add 'thumbnail' and set it to the image
        form.add_field('thumbnail', img)
        # Post the form
        async with client.http_session.post(url=PATH, data=form) as response:
            # Get the JSON response
            rdata = await response.json()
            # Return the cdn2 url for the new thumbnail
            return rdata['thumbnail_url'].strip('//')

Tested and worked for me!

Last edited by SansStudios (May 16, 2022 01:51:18)

mybearworld
Scratcher
1000+ posts

Change Profile Picture with Python

SansStudios wrote:

(#14)
import scratchcloud
import aiohttp
async def post_change_icon(client: CloudClient, file: str):
    # ...
        async with client.http_session.post(url=PATH, data=form) as response:
            # ...
What are you supposed to put into “client”? I tried scratchcloud.CloudClient but that just returns none
SansStudios
Scratcher
1000+ posts

Change Profile Picture with Python

mybearworld wrote:

SansStudios wrote:

(#14)
# code
What are you supposed to put into “client”? I tried scratchcloud.CloudClient but that just returns none

You're supposed to use a client object. If you haven't used scratchcloud before, i'd recommend checking out the docs.

A basic runner script might look like this:

from scratchcloud import CloudClient
import aiohttp
client = CloudClient(username='username', project_id='686861597')
async def post_change_icon(client: CloudClient, file: str):
    # code
@client.event
async def on_connect():
    print("Connected! Changing icon now...")
    image_url = await post_change_icon(client, 'relative/path/to/image.png')
    print(f'Icon changed to: {image_url}')
    client.stop()
client.run("pswd")
mybearworld
Scratcher
1000+ posts

Change Profile Picture with Python

Okay, thank you! I'll probably never understand the @ things, but anyways, it works for me too!
SansStudios
Scratcher
1000+ posts

Change Profile Picture with Python

Glad it worked! Always a relief to hear that your code doesn't just work for one very specifically set up computer

In Python, the @ symbol represents a “decorator”. A decorator can change the function that it decorates. In scratchcloud, decorators let the client object to run a specific function whenever something happens. In the above example, the `@client.event` decorator runs the `on_connect` function whenever the client object connects to the cloud.

If you'd rather a full async approach that doesn't use any decorators or any cloud data, you could also do this:

#Previous code up here
import asyncio
async def change_icon():
    # Create a CloudClient that will be used solely for changing the pfp
    client = CloudClient(username='SS-2', project_id=None)
    # Login to scratch
    await client.login("Enter your password here")
    # Run the icon code
    icon_url = await post_change_icon(client, 'another_profile_icon.png')
    # Print the new icon cdn2 link
    print(f'Icon changed to: {icon_url}')
    # Close the client
    await client.close()
# Run the change_icon function synchronously
asyncio.run(change_icon())

scratchcloud is not built for API interactions—it's main focus is cloud interactions which is why all of the extra runner stuff is needed. I'm sure that libraries like ScratchConnect or Scratch2Py would handle this with less boilerplate code.

Last edited by SansStudios (May 16, 2022 05:18:49)

NFlex23
Scratcher
1000+ posts

Change Profile Picture with Python

It worked for me too; thank you so much for your help @SansStudios!
GalakC
Scratcher
500+ posts

Change Profile Picture with Python

i don't know if this would be allowed, since it is sort of self-botting, but it seems cool. why did you want to do this?

Powered by DjangoBB