Skip to content

Commit

Permalink
Merge pull request #1034 from jarhill0/raise-timeout
Browse files Browse the repository at this point in the history
Raise and customize timeout when submitting images and videos
  • Loading branch information
bboe committed Feb 27, 2019
2 parents 9e9d68c + d5acc05 commit b5fa7d0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Unreleased
* :meth:`~.Subreddit.submit`, :meth:`~.Subreddit.submit_image`, and
:meth:`~.Subreddit.submit_video` support parameter ``spoiler`` to
mark the submission as a spoiler immediately upon posting.
* :meth:`~.Subreddit.submit_image` and `~.Subreddit.submit_video` support
parameter ``timeout``. Default timeout has been raised from 2 seconds to
10 seconds.

**Other**

Expand Down
30 changes: 19 additions & 11 deletions praw/models/reddit/subreddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def __init__(self, reddit, display_name=None, _data=None):
def _info_path(self):
return API_PATH['subreddit_about'].format(subreddit=self)

def _submit_media(self, data):
def _submit_media(self, data, timeout):
"""Submit and return an `image`, `video`, or `videogif`.
This is a helper method for submitting posts that are not link posts or
Expand Down Expand Up @@ -446,9 +446,9 @@ def _submit_media(self, data):
try:
socket = websocket.create_connection(response['json']['data']
['websocket_url'],
timeout=2)
timeout=timeout)
ws_update = loads(socket.recv())
socket.close(timeout=2)
socket.close()
except websocket.WebSocketTimeoutException:
raise ClientException('Websocket error. Check your media file. '
'Your post may still have been created.')
Expand Down Expand Up @@ -611,7 +611,7 @@ def submit(self, title, selftext=None, url=None, flair_id=None,

def submit_image(self, title, image_path, flair_id=None,
flair_text=None, resubmit=True, send_replies=True,
nsfw=False, spoiler=False):
nsfw=False, spoiler=False, timeout=10):
"""Add an image submission to the subreddit.
:param title: The title of the submission.
Expand All @@ -627,13 +627,17 @@ def submit_image(self, title, image_path, flair_id=None,
(default: False).
:param spoiler: Whether or not the submission should be marked as
a spoiler (default: False).
:param timeout: Specifies a particular timeout, in seconds. Use to
avoid "Websocket error" exceptions (default: 10).
.. note::
Reddit's API uses WebSockets to respond with the link of the
newly created post. If this fails, the method will raise
:class:`.ClientException`. Occasionally, the Reddit post will still
be created. More often, there is an error with the image file.
be created. More often, there is an error with the image file. If
you frequently get exceptions but successfully created posts, try
setting the ``timeout`` parameter to a value above 10.
:returns: A :class:`~.Submission` object for the newly created
submission.
Expand All @@ -654,19 +658,19 @@ def submit_image(self, title, image_path, flair_id=None,
if value is not None:
data[key] = value
data.update(kind='image', url=self._upload_media(image_path))
return self._submit_media(data)
return self._submit_media(data, timeout)

def submit_video(self, title, video_path, videogif=False,
thumbnail_path=None, flair_id=None, flair_text=None,
resubmit=True, send_replies=True, nsfw=False,
spoiler=False):
spoiler=False, timeout=10):
"""Add a video or videogif submission to the subreddit.
:param title: The title of the submission.
:param video_path: The path to a video, to upload and post.
:param videogif: A ``bool`` value. If ``True``, the video is
uploaded as a videogif, which is essentially a silent video (
default: ``False``).
uploaded as a videogif, which is essentially a silent video
(default: ``False``).
:param thumbnail_path: (Optional) The path to an image, to be uploaded
and used as the thumbnail for this video. If not provided, the
PRAW logo will be used as the thumbnail.
Expand All @@ -682,13 +686,17 @@ def submit_video(self, title, video_path, videogif=False,
(default: False).
:param spoiler: Whether or not the submission should be marked as
a spoiler (default: False).
:param timeout: Specifies a particular timeout, in seconds. Use to
avoid "Websocket error" exceptions (default: 10).
.. note::
Reddit's API uses WebSockets to respond with the link of the
newly created post. If this fails, the method will raise
:class:`.ClientException`. Occasionally, the Reddit post will still
be created. More often, there is an error with the video file.
be created. More often, there is an error with the video file. If
you frequently get exceptions but successfully created posts, try
setting the ``timeout`` parameter to a value above 10.
:returns: A :class:`~.Submission` object for the newly created
submission.
Expand All @@ -712,7 +720,7 @@ def submit_video(self, title, video_path, videogif=False,
url=self._upload_media(video_path),
# if thumbnail_path is None, it uploads the PRAW logo
video_poster_url=self._upload_media(thumbnail_path))
return self._submit_media(data)
return self._submit_media(data, timeout)

def subscribe(self, other_subreddits=None):
"""Subscribe to the subreddit.
Expand Down

0 comments on commit b5fa7d0

Please sign in to comment.