@@ -94,83 +94,41 @@ def __post_init__(self) -> None:
9494 self ._json_headers ["Content-Type" ] = "application/json"
9595
9696 def _update_from_dict (self , data : ProjectDict ):
97- try :
98- self .id = int (data ["id" ])
99- except KeyError :
100- pass
101- try :
102- self .url = f"https://scratch.mit.edu/projects/{ self .id } "
103- except KeyError :
104- pass
105- try :
106- self .author_name = data ["author" ]["username" ]
107- except KeyError :
108- pass
109- try :
110- self .author_name = data ["username" ] # type: ignore[typeddict-item]
111- except KeyError :
112- pass
113- try :
114- self .comments_allowed = data ["comments_allowed" ]
115- except KeyError :
116- pass
117- try :
118- self .instructions = data ["instructions" ]
119- except KeyError :
120- pass
121- try :
122- self .notes = data ["description" ]
123- except KeyError :
124- pass
125- try :
126- self .created = data ["history" ]["created" ]
127- except KeyError :
128- pass
129- try :
130- self .last_modified = data ["history" ]["modified" ]
131- except KeyError :
132- pass
133- try :
134- self .share_date = data ["history" ]["shared" ]
135- except KeyError :
136- pass
137- try :
138- self .thumbnail_url = data ["image" ]
139- except KeyError :
140- pass
141- try :
142- self .remix_parent = data ["remix" ]["parent" ]
143- self .remix_root = data ["remix" ]["root" ]
144- except KeyError :
145- self .remix_parent = None
146- self .remix_root = None
147- try :
148- self .favorites = data ["stats" ]["favorites" ]
149- except KeyError :
150- pass
151- try :
152- self .loves = data ["stats" ]["loves" ]
153- except KeyError :
154- pass
155- try :
156- self .remix_count = data ["stats" ]["remixes" ]
157- except KeyError :
158- pass
159- try :
160- self .views = data ["stats" ]["views" ]
161- except KeyError :
162- pass
163- try :
164- self .title = data ["title" ]
165- except KeyError :
166- pass
167- try :
168- self .project_token = data ["project_token" ]
169- except KeyError :
170- self .project_token = None
171- if "code" in data : # Project is unshared -> return false
172- return False
173- return True
97+ self .id = int (data .get ("id" , self .id ))
98+ self .url = f"https://scratch.mit.edu/projects/{ self .id } "
99+ if author := data .get ("author" ):
100+ self .author_name = author .get ("username" , self .author_name )
101+ self .author_name = data .get ("username" , self .author_name )
102+ self .comments_allowed = data .get ("comments_allowed" , self .comments_allowed )
103+ self .instructions = data .get ("instructions" , self .instructions )
104+ self .notes = data .get ("description" , self .notes )
105+
106+ if history := data .get ("history" ):
107+ self .created = history .get ("created" , self .created )
108+ self .last_modified = history .get ("modified" , self .last_modified )
109+ self .share_date = history .get ("shared" , self .share_date )
110+
111+ self .thumbnail_url = data .get ("image" , self .thumbnail_url )
112+
113+ # NOTE: if we have no value, then we set it to None instead of empty string.
114+ # TODO: consider changing this behavior
115+ remix_data = data .get ("remix" , {})
116+ self .remix_parent = remix_data .get ("parent" )
117+ self .remix_root = remix_data .get ("root" )
118+
119+ if stats := data .get ("stats" ):
120+ self .favorites = stats .get ("favorites" , self .favorites )
121+ self .loves = stats .get ("loves" , self .loves )
122+ self .remix_count = stats .get ("remixes" , self .remix_count )
123+ self .views = stats .get ("views" , self .views )
124+
125+ self .title = data .get ("title" , self .title )
126+ self .project_token = data .get ("project_token" , None )
127+
128+ # the typed dict here isn't perfect:
129+ # code as in {"code": "not found"}
130+ # if the project is unshared, then we get that error code
131+ return "code" not in data
174132
175133 def __rich__ (self ):
176134 from rich .panel import Panel
0 commit comments