TimMcCool / scratchattach

Scratch API wrapper with support for almost all site features and cloud requests framework
MIT License
174 stars 43 forks source link

Is it possible to get the cloud vars' names of a project? #128

Closed Boubou78000 closed 1 year ago

Boubou78000 commented 1 year ago

I have a project`import scratchattach as s3 import time import asyncio

class ChangesEmitter: def init(self, Id, CheckList, Callback, Delay): self.Id=Id self.CheckList=CheckList self.Callback=Callback self.Delay=Delay self.Values=[] for i in self.CheckList: self.Values.append(s3.get_var(self.Id, i)) async def Process(self): for i in range(len(self.CheckList)): if (Var:=s3.get_var(self.Id, self.CheckList[i]))!=self.Values[i] and Var!=None: self.Values[i]=Var self.Callback(self.CheckList[i],Var) async def Manager(self): while True: await self.Process() await asyncio.sleep(self.Delay)

def Print(Name, Value): print(f"{Name}: {Value}.")

async def Main(): Emitter=ChangesEmitter(883671226, ["Current"], Print, 0.1) await Emitter.Manager()

if name == "main": asyncio.run(Main())` to replace the events. (Don't work for me) But we need to say which vars to check and when we can't say that it's thery problematic.

Boubou78000 commented 1 year ago

Sorry the __ char is not visible on code

Ellirio commented 1 year ago

Idk but can't you get the contents of a website so can't you just go to https://scratch.mit.edu/projects/ID/#editor and get the variables...? I might try you know. I will let you know if I can. Im @LoIdesMio on scratch. But I can see this being abused like to change projects variables really fast without having to target 1? and its specifically names cloud vars.

Ellirio commented 1 year ago

Actually just get the project as a sb3 file theres a endpoint for doing that and then from the json im sure it most likely contains the variables.

RC9910-Scratch commented 1 year ago

variables = scratch3.get_cloud("project_id") #Returns a dict with all cloud var values

You can get the variable names from here, along with their values

Ellirio commented 1 year ago

I spent 2 hours making this only to read the comment above but feel free to use if you still want to...

      import requests
      import json

      def get_cloud_vars(id):
          json = requests.get(f"https://api.scratch.mit.edu/projects/{id}").json()
          token = json["project_token"]

          project = requests.get(f"https://projects.scratch.mit.edu/{id}?token={token}").json()

          var_dict = project["targets"][0]["variables"]
          var_val = var_dict.values()
          var_list = list(var_val)
          variables = []
          cloud_vars = []
          for i in range(len(var_list)):
              variables.append(var_list[i][0])

          for i in range(len(variables)):
              if "☁" in variables[i]:
                  cloud_vars.append(variables[i])
          return cloud_vars

If you also want the normal variables its the variables list.

Boubou78000 commented 1 year ago

I have a problem: it writes

Warning: Logged in as Boubou78000, but couldn't fetch XToken. Cloud variables will still work, but other features may not work properly. If you're using an online IDE (like replit.com) Scratch possibly banned its IP adress.

But I can't change the values of the cloud variables.

RC9910-Scratch commented 1 year ago

Are you using replit?

Boubou78000 commented 1 year ago

Yes

RC9910-Scratch commented 1 year ago

Replit doesn't work, so instead try running it locally on your computer

TimMcCool commented 1 year ago

You can use:

variables = scratch3.get_cloud("project_id")
variable_names = list(variables.keys()) # A list with the names of all cloud variables

Works on replit too

TheCommCraft commented 1 year ago

I spent 2 hours making this only to read the comment above but feel free to use if you still want to...

      import requests
      import json

      def get_cloud_vars(id):
          json = requests.get(f"https://api.scratch.mit.edu/projects/{id}").json()
          token = json["project_token"]

          project = requests.get(f"https://projects.scratch.mit.edu/{id}?token={token}").json()

          var_dict = project["targets"][0]["variables"]
          var_val = var_dict.values()
          var_list = list(var_val)
          variables = []
          cloud_vars = []
          for i in range(len(var_list)):
              variables.append(var_list[i][0])

          for i in range(len(variables)):
              if "☁" in variables[i]:
                  cloud_vars.append(variables[i])
          return cloud_vars

If you also want the normal variables its the variables list.

You can replace the last two for-loops with this

for i in var_list
    try:
        if i[2]:
            cloud_vars.append(i[0])
    except:
        pass