Skip to content
This repository was archived by the owner on Oct 4, 2023. It is now read-only.

Commit 61b4974

Browse files
committed
Finishing comment functionality
1 parent d8d94ef commit 61b4974

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

imgur-python/client.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from imgur.models.account import Account
55
from imgur.models.comment import Comment
66
from helpers.error import ImgurClientError
7+
from helpers.format import build_comment_tree
78
from imgur.models.gallery_album import GalleryAlbum
89
from imgur.models.gallery_image import GalleryImage
910
from imgur.models.account_settings import AccountSettings
@@ -92,7 +93,9 @@ def make_request(self, method, route, data=None):
9293
else:
9394
response = method_to_call(url, headers=header, data=data)
9495

95-
# TODO: Add rate-limit checks
96+
# Rate-limit check
97+
if response.status_code == 429:
98+
raise ImgurClientError('Rate-limit exceeded!')
9699

97100
try:
98101
response_data = response.json()
@@ -232,6 +235,7 @@ def get_account_images_count(self, username, page=0):
232235
self.validate_user_context(username)
233236
return self.make_request('GET', 'account/%s/images/ids/%d' % (username, page))
234237

238+
# Album-related endpoints
235239
def get_album(self, album_id):
236240
album = self.make_request('GET', 'album/%s' % album_id)
237241
return Album(album)
@@ -279,4 +283,35 @@ def album_remove_images(self, album_id, ids):
279283
if isinstance(ids, list):
280284
ids = ','.join(ids)
281285

282-
return self.make_request('DELETE', 'album/%s/remove_images' % album_id, {'ids': ids})
286+
return self.make_request('DELETE', 'album/%s/remove_images' % album_id, {'ids': ids})
287+
288+
# Comment-related endpoints
289+
def get_comment(self, comment_id):
290+
comment = self.make_request('GET', 'comment/%d' % comment_id)
291+
return Comment(comment)
292+
293+
def delete_comment(self, comment_id):
294+
self.logged_in()
295+
return self.make_request('DELETE', 'comment/%d' % comment_id)
296+
297+
def get_comment_replies(self, comment_id):
298+
replies = self.make_request('GET', 'comment/%d/replies' % comment_id)
299+
replies['children'] = build_comment_tree(replies['children'])
300+
301+
return replies
302+
303+
def post_comment_reply(self, comment_id, image_id, comment):
304+
data = {
305+
'image_id': image_id,
306+
'comment': comment
307+
}
308+
309+
return self.make_request('POST', 'comment/%d' % comment_id, data)
310+
311+
def comment_vote(self, comment_id, vote, toggle=True):
312+
toggle_behavior = 1 if toggle else 0
313+
314+
return self.make_request('POST', 'comment/%d/vote/%s?toggle=%d' % (comment_id, vote, toggle_behavior))
315+
316+
def comment_report(self, comment_id):
317+
return self.make_request('POST', 'comment/%d/report' % comment_id)

imgur-python/helpers/format.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import math
2+
from imgur.models.comment import Comment
23

34

45
def center_pad(s, length):
@@ -11,4 +12,14 @@ def center_pad(s, length):
1112

1213
def two_column_with_period(left, right, length):
1314
num_periods = int(length - (len(left) + len(right) + 2))
14-
return left + ' ' + ('.' * num_periods) + ' ' + right
15+
return left + ' ' + ('.' * num_periods) + ' ' + right
16+
17+
18+
def build_comment_tree(children):
19+
children_objects = []
20+
for child in children:
21+
to_insert = Comment(child)
22+
to_insert.children = build_comment_tree(to_insert.children)
23+
children_objects.append(to_insert)
24+
25+
return children_objects

0 commit comments

Comments
 (0)