scratchattach icon indicating copy to clipboard operation
scratchattach copied to clipboard

Favourite/Love gives KeyError: "userFavorite/userLove"

Open ghost opened this issue 10 months ago • 4 comments

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 avatar Apr 06 '24 17:04 ghost

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

ghost avatar Apr 06 '24 17:04 ghost

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

TimMcCool avatar Apr 16 '24 18:04 TimMcCool

@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 🥺

ghost avatar Apr 24 '24 15:04 ghost

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

namtacs avatar Jun 20 '24 07:06 namtacs

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

TimMcCool avatar Sep 28 '24 11:09 TimMcCool