File: quick_start.rst

package info (click to toggle)
praw 7.7.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 149,360 kB
  • sloc: python: 16,669; makefile: 131
file content (310 lines) | stat: -rw-r--r-- 11,019 bytes parent folder | download
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
Quick Start
===========

In this section, we go over everything you need to know to start building scripts or
bots using PRAW, the Python Reddit API Wrapper. It's fun and easy. Let's get started.

Prerequisites
-------------

:Python Knowledge: You need to know at least a little Python to use PRAW. PRAW supports
    `Python 3.7+`_. If you are stuck on a problem, `r/learnpython`_ is a great place to
    ask for help.
:Reddit Knowledge: A basic understanding of how Reddit works is a must. In the event you
    are not already familiar with Reddit start at `Reddit Help`_.
:Reddit Account: A Reddit account is required to access Reddit's API. Create one at
    reddit.com_.
:Client ID & Client Secret: These two values are needed to access Reddit's API as a
    **script** application (see :ref:`oauth` for other application types). If you don't
    already have a client ID and client secret, follow Reddit's `First Steps Guide`_ to
    create them.
:User Agent: A user agent is a unique identifier that helps Reddit determine the source
    of network requests. To use Reddit's API, you need a unique and descriptive user
    agent. The recommended format is ``<platform>:<app ID>:<version string> (by
    u/<Reddit username>)``. For example, ``android:com.example.myredditapp:v1.2.3 (by
    u/kemitche)``. Read more about user agents at `Reddit's API wiki page`_.

.. _first steps guide: https://github.com/reddit/reddit/wiki/OAuth2-Quick-Start-Example#first-steps

.. _python 3.7+: https://docs.python.org/3/tutorial/index.html

.. _r/learnpython: https://www.reddit.com/r/learnpython/

.. _reddit help: https://www.reddithelp.com/en

.. _reddit's api wiki page: https://github.com/reddit/reddit/wiki/API

.. _reddit.com: https://www.reddit.com

With these prerequisites satisfied, you are ready to learn how to do some of the most
common tasks with Reddit's API.

Common Tasks
------------

Obtain a :class:`.Reddit` Instance
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. warning::

    For the sake of brevity, the following examples pass authentication information via
    arguments to :meth:`praw.Reddit`. If you do this, you need to be careful not to
    reveal this information to the outside world if you share your code. It is
    recommended to use a :ref:`praw.ini file <praw.ini>` in order to keep your
    authentication information separate from your code.

You need an instance of the :class:`.Reddit` class to do *anything* with PRAW. There are
two distinct states a :class:`.Reddit` instance can be in: :ref:`read-only <read-only>`,
and :ref:`authorized <authorized>`.

.. _read-only:

Read-only :class:`.Reddit` Instances
++++++++++++++++++++++++++++++++++++

To create a read-only :class:`.Reddit` instance, you need three pieces of information:

1. Client ID
2. Client secret
3. User agent

You may choose to provide these by passing in three keyword arguments when calling the
initializer of the :class:`.Reddit` class: ``client_id``, ``client_secret``,
``user_agent`` (see :ref:`configuration` for other methods of providing this
information). For example:

.. code-block:: python

    import praw

    reddit = praw.Reddit(
        client_id="my client id",
        client_secret="my client secret",
        user_agent="my user agent",
    )

Just like that, you now have a read-only :class:`.Reddit` instance.

.. code-block:: python

    print(reddit.read_only)
    # Output: True

With a read-only instance, you can do something like obtaining 10 "hot" submissions from
``r/test``:

.. code-block:: python

    # continued from code above

    for submission in reddit.subreddit("test").hot(limit=10):
        print(submission.title)

    # Output: 10 submissions

If you want to do more than retrieve public information from Reddit, then you need an
authorized :class:`.Reddit` instance.

.. note::

    In the above example we are limiting the results to ``10``. Without the ``limit``
    parameter PRAW should yield as many results as it can with a single request. For
    most endpoints this results in 100 items per request. If you want to retrieve as
    many as possible pass in ``limit=None``.

.. _authorized:

Authorized :class:`.Reddit` Instances
+++++++++++++++++++++++++++++++++++++

In order to create an authorized :class:`.Reddit` instance, two additional pieces of
information are required for **script** applications (see :ref:`oauth` for other
application types):

4. Your Reddit username, and
5. Your Reddit password

Again, you may choose to provide these by passing in keyword arguments ``username`` and
``password`` when you call the :class:`.Reddit` initializer, like the following:

.. code-block:: python

    import praw

    reddit = praw.Reddit(
        client_id="my client id",
        client_secret="my client secret",
        password="my password",
        user_agent="my user agent",
        username="my username",
    )

    print(reddit.read_only)
    # Output: False

Now you can do whatever your Reddit account is authorized to do. And you can switch back
to read-only mode whenever you want:

.. code-block:: python

    # continued from code above
    reddit.read_only = True

.. note::

    If you are uncomfortable hard-coding your credentials into your program, there are
    some options available to you. Please see: :ref:`configuration`.

Obtain a :class:`.Subreddit`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To obtain a :class:`.Subreddit` instance, pass the subreddit's name when calling
``subreddit`` on your :class:`.Reddit` instance. For example:

.. code-block:: python

    # assume you have a praw.Reddit instance bound to variable `reddit`
    subreddit = reddit.subreddit("redditdev")

    print(subreddit.display_name)
    # Output: redditdev
    print(subreddit.title)
    # Output: reddit development
    print(subreddit.description)
    # Output: a subreddit for discussion of ...

Obtain :class:`.Submission` Instances from a :class:`.Subreddit`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now that you have a :class:`.Subreddit` instance, you can iterate through some of its
submissions, each bound to an instance of :class:`.Submission`. There are several sorts
that you can iterate through:

- controversial
- gilded
- hot
- new
- rising
- top

.. _submission-iteration:

Each of these methods will immediately return a :class:`.ListingGenerator`, which is to
be iterated through. For example, to iterate through the first 10 submissions based on
the ``hot`` sort for a given subreddit try:

.. code-block:: python

    # assume you have a Subreddit instance bound to variable `subreddit`
    for submission in subreddit.hot(limit=10):
        print(submission.title)
        # Output: the submission's title
        print(submission.score)
        # Output: the submission's score
        print(submission.id)
        # Output: the submission's ID
        print(submission.url)
        # Output: the URL the submission points to or the submission's URL if it's a self post

.. note::

    The act of calling a method that returns a :class:`.ListingGenerator` does not
    result in any network requests until you begin to iterate through the
    :class:`.ListingGenerator`.

You can create :class:`.Submission` instances in other ways too:

.. code-block:: python

    # assume you have a praw.Reddit instance bound to variable `reddit`
    submission = reddit.submission("39zje0")
    print(submission.title)
    # Output: reddit will soon only be available ...

    # or
    submission = reddit.submission(url="https://www.reddit.com/...")

Obtain :class:`.Redditor` Instances
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There are several ways to obtain a redditor (a :class:`.Redditor` instance). Two of the
most common ones are:

- via the ``author`` attribute of a :class:`.Submission` or :class:`.Comment` instance
- via the :meth:`.redditor` method of :class:`.Reddit`

For example:

.. code-block:: python

    # assume you have a Submission instance bound to variable `submission`
    redditor1 = submission.author
    print(redditor1.name)
    # Output: name of the redditor

    # assume you have a praw.Reddit instance bound to variable `reddit`
    redditor2 = reddit.redditor("bboe")
    print(redditor2.link_karma)
    # Output: u/bboe's karma

Obtain :class:`.Comment` Instances
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Submissions have a ``comments`` attribute that is a :class:`.CommentForest` instance.
That instance is iterable and represents the top-level comments of the submission by the
default comment sort (``confidence``). If you instead want to iterate over *all*
comments as a flattened list you can call the :meth:`.list` method on a
:class:`.CommentForest` instance. For example:

.. code-block:: python

    # assume you have a praw.Reddit instance bound to variable `reddit`
    top_level_comments = list(submission.comments)
    all_comments = submission.comments.list()

.. note::

    The comment sort order can be changed by updating the value of ``comment_sort`` on
    the :class:`.Submission` instance prior to accessing ``comments`` (see:
    `/api/set_suggested_sort
    <https://www.reddit.com/dev/api#POST_api_set_suggested_sort>`_ for possible values).
    For example to have comments sorted by ``new`` try something like:

    .. code-block:: python

        # assume you have a praw.Reddit instance bound to variable `reddit`
        submission = reddit.submission("39zje0")
        submission.comment_sort = "new"
        top_level_comments = list(submission.comments)

As you may be aware there will periodically be :class:`.MoreComments` instances
scattered throughout the forest. Replace those :class:`.MoreComments` instances at any
time by calling :meth:`.replace_more` on a :class:`.CommentForest` instance. Calling
:meth:`.replace_more` access ``comments``, and so must be done after ``comment_sort`` is
updated. See :ref:`extracting_comments` for an example.

.. _determine-available-attributes-of-an-object:

Determine Available Attributes of an Object
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you have a PRAW object, e.g., :class:`.Comment`, :class:`.Message`,
:class:`.Redditor`, or :class:`.Submission`, and you want to see what attributes are
available along with their values, use the built-in :py:func:`vars` function of python.
For example:

.. code-block:: python

    import pprint

    # assume you have a praw.Reddit instance bound to variable `reddit`
    submission = reddit.submission("39zje0")
    print(submission.title)  # to make it non-lazy
    pprint.pprint(vars(submission))

Note the line where we print the title. PRAW uses lazy objects so that network requests
to Reddit's API are only issued when information is needed. Here, before the print line,
``submission`` points to a lazy :class:`.Submission` object. When we try to print its
title, additional information is needed, thus a network request is made, and the
instances ceases to be lazy. Outputting all the attributes of a lazy object will result
in fewer attributes than expected.