TimMcCool / scratchattach

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

Favourite/Love gives KeyError: "userFavorite/userLove" #191

Closed ghost closed 6 days ago

ghost commented 6 months ago

It feels like it randomly happens out of nowhere, I'm quite positive it's not a rate exceeding error, as when I do it manually it stays after reloading the page.

ghost commented 6 months ago

Error for debugging: "...\site-packages\scratchattach\project.py", line 370, in unfavorite if r['userFavorite'] is True: KeyError: 'userFavorite'"

TimMcCool commented 5 months ago

How often does this error occur? Can you provide your code?

ghost commented 5 months ago

@TimMcCool It happens quite often, sometimes a project or two does actually unfavourite, here's the code:

def Write(input):
    os.system('CLS')
    print(input)

#sign-in
Write("Logging in...")
session = scratch3.login() #hidden for obvious reasons
MainUser = session.get_linked_user()

Write("Finding Projects...")
Projects = []
for i in range(math.ceil(MainUser.favorites_count() / 20)):
    projects_chunk = MainUser.favorites(limit=20, offset=i * 20)
    Projects += projects_chunk

for i in range(len(Projects)):
    project = session.connect_project(Projects[i].id)
    Write("Removing Project " + project.title)
    project.unfavorite()

What I've noticed is that while you can unfavourite as many projects as you like manually, It takes a minute or two to actually update on your profile, and it the code breaks the same time it doesn't update manually. I'm pretty sure it might be the scratch API at fault here and not scratchattach 🥺

namtacs commented 3 months ago

i fixed this in my private fork, here's how you can do it:

  1. open the %appdata%\Local\Programs\Python\Python310\lib\site-packages\scratchattach\project.py file
  2. go to line 373 (unlove)
  3. change if r["userLove"] is True: to if r.get("userLove") == True:
  4. try it
  5. it it doesn't work, try adding a 1 second delay
TimMcCool commented 6 days ago

v2.0 will raise the APIError Exception and show the server response when this happens