MCPcopy
hub / github.com/praw-dev/praw / _upload

Method _upload

praw/models/media.py:253–279  ·  view source on GitHub ↗

Upload the media to Reddit. :param subreddit: The subreddit the stylesheet image is associated with. :param additional_data: Additional data to include in the upload request. :returns: A dictionary containing a link to the uploaded image under the key ``img_src`

(  # pyright: ignore[reportIncompatibleMethodOverride]  # stylesheet image upload returns the raw response dict
        self, subreddit: models.Subreddit, /, **additional_data: str
    )

Source from the content-addressed store, hash-verified

251 return "jpg" if self._data.startswith(JPEG_HEADER) else "png"
252
253 def _upload( # pyright: ignore[reportIncompatibleMethodOverride] # stylesheet image upload returns the raw response dict
254 self, subreddit: models.Subreddit, /, **additional_data: str
255 ) -> dict[str, Any]:
256 """Upload the media to Reddit.
257
258 :param subreddit: The subreddit the stylesheet image is associated with.
259 :param additional_data: Additional data to include in the upload request.
260
261 :returns: A dictionary containing a link to the uploaded image under the key
262 ``img_src``.
263
264 """
265 data = {"img_type": self._image_type, **additional_data}
266 url = self.UPLOAD_API_PATH.format(subreddit=subreddit)
267 # ``requests`` accepts ``(filename, content)`` tuples for file uploads, but
268 # ``Reddit.post`` types ``files`` as ``dict[str, IO]``.
269 files = cast("dict[str, IO[Any]]", {"file": (self.name, self._data)})
270 response = subreddit._reddit.post(url, data=data, files=files)
271 if response["errors"]:
272 error_type = response["errors"][0]
273 error_value = response.get("errors_values", [""])[0]
274 assert error_type in {
275 "BAD_CSS_NAME",
276 "IMAGE_ERROR",
277 }, "Please file a bug with PRAW."
278 raise RedditAPIException([RedditErrorItem(error_type=error_type, field="", message=error_value or "")])
279 return response
280
281
282class WidgetMedia(Media):

Callers

nothing calls this directly

Calls 4

RedditAPIExceptionClass · 0.90
RedditErrorItemClass · 0.90
postMethod · 0.80
getMethod · 0.80

Tested by

no test coverage detected