/
reddit.py
995 lines (804 loc) · 35 KB
/
reddit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
"""Provide the Reddit class."""
import asyncio
import configparser
import os
import re
import time
from itertools import islice
from logging import getLogger
from typing import (
IO,
TYPE_CHECKING,
Any,
Dict,
Generator,
Iterable,
Optional,
Type,
Union,
)
from warnings import warn
from prawcore import (
Authorizer,
DeviceIDAuthorizer,
ReadOnlyAuthorizer,
Redirect,
Requestor,
ScriptAuthorizer,
TrustedAuthenticator,
UntrustedAuthenticator,
session,
)
from prawcore.exceptions import BadRequest
from . import models
from .config import Config
from .const import API_PATH, USER_AGENT_FORMAT, __version__
from .exceptions import (
ClientException,
MissingRequiredAttributeException,
RedditAPIException,
)
from .objector import Objector
from .util import _deprecate_args
from .util.token_manager import BaseTokenManager
try:
from update_checker import update_check
UPDATE_CHECKER_MISSING = False
except ImportError: # pragma: no cover
UPDATE_CHECKER_MISSING = True
if TYPE_CHECKING: # pragma: no cover
import praw
Comment = models.Comment
Redditor = models.Redditor
Submission = models.Submission
Subreddit = models.Subreddit
logger = getLogger("praw")
class Reddit:
"""The Reddit class provides convenient access to Reddit's API.
Instances of this class are the gateway to interacting with Reddit's API through
PRAW. The canonical way to obtain an instance of this class is via:
.. code-block:: python
import praw
reddit = praw.Reddit(
client_id="CLIENT_ID",
client_secret="CLIENT_SECRET",
password="PASSWORD",
user_agent="USERAGENT",
username="USERNAME",
)
"""
update_checked = False
_ratelimit_regex = re.compile(r"([0-9]{1,3}) (milliseconds?|seconds?|minutes?)")
@property
def _next_unique(self) -> int:
value = self._unique_counter
self._unique_counter += 1
return value
@property
def read_only(self) -> bool:
"""Return ``True`` when using the ``ReadOnlyAuthorizer``."""
return self._core == self._read_only_core
@read_only.setter
def read_only(self, value: bool) -> None:
"""Set or unset the use of the ReadOnlyAuthorizer.
:raises: :class:`.ClientException` when attempting to unset ``read_only`` and
only the ``ReadOnlyAuthorizer`` is available.
"""
if value:
self._core = self._read_only_core
elif self._authorized_core is None:
raise ClientException(
"read_only cannot be unset as only the ReadOnlyAuthorizer is available."
)
else:
self._core = self._authorized_core
@property
def validate_on_submit(self) -> bool:
"""Get validate_on_submit.
.. deprecated:: 7.0
If property :attr:`.validate_on_submit` is set to ``False``, the behavior is
deprecated by Reddit. This attribute will be removed around May-June 2020.
"""
value = self._validate_on_submit
if value is False:
warn(
"Reddit will check for validation on all posts around May-June 2020. It"
" is recommended to check for validation by setting"
" reddit.validate_on_submit to True.",
category=DeprecationWarning,
stacklevel=3,
)
return value
@validate_on_submit.setter
def validate_on_submit(self, val: bool):
self._validate_on_submit = val
def __enter__(self):
"""Handle the context manager open."""
return self
def __exit__(self, *_args):
"""Handle the context manager close."""
@_deprecate_args(
"site_name",
"config_interpolation",
"requestor_class",
"requestor_kwargs",
"token_manager",
)
def __init__(
self,
site_name: Optional[str] = None,
*,
config_interpolation: Optional[str] = None,
requestor_class: Optional[Type[Requestor]] = None,
requestor_kwargs: Optional[Dict[str, Any]] = None,
token_manager: Optional[BaseTokenManager] = None,
**config_settings: Optional[Union[str, bool, int]],
): # noqa: D207, D301
"""Initialize a :class:`.Reddit` instance.
:param site_name: The name of a section in your ``praw.ini`` file from which to
load settings from. This parameter, in tandem with an appropriately
configured ``praw.ini``, file is useful if you wish to easily save
credentials for different applications, or communicate with other servers
running Reddit. If ``site_name`` is ``None``, then the site name will be
looked for in the environment variable ``praw_site``. If it is not found
there, the ``DEFAULT`` site will be used (default: ``None``).
:param config_interpolation: Config parser interpolation type that will be
passed to :class:`.Config` (default: ``None``).
:param requestor_class: A class that will be used to create a requestor. If not
set, use ``prawcore.Requestor`` (default: ``None``).
:param requestor_kwargs: Dictionary with additional keyword arguments used to
initialize the requestor (default: ``None``).
:param token_manager: When provided, the passed instance, a subclass of
:class:`.BaseTokenManager`, will manage tokens via two callback functions.
This parameter must be provided in order to work with refresh tokens
(default: ``None``).
Additional keyword arguments will be used to initialize the :class:`.Config`
object. This can be used to specify configuration settings during instantiation
of the :class:`.Reddit` instance. For more details, please see
:ref:`configuration`.
Required settings are:
- ``client_id``
- ``client_secret`` (for installed applications set this value to ``None``)
- ``user_agent``
The ``requestor_class`` and ``requestor_kwargs`` allow for customization of the
requestor :class:`.Reddit` will use. This allows, e.g., easily adding behavior
to the requestor or wrapping its |Session|_ in a caching layer. Example usage:
.. |Session| replace:: ``Session``
.. _session: https://2.python-requests.org/en/master/api/#requests.Session
.. code-block:: python
import json
import betamax
import requests
from prawcore import Requestor
from praw import Reddit
class JSONDebugRequestor(Requestor):
def request(self, *args, **kwargs):
response = super().request(*args, **kwargs)
print(json.dumps(response.json(), indent=4))
return response
my_session = betamax.Betamax(requests.Session())
reddit = Reddit(
..., requestor_class=JSONDebugRequestor, requestor_kwargs={"session": my_session}
)
"""
self._core = self._authorized_core = self._read_only_core = None
self._objector = None
self._token_manager = token_manager
self._unique_counter = 0
self._validate_on_submit = False
try:
config_section = site_name or os.getenv("praw_site") or "DEFAULT"
self.config = Config(
config_section, config_interpolation, **config_settings
)
except configparser.NoSectionError as exc:
help_message = (
"You provided the name of a praw.ini configuration which does not"
" exist.\n\nFor help with creating a Reddit instance,"
" visit\nhttps://praw.readthedocs.io/en/latest/code_overview/reddit_instance.html\n\nFor"
" help on configuring PRAW,"
" visit\nhttps://praw.readthedocs.io/en/latest/getting_started/configuration.html"
)
if site_name is not None:
exc.message += f"\n{help_message}"
raise
required_message = (
"Required configuration setting {!r} missing. \nThis setting can be"
" provided in a praw.ini file, as a keyword argument to the Reddit class"
" constructor, or as an environment variable."
)
for attribute in ("client_id", "user_agent"):
if getattr(self.config, attribute) in (self.config.CONFIG_NOT_SET, None):
raise MissingRequiredAttributeException(
required_message.format(attribute)
)
if self.config.client_secret is self.config.CONFIG_NOT_SET:
raise MissingRequiredAttributeException(
f"{required_message.format('client_secret')}\nFor installed"
" applications this value must be set to None via a keyword argument"
" to the Reddit class constructor."
)
self._check_for_update()
self._prepare_objector()
self._prepare_prawcore(
requestor_class=requestor_class, requestor_kwargs=requestor_kwargs
)
self.auth = models.Auth(self, None)
"""An instance of :class:`.Auth`.
Provides the interface for interacting with installed and web applications.
.. seealso::
:ref:`auth_url`
"""
self.drafts = models.DraftHelper(self, None)
"""An instance of :class:`.DraftHelper`.
Provides the interface for working with :class:`.Draft` instances.
For example, to list the currently authenticated user's drafts:
.. code-block:: python
drafts = reddit.drafts()
To create a draft on r/test run:
.. code-block:: python
reddit.drafts.create(title="title", selftext="selftext", subreddit="test")
"""
self.front = models.Front(self)
"""An instance of :class:`.Front`.
Provides the interface for interacting with front page listings. For example:
.. code-block:: python
for submission in reddit.front.hot():
print(submission)
"""
self.inbox = models.Inbox(self, None)
"""An instance of :class:`.Inbox`.
Provides the interface to a user's inbox which produces :class:`.Message`,
:class:`.Comment`, and :class:`.Submission` instances. For example, to iterate
through comments which mention the authorized user run:
.. code-block:: python
for comment in reddit.inbox.mentions():
print(comment)
"""
self.live = models.LiveHelper(self, None)
"""An instance of :class:`.LiveHelper`.
Provides the interface for working with :class:`.LiveThread` instances. At
present only new live threads can be created.
.. code-block:: python
reddit.live.create(title="title", description="description")
"""
self.multireddit = models.MultiredditHelper(self, None)
"""An instance of :class:`.MultiredditHelper`.
Provides the interface to working with :class:`.Multireddit` instances. For
example, you can obtain a :class:`.Multireddit` instance via:
.. code-block:: python
reddit.multireddit(redditor="samuraisam", name="programming")
"""
self.notes = models.RedditModNotes(self)
r"""An instance of :class:`.RedditModNotes`.
Provides the interface for working with :class:`.ModNote`\ s for multiple
redditors across multiple subreddits.
.. note::
The authenticated user must be a moderator of the provided subreddit(s).
For example, the latest note for u/spez in r/redditdev and r/test, and for
u/bboe in r/redditdev can be iterated through like so:
.. code-block:: python
redditor = reddit.redditor("bboe")
subreddit = reddit.subreddit("redditdev")
pairs = [(subreddit, "spez"), ("test", "spez"), (subreddit, redditor)]
for note in reddit.notes(pairs=pairs):
print(f"{note.label}: {note.note}")
"""
self.redditors = models.Redditors(self, None)
"""An instance of :class:`.Redditors`.
Provides the interface for :class:`.Redditor` discovery. For example, to iterate
over the newest Redditors, run:
.. code-block:: python
for redditor in reddit.redditors.new(limit=None):
print(redditor)
"""
self.subreddit = models.SubredditHelper(self, None)
"""An instance of :class:`.SubredditHelper`.
Provides the interface to working with :class:`.Subreddit` instances. For
example to create a :class:`.Subreddit` run:
.. code-block:: python
reddit.subreddit.create(name="coolnewsubname")
To obtain a lazy :class:`.Subreddit` instance run:
.. code-block:: python
reddit.subreddit("test")
Multiple subreddits can be combined and filtered views of r/all can also be used
just like a subreddit:
.. code-block:: python
reddit.subreddit("redditdev+learnpython+botwatch")
reddit.subreddit("all-redditdev-learnpython")
"""
self.subreddits = models.Subreddits(self, None)
"""An instance of :class:`.Subreddits`.
Provides the interface for :class:`.Subreddit` discovery. For example, to
iterate over the set of default subreddits run:
.. code-block:: python
for subreddit in reddit.subreddits.default(limit=None):
print(subreddit)
"""
self.user = models.User(self)
"""An instance of :class:`.User`.
Provides the interface to the currently authorized :class:`.Redditor`. For
example to get the name of the current user run:
.. code-block:: python
print(reddit.user.me())
"""
def _check_for_async(self):
if self.config.check_for_async: # pragma: no cover
try:
shell = get_ipython().__class__.__name__
if shell == "ZMQInteractiveShell":
return
except NameError:
pass
in_async = False
try:
asyncio.get_running_loop()
in_async = True
except Exception: # Quietly fail if any exception occurs during the check
pass
if in_async:
logger.warning(
"It appears that you are using PRAW in an asynchronous"
" environment.\nIt is strongly recommended to use Async PRAW:"
" https://asyncpraw.readthedocs.io.\nSee"
" https://praw.readthedocs.io/en/latest/getting_started/multiple_instances.html#discord-bots-and-asynchronous-environments"
" for more info.\n",
)
def _check_for_update(self):
if UPDATE_CHECKER_MISSING:
return
if not Reddit.update_checked and self.config.check_for_updates:
update_check(__package__, __version__)
Reddit.update_checked = True
def _prepare_common_authorizer(self, authenticator):
if self._token_manager is not None:
warn(
"Token managers have been deprecated and will be removed in the near"
" future. See https://www.reddit.com/r/redditdev/comments/olk5e6/"
"followup_oauth2_api_changes_regarding_refresh/ for more details.",
category=DeprecationWarning,
stacklevel=2,
)
if self.config.refresh_token:
raise TypeError(
"'refresh_token' setting cannot be provided when providing"
" 'token_manager'"
)
self._token_manager.reddit = self
authorizer = Authorizer(
authenticator,
post_refresh_callback=self._token_manager.post_refresh_callback,
pre_refresh_callback=self._token_manager.pre_refresh_callback,
)
elif self.config.refresh_token:
authorizer = Authorizer(
authenticator, refresh_token=self.config.refresh_token
)
else:
self._core = self._read_only_core
return
self._core = self._authorized_core = session(authorizer)
def _prepare_objector(self):
mappings = {
self.config.kinds["comment"]: models.Comment,
self.config.kinds["message"]: models.Message,
self.config.kinds["redditor"]: models.Redditor,
self.config.kinds["submission"]: models.Submission,
self.config.kinds["subreddit"]: models.Subreddit,
self.config.kinds["trophy"]: models.Trophy,
"Button": models.Button,
"Collection": models.Collection,
"Draft": models.Draft,
"DraftList": models.DraftList,
"Image": models.Image,
"LabeledMulti": models.Multireddit,
"Listing": models.Listing,
"LiveUpdate": models.LiveUpdate,
"LiveUpdateEvent": models.LiveThread,
"MenuLink": models.MenuLink,
"ModeratedList": models.ModeratedList,
"ModmailAction": models.ModmailAction,
"ModmailConversation": models.ModmailConversation,
"ModmailConversations-list": models.ModmailConversationsListing,
"ModmailMessage": models.ModmailMessage,
"Submenu": models.Submenu,
"TrophyList": models.TrophyList,
"UserList": models.RedditorList,
"UserSubreddit": models.UserSubreddit,
"button": models.ButtonWidget,
"calendar": models.Calendar,
"community-list": models.CommunityList,
"custom": models.CustomWidget,
"id-card": models.IDCard,
"image": models.ImageWidget,
"menu": models.Menu,
"modaction": models.ModAction,
"moderator-list": models.ModeratorListing,
"moderators": models.ModeratorsWidget,
"mod_note": models.ModNote,
"more": models.MoreComments,
"post-flair": models.PostFlairWidget,
"rule": models.Rule,
"stylesheet": models.Stylesheet,
"subreddit-rules": models.RulesWidget,
"textarea": models.TextArea,
"widget": models.Widget,
}
self._objector = Objector(self, mappings)
def _prepare_prawcore(self, *, requestor_class=None, requestor_kwargs=None):
requestor_class = requestor_class or Requestor
requestor_kwargs = requestor_kwargs or {}
requestor = requestor_class(
USER_AGENT_FORMAT.format(self.config.user_agent),
self.config.oauth_url,
self.config.reddit_url,
**requestor_kwargs,
)
if self.config.client_secret:
self._prepare_trusted_prawcore(requestor)
else:
self._prepare_untrusted_prawcore(requestor)
def _prepare_trusted_prawcore(self, requestor):
authenticator = TrustedAuthenticator(
requestor,
self.config.client_id,
self.config.client_secret,
self.config.redirect_uri,
)
read_only_authorizer = ReadOnlyAuthorizer(authenticator)
self._read_only_core = session(read_only_authorizer)
if self.config.username and self.config.password:
script_authorizer = ScriptAuthorizer(
authenticator, self.config.username, self.config.password
)
self._core = self._authorized_core = session(script_authorizer)
else:
self._prepare_common_authorizer(authenticator)
def _prepare_untrusted_prawcore(self, requestor):
authenticator = UntrustedAuthenticator(
requestor, self.config.client_id, self.config.redirect_uri
)
read_only_authorizer = DeviceIDAuthorizer(authenticator)
self._read_only_core = session(read_only_authorizer)
self._prepare_common_authorizer(authenticator)
@_deprecate_args("id", "url")
def comment(
self, # pylint: disable=invalid-name
id: Optional[str] = None, # pylint: disable=redefined-builtin
*,
url: Optional[str] = None,
):
"""Return a lazy instance of :class:`.Comment`.
:param id: The ID of the comment.
:param url: A permalink pointing to the comment.
.. note::
If you want to obtain the comment's replies, you will need to call
:meth:`~.Comment.refresh` on the returned :class:`.Comment`.
"""
return models.Comment(self, id=id, url=url)
def domain(self, domain: str):
"""Return an instance of :class:`.DomainListing`.
:param domain: The domain to obtain submission listings for.
"""
return models.DomainListing(self, domain)
@_deprecate_args("path", "params")
def get(
self,
path: str,
*,
params: Optional[Union[str, Dict[str, Union[str, int]]]] = None,
):
"""Return parsed objects returned from a GET request to ``path``.
:param path: The path to fetch.
:param params: The query parameters to add to the request (default: ``None``).
"""
return self._objectify_request(method="GET", params=params, path=path)
@_deprecate_args("fullnames", "url", "subreddits")
def info(
self,
*,
fullnames: Optional[Iterable[str]] = None,
subreddits: Optional[Iterable[Union["praw.models.Subreddit", str]]] = None,
url: Optional[str] = None,
) -> Generator[
Union["praw.models.Subreddit", "praw.models.Comment", "praw.models.Submission"],
None,
None,
]:
"""Fetch information about each item in ``fullnames``, ``url``, or ``subreddits``.
:param fullnames: A list of fullnames for comments, submissions, and/or
subreddits.
:param subreddits: A list of subreddit names or :class:`.Subreddit` objects to
retrieve subreddits from.
:param url: A url (as a string) to retrieve lists of link submissions from.
:returns: A generator that yields found items in their relative order.
Items that cannot be matched will not be generated. Requests will be issued in
batches for each 100 fullnames.
.. note::
For comments that are retrieved via this method, if you want to obtain its
replies, you will need to call :meth:`~.Comment.refresh` on the yielded
:class:`.Comment`.
.. note::
When using the URL option, it is important to be aware that URLs are treated
literally by Reddit's API. As such, the URLs ``"youtube.com"`` and
``"https://www.youtube.com"`` will provide a different set of submissions.
"""
none_count = (fullnames, url, subreddits).count(None)
if none_count != 2:
raise TypeError(
"Either 'fullnames', 'url', or 'subreddits' must be provided."
)
is_using_fullnames = fullnames is not None
ids_or_names = fullnames if is_using_fullnames else subreddits
if ids_or_names is not None:
if isinstance(ids_or_names, str):
raise TypeError(
"'fullnames' and 'subreddits' must be a non-str iterable."
)
api_parameter_name = "id" if is_using_fullnames else "sr_name"
def generator(names):
if is_using_fullnames:
iterable = iter(names)
else:
iterable = iter([str(item) for item in names])
while True:
chunk = list(islice(iterable, 100))
if not chunk:
break
params = {api_parameter_name: ",".join(chunk)}
for result in self.get(API_PATH["info"], params=params):
yield result
return generator(ids_or_names)
def generator(url):
params = {"url": url}
for result in self.get(API_PATH["info"], params=params):
yield result
return generator(url)
def _objectify_request(
self,
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
files: Optional[Dict[str, IO]] = None,
json: Optional[Dict[Any, Any]] = None,
method: str = "",
params: Optional[Union[str, Dict[str, str]]] = None,
path: str = "",
) -> Any:
"""Run a request through the ``Objector``.
:param data: Dictionary, bytes, or file-like object to send in the body of the
request (default: ``None``).
:param files: Dictionary, filename to file (like) object mapping (default:
``None``).
:param json: JSON-serializable object to send in the body of the request with a
Content-Type header of application/json (default: ``None``). If ``json`` is
provided, ``data`` should not be.
:param method: The HTTP method (e.g., ``"GET"``, ``"POST"``, ``"PUT"``,
``"DELETE"``).
:param params: The query parameters to add to the request (default: ``None``).
:param path: The path to fetch.
"""
return self._objector.objectify(
self.request(
data=data,
files=files,
json=json,
method=method,
params=params,
path=path,
)
)
def _handle_rate_limit(
self, exception: RedditAPIException
) -> Optional[Union[int, float]]:
for item in exception.items:
if item.error_type == "RATELIMIT":
amount_search = self._ratelimit_regex.search(item.message)
if not amount_search:
break
seconds = int(amount_search.group(1))
if amount_search.group(2).startswith("minute"):
seconds *= 60
elif amount_search.group(2).startswith("millisecond"):
seconds = 0
if seconds <= int(self.config.ratelimit_seconds):
sleep_seconds = seconds + 1
return sleep_seconds
return None
@_deprecate_args("path", "data", "json", "params")
def delete(
self,
path: str,
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
json: Optional[Dict[Any, Any]] = None,
params: Optional[Union[str, Dict[str, str]]] = None,
) -> Any:
"""Return parsed objects returned from a DELETE request to ``path``.
:param path: The path to fetch.
:param data: Dictionary, bytes, or file-like object to send in the body of the
request (default: ``None``).
:param json: JSON-serializable object to send in the body of the request with a
Content-Type header of application/json (default: ``None``). If ``json`` is
provided, ``data`` should not be.
:param params: The query parameters to add to the request (default: ``None``).
"""
return self._objectify_request(
data=data, json=json, method="DELETE", params=params, path=path
)
@_deprecate_args("path", "data", "json")
def patch(
self,
path: str,
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
json: Optional[Dict[Any, Any]] = None,
) -> Any:
"""Return parsed objects returned from a PATCH request to ``path``.
:param path: The path to fetch.
:param data: Dictionary, bytes, or file-like object to send in the body of the
request (default: ``None``).
:param json: JSON-serializable object to send in the body of the request with a
Content-Type header of application/json (default: ``None``). If ``json`` is
provided, ``data`` should not be.
"""
return self._objectify_request(data=data, json=json, method="PATCH", path=path)
@_deprecate_args("path", "data", "files", "params", "json")
def post(
self,
path: str,
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
files: Optional[Dict[str, IO]] = None,
json: Optional[Dict[Any, Any]] = None,
params: Optional[Union[str, Dict[str, str]]] = None,
) -> Any:
"""Return parsed objects returned from a POST request to ``path``.
:param path: The path to fetch.
:param data: Dictionary, bytes, or file-like object to send in the body of the
request (default: ``None``).
:param files: Dictionary, filename to file (like) object mapping (default:
``None``).
:param json: JSON-serializable object to send in the body of the request with a
Content-Type header of application/json (default: ``None``). If ``json`` is
provided, ``data`` should not be.
:param params: The query parameters to add to the request (default: ``None``).
"""
if json is None:
data = data or {}
attempts = 3
last_exception = None
while attempts > 0:
attempts -= 1
try:
return self._objectify_request(
data=data,
files=files,
json=json,
method="POST",
params=params,
path=path,
)
except RedditAPIException as exception:
last_exception = exception
seconds = self._handle_rate_limit(exception=exception)
if seconds is None:
break
second_string = "second" if seconds == 1 else "seconds"
logger.debug(f"Rate limit hit, sleeping for {seconds} {second_string}")
time.sleep(seconds)
raise last_exception
@_deprecate_args("path", "data", "json")
def put(
self,
path: str,
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
json: Optional[Dict[Any, Any]] = None,
):
"""Return parsed objects returned from a PUT request to ``path``.
:param path: The path to fetch.
:param data: Dictionary, bytes, or file-like object to send in the body of the
request (default: ``None``).
:param json: JSON-serializable object to send in the body of the request with a
Content-Type header of application/json (default: ``None``). If ``json`` is
provided, ``data`` should not be.
"""
return self._objectify_request(data=data, json=json, method="PUT", path=path)
@_deprecate_args("nsfw")
def random_subreddit(self, *, nsfw: bool = False) -> "praw.models.Subreddit":
"""Return a random lazy instance of :class:`.Subreddit`.
:param nsfw: Return a random NSFW (not safe for work) subreddit (default:
``False``).
"""
url = API_PATH["subreddit"].format(subreddit="randnsfw" if nsfw else "random")
path = None
try:
self.get(url, params={"unique": self._next_unique})
except Redirect as redirect:
path = redirect.path
return models.Subreddit(self, path.split("/")[2])
@_deprecate_args("name", "fullname")
def redditor(
self, name: Optional[str] = None, *, fullname: Optional[str] = None
) -> "praw.models.Redditor":
"""Return a lazy instance of :class:`.Redditor`.
:param name: The name of the redditor.
:param fullname: The fullname of the redditor, starting with ``t2_``.
Either ``name`` or ``fullname`` can be provided, but not both.
"""
return models.Redditor(self, name=name, fullname=fullname)
@_deprecate_args("method", "path", "params", "data", "files", "json")
def request(
self,
*,
data: Optional[Union[Dict[str, Union[str, Any]], bytes, IO, str]] = None,
files: Optional[Dict[str, IO]] = None,
json: Optional[Dict[Any, Any]] = None,
method: str,
params: Optional[Union[str, Dict[str, Union[str, int]]]] = None,
path: str,
) -> Any:
"""Return the parsed JSON data returned from a request to URL.
:param data: Dictionary, bytes, or file-like object to send in the body of the
request (default: ``None``).
:param files: Dictionary, filename to file (like) object mapping (default:
``None``).
:param json: JSON-serializable object to send in the body of the request with a
Content-Type header of application/json (default: ``None``). If ``json`` is
provided, ``data`` should not be.
:param method: The HTTP method (e.g., ``"GET"``, ``"POST"``, ``"PUT"``,
``"DELETE"``).
:param params: The query parameters to add to the request (default: ``None``).
:param path: The path to fetch.
"""
if self.config.check_for_async:
self._check_for_async()
if data and json:
raise ClientException("At most one of 'data' or 'json' is supported.")
try:
return self._core.request(
data=data,
files=files,
json=json,
method=method,
params=params,
path=path,
)
except BadRequest as exception:
try:
data = exception.response.json()
except ValueError:
if exception.response.text:
data = {"reason": exception.response.text}
else:
raise exception
if set(data) == {"error", "message"}:
raise
explanation = data.get("explanation")
if "fields" in data:
assert len(data["fields"]) == 1
field = data["fields"][0]
else:
field = None
raise RedditAPIException(
[data["reason"], explanation, field]
) from exception
@_deprecate_args("id", "url")
def submission( # pylint: disable=invalid-name,redefined-builtin
self, id: Optional[str] = None, *, url: Optional[str] = None
) -> "praw.models.Submission":
"""Return a lazy instance of :class:`.Submission`.
:param id: A Reddit base36 submission ID, e.g., ``"2gmzqe"``.
:param url: A URL supported by :meth:`.Submission.id_from_url`.
Either ``id`` or ``url`` can be provided, but not both.
"""
return models.Submission(self, id=id, url=url)
def username_available(self, name: str) -> bool:
"""Check to see if the username is available.
For example, to check if the username ``bboe`` is available, try:
.. code-block:: python
reddit.username_available("bboe")
"""
return self._objectify_request(
method="GET", params={"user": name}, path=API_PATH["username_available"]
)