@@ -10,8 +10,23 @@ def get_boards(board_name_list, *args, **kwargs):
10
10
return [Board (name , * args , ** kwargs ) for name in board_name_list ]
11
11
12
12
class Board (object ):
13
+ """Represents a 4chan board.
14
+
15
+ Attributes:
16
+ name (str): Name of this board, such as ``tg`` or ``k``
17
+ """
13
18
def __init__ (self , board_name , https = False , clean_comments = True ,
14
19
api_url = URL ['api' ], session = None ):
20
+ """Creates a :mod:`basc_py4chan.Board` object.
21
+
22
+ Args:
23
+ board_name (string): Name of the board, such as "tg" or "etc".
24
+ https (bool): Whether to use a secure connection to 4chan.
25
+ clean_comments (bool): Whether post objects will try to parse HTML comments
26
+ (HTML entities, tags and links) into "cleaned" plaintext.
27
+ api_url: Base 4chan API URL. This will be automatically set in all cases.
28
+ session: Existing requests.session object to use instead of our current one.
29
+ """
15
30
self ._board_name = board_name
16
31
self ._protocol = 'https://' if https else 'http://'
17
32
self ._clean_comments = clean_comments
@@ -33,12 +48,14 @@ def _get_json(self, path):
33
48
return res .json ()
34
49
35
50
def get_thread (self , thread_id , update_if_cached = True ):
36
- """
37
- Get a thread from 4chan via 4chan API.
51
+ """Get a thread from 4chan via 4chan API.
52
+
53
+ Args:
54
+ thread_id (int): Thread ID
55
+ update_if_cached (bool): Whether the thread should be updated if it's already in our cache
38
56
39
- :param thread_id: Thread ID
40
- :param update_if_cached: Should the thread be updated if it's found in the cache?
41
- :return: Thread
57
+ Returns:
58
+ :class:`basc_py4chan.Thread`: Thread object
42
59
"""
43
60
if thread_id in self ._thread_cache :
44
61
thread = self ._thread_cache [thread_id ]
@@ -55,11 +72,13 @@ def get_thread(self, thread_id, update_if_cached=True):
55
72
return thread
56
73
57
74
def thread_exists (self , thread_id ):
58
- """
59
- Check if a thread exists, or is 404.
75
+ """Check if a thread exists or has 404'd.
60
76
61
- :param thread_id: Thread ID
62
- :return: bool
77
+ Args:
78
+ thread_id (int): Thread ID
79
+
80
+ Returns:
81
+ bool: Whether the given thread exists on this board.
63
82
"""
64
83
return self ._requests_session .head (self ._thread_path % thread_id ).ok
65
84
@@ -96,38 +115,48 @@ def _request_threads(self, page):
96
115
return threads
97
116
98
117
def get_threads (self , page = 1 ):
99
- """
100
- Get all threads on a certain page. Pages are now indexed at 1, instead of 0.
101
- If the thread is already in cache, return cached thread entry.
118
+ """Returns all threads on a certain page.
119
+
120
+ Gets a list of Thread objects for every thread on the given page. If a thread is
121
+ already in our cache, the cached version is returned and thread.want_update is
122
+ set to True on the specific thread object.
123
+
124
+ Pages on 4chan are indexed from 1 onwards.
125
+
126
+ Args:
127
+ page (int): Page to request threads for. Defaults to the first page.
102
128
103
- Sets thread.want_update to True if the thread is being returned from the cache.
104
- :param: page: Page to fetch thread from
105
- :return: list[Thread]
129
+ Returns:
130
+ list: List of Thread objects representing the threads on the given page.
106
131
"""
107
132
return self ._request_threads (page )
108
133
109
134
def get_all_thread_ids (self ):
110
- """
111
- Return a list of all thread IDs.
135
+ """Return the ID of every thread on this board.
112
136
113
- :return: list[int]
137
+ Returns:
138
+ list: List of IDs of every thread on this board.
114
139
"""
115
140
json = self ._get_json (ALL_THREADS )
116
141
return [thread ['no' ] for page in json for thread in page ['threads' ]]
117
142
118
143
def get_all_threads (self , expand = False ):
119
- """
120
- Return all threads on all pages.
144
+ """Return every thread on this board.
145
+
146
+ If not expanded, result is same as get_threads run across all board pages,
147
+ with last 3-5 replies included.
121
148
122
- If not expanded, result is same as get_threads run across all board pages,
123
- with last 3-5 replies included .
149
+ Uses the catalog when not expanding, and uses the flat thread ID listing
150
+ at /{board}/threads.json when expanding for more efficient resource usage .
124
151
125
- Uses the catalog when not expanding, and uses the flat thread ID listing
126
- at /{board}/threads.json when expanding for more efficient resource usage.
152
+ If expanded, all data of all threads is returned with no omitted posts.
127
153
128
- If expanded, all data of all threads is returned with no omitted posts.
129
- :param: expand: Whether or not to expand threads
130
- :return: list[Thread]
154
+ Args:
155
+ expand (bool): Whether to download every single post of every thread.
156
+ If enabled, this option can be very slow and bandwidth-intensive.
157
+
158
+ Returns:
159
+ list: List of Thread objects representing every thread on this board.
131
160
"""
132
161
if not expand :
133
162
return self ._request_threads (CATALOG )
@@ -138,27 +167,19 @@ def get_all_threads(self, expand=False):
138
167
return filter (None , threads )
139
168
140
169
def refresh_cache (self , if_want_update = False ):
141
- """
142
- Update all threads currently stored in the cache.
143
- """
170
+ """Update all threads currently stored in our cache."""
144
171
for thread in self ._thread_cache .values ():
145
172
if if_want_update :
146
173
if not thread .want_update :
147
174
continue
148
175
thread .update ()
149
176
150
177
def clear_cache (self ):
151
- """
152
- Remove everything currently stored in the cache.
153
- """
178
+ """Remove everything currently stored in our cache."""
154
179
self ._thread_cache .clear ()
155
180
156
181
@property
157
182
def name (self ):
158
- """
159
- Board name that this board represents.
160
- :return: string
161
- """
162
183
return self ._board_name
163
184
164
185
def __repr__ (self ):
0 commit comments