Skip to content

Commit

Permalink
Merge pull request #7 from faustocarrera/develop
Browse files Browse the repository at this point in the history
Image, comments and album features
  • Loading branch information
faustocarrera authored Apr 3, 2020
2 parents 5f9e24d + 149f957 commit e057881
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 38 deletions.
15 changes: 15 additions & 0 deletions imgur_python/Account.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,18 @@ def unfollow_tag(self, tagname):
}
request = requests.delete(url, headers=headers)
return self.response(request, url)

def notifications(self, new):
"Returns all of the reply notifications for the user"
url = '{0}/3/account/{1}/notifications/replies'.format(
self.api_url,
self.config['account_username']
)
headers = {
'authorization': 'Bearer {0}'.format(self.config['access_token'])
}
payload = {
'new': new
}
request = requests.get(url, headers=headers, data=payload)
return self.response(request, url)
46 changes: 43 additions & 3 deletions imgur_python/Album.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ def __init__(self, config, api_url):
self.config = config
self.api_url = api_url

def albums(self, username, page):
def albums(self, page):
"Get all the albums associated with the account"
url = '{0}/3/account/{1}/albums/{2}'.format(
self.api_url,
username,
self.config['account_username'],
page
)
headers = {
'authorization': 'Bearer {0}'.format(self.config['access_token'])
}
request = requests.get(url, headers=headers)
return self.response(request, url)
response = self.response(request, url)
# the total number of comments
response['response']['total'] = self.album_count()
return response

def album(self, album_id):
"Get additional information about an album"
Expand Down Expand Up @@ -90,3 +93,40 @@ def remove(self, delete_hash, payload):
}
request = requests.post(url, headers=headers, data=payload)
return self.response(request, url)

def album_ids(self, page):
"Return an array of all of the album IDs"
url = '{0}/3/account/{1}/albums/ids/{2}'.format(
self.api_url,
self.config['account_username'],
page
)
headers = {
'authorization': 'Bearer {0}'.format(self.config['access_token'])
}
request = requests.get(url, headers=headers)
response = self.response(request, url)
# the total number of comments
response['response']['total'] = self.album_count()
return response

def album_fav(self, album_id):
"Favorite an album with a given ID"
url = '{0}/3/album/{1}/favorite'.format(self.api_url, album_id)
headers = {
'authorization': 'Bearer {0}'.format(self.config['access_token'])
}
request = requests.post(url, headers=headers)
return self.response(request, url)

def album_count(self):
"Return the total number of albums associated with the account"
url = '{0}/3/account/{1}/albums/count'.format(
self.api_url,
self.config['account_username']
)
headers = {
'authorization': 'Bearer {0}'.format(self.config['access_token'])
}
request = requests.get(url, headers=headers)
return self.data(request)
52 changes: 48 additions & 4 deletions imgur_python/Comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@ def __init__(self, config, api_url):
self.config = config
self.api_url = api_url

def comments(self, username, page=0, sort='newest'):
def comments(self, page=0, sort='newest'):
"Return the comments the user has created"
url = '{0}/3/account/{1}/comments/{2}/{3}'.format(
self.api_url,
username,
self.config['account_username'],
sort,
page
)
headers = {
'authorization': 'Bearer {0}'.format(self.config['access_token'])
}
request = requests.get(url, headers=headers)
return self.response(request, url)
response = self.response(request, url)
# the total number of comments
response['response']['total'] = self.comment_count()
return response

def comment(self, comment_id):
"Get information about a specific comment"
url = '{0}/3/comment/{1}/replies'.format(self.api_url, comment_id)
url = '{0}/3/comment/{1}'.format(self.api_url, comment_id)
headers = {
'authorization': 'Client-ID {0}'.format(self.config['client_id'])
}
Expand Down Expand Up @@ -80,3 +83,44 @@ def report(self, comment_id, reason):
}
request = requests.post(url, headers=headers, data=payload)
return self.response(request, url)

def comment_ids(self, page, sort):
"Return an array of all of the comment IDs"
url = '{0}/3/account/{1}/comments/ids/{2}/{3}'.format(
self.api_url,
self.config['account_username'],
sort,
page
)
headers = {
'authorization': 'Client-ID {0}'.format(self.config['client_id'])
}
request = requests.get(url, headers=headers)
response = self.response(request, url)
# the total number of comments
response['response']['total'] = self.comment_count()
return response

def comment_replies(self, comment_id):
"Get the comment with all of the replies for the comment"
url = '{0}/3/comment/{1}/replies'.format(
self.api_url,
comment_id
)
headers = {
'authorization': 'Client-ID {0}'.format(self.config['client_id'])
}
request = requests.get(url, headers=headers)
return self.response(request, url)

def comment_count(self):
"Return a count of all of the comments associated with the account"
url = '{0}/3/account/{1}/comments/count'.format(
self.api_url,
self.config['account_username']
)
headers = {
'authorization': 'Client-ID {0}'.format(self.config['client_id'])
}
request = requests.get(url, headers=headers)
return self.data(request)
47 changes: 43 additions & 4 deletions imgur_python/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ def __init__(self, config, api_url):
self.config = config
self.api_url = api_url

def images(self, username, page):
def images(self, page):
"Get account images"
url = '{0}/3/account/{1}/images/{2}'.format(
url = '{0}/3/account/me/images/{1}'.format(
self.api_url,
username,
page
)
headers = {
'authorization': 'Bearer {0}'.format(self.config['access_token'])
}
request = requests.get(url, headers=headers)
return self.response(request, url)
response = self.response(request, url)
# the total number of comments
response['response']['total'] = self.image_count()
return response

def image(self, image_id):
"Get information about an image"
Expand Down Expand Up @@ -71,3 +73,40 @@ def delete(self, image_id):
}
request = requests.delete(url, headers=headers)
return self.response(request, url)

def image_ids(self, page):
"Returns an array of Image IDs that are associated with the account"
url = '{0}/3/account/{1}/images/ids/{2}'.format(
self.api_url,
self.config['account_username'],
page
)
headers = {
'authorization': 'Bearer {0}'.format(self.config['access_token'])
}
request = requests.get(url, headers=headers)
response = self.response(request, url)
# the total number of comments
response['response']['total'] = self.image_count()
return response

def image_fav(self, image_id):
"Favorite an image with the given ID"
url = '{0}/3/image/{1}/favorite'.format(self.api_url, image_id)
headers = {
'authorization': 'Bearer {0}'.format(self.config['access_token'])
}
request = requests.post(url, headers=headers)
return self.response(request, url)

def image_count(self):
"Return the total number of albums associated with the account"
url = '{0}/3/account/{1}/images/count'.format(
self.api_url,
self.config['account_username']
)
headers = {
'authorization': 'Bearer {0}'.format(self.config['access_token'])
}
request = requests.get(url, headers=headers)
return self.data(request)
46 changes: 36 additions & 10 deletions imgur_python/Imgur.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class Imgur():
"Imgur classes entry point"

__version__ = '0.1.0'
__version__ = '0.2.0'

def __init__(self, config):
# config
Expand All @@ -34,7 +34,7 @@ def __init__(self, config):

def version(self):
"API client version"
return self.__version__
return 'Imgur API client {0}'.format(self.__version__)

# Authorization

Expand Down Expand Up @@ -102,12 +102,16 @@ def follow_tag(self, tag):
def unfollow_tag(self, tag):
"Unfollows the <tag> specified for the currently logged in user"
return self.account.unfollow_tag(tag)

def notifications(self, new=False):
"Returns all of the reply notifications for the user"
return self.account.notifications(new)

# Comment

def comments(self, username, page=0, sort='newest'):
def comments(self, page=0, sort='newest'):
"Return the comments the user has created"
return self.comment.comments(username, page, sort)
return self.comment.comments(page, sort)

def comment_get(self, comment_id):
"Get information about a specific comment"
Expand Down Expand Up @@ -135,11 +139,19 @@ def comment_report(self, comment_id, reason):
"Report a comment for being inappropriate"
return self.comment.report(comment_id, reason)

def comment_ids(self, page=0, sort='newest'):
"Return an array of all of the comment IDs"
return self.comment.comment_ids(page, sort)

def comment_replies(self, comment_id):
"Returns all of the reply notifications for the user"
return self.comment.comment_replies(comment_id)

# Album

def albums(self, username, page=0):
def albums(self, page=0):
"Get all the albums associated with the account"
return self.album.albums(username, page)
return self.album.albums(page)

def album_get(self, album_id):
"Get additional information about an album"
Expand Down Expand Up @@ -188,14 +200,20 @@ def album_remove(self, delete_hash, images):
'ids[]': images
}
return self.album.remove(delete_hash, payload)

def album_ids(self, page=0):
"Return an array of all of the album IDs"
return self.album.album_ids(page)

def album_fav(self, album_id):
"Favorite an album with a given ID"
return self.album.album_fav(album_id)

# Image

def images(self, username, page):
def images(self, page=0):
"Get account images"
if username == self.config['account_username']:
username = 'me'
return self.image.images(username, page)
return self.image.images(page)

def image_get(self, image_id):
"Get information about an image"
Expand Down Expand Up @@ -247,6 +265,14 @@ def image_update(self, image_id, title=None, description=None):
def image_delete(self, image_id):
"Deletes an image"
return self.image.delete(image_id)

def image_ids(self, page=0):
"Returns an array of Image IDs that are associated with the account"
return self.image.image_ids(page)

def image_fav(self, image_id):
"Favorite an image with the given ID"
return self.image.image_fav(image_id)

# Gallery

Expand Down
10 changes: 9 additions & 1 deletion imgur_python/ImgurBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ def response(request, url):
response = {}
response['url'] = url
response['status'] = request.status_code
# response['headers'] = request.headers
if request.status_code == 200:
response['response'] = request.json()
else:
response['response'] = request
return response

@staticmethod
def data(request):
"Just return the data"
if request.status_code == 200:
response = request.json()
return response['data']
else:
return null
Loading

0 comments on commit e057881

Please sign in to comment.