MCPcopy Create free account
hub / github.com/TimMcCool/scratchattach / set_thumbnail

Method set_thumbnail

scratchattach/site/studio.py:252–295  ·  view source on GitHub ↗

Sets the studio thumbnail. You can only use this function if this object was created using :meth:`scratchattach.session.Session.connect_studio` Keyword Arguments: file: The path to the image file Returns: str: Scratch cdn link to the set thumbnail

(self, *, file)

Source from the content-addressed store, hash-verified

250 )
251
252 def set_thumbnail(self, *, file):
253 """
254 Sets the studio thumbnail. You can only use this function if this object was created using :meth:`scratchattach.session.Session.connect_studio`
255
256 Keyword Arguments:
257 file: The path to the image file
258
259 Returns:
260 str: Scratch cdn link to the set thumbnail
261 """
262 self._assert_auth()
263 with open(file, "rb") as f:
264 thumbnail = f.read()
265
266 filename = file.replace("\\", "/")
267 if filename.endswith("/"):
268 filename = filename[:-1]
269 filename = filename.split("/").pop()
270
271 file_type = filename.split(".").pop()
272
273 payload1 = f'------WebKitFormBoundaryhKZwFjoxAyUTMlSh\r\nContent-Disposition: form-data; name="file"; filename="{filename}"\r\nContent-Type: image/{file_type}\r\n\r\n'
274 payload1 = payload1.encode("utf-8")
275 payload2 = b"\r\n------WebKitFormBoundaryhKZwFjoxAyUTMlSh--\r\n"
276 payload = b"".join([payload1, thumbnail, payload2])
277
278 r = requests.post(
279 f"https://scratch.mit.edu/site-api/galleries/all/{self.id}/",
280 headers={
281 "accept": "*/",
282 "content-type": "multipart/form-data; boundary=----WebKitFormBoundaryhKZwFjoxAyUTMlSh",
283 "Referer": "https://scratch.mit.edu/",
284 "x-csrftoken": "a",
285 "x-requested-with": "XMLHttpRequest",
286 },
287 data=payload,
288 cookies=self._cookies,
289 timeout=10,
290 ).json()
291
292 if "errors" in r:
293 raise (exceptions.BadRequest(", ".join(r["errors"])))
294 else:
295 return r["thumbnail_url"]
296
297 def reply_comment(self, content, *, parent_id, commentee_id=""):
298 """

Callers

nothing calls this directly

Calls 5

encodeMethod · 0.80
jsonMethod · 0.80
postMethod · 0.80
_assert_authMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected