From b8f03164ee5c4724ef01cd5a5e65e7353052676c Mon Sep 17 00:00:00 2001 From: Dolan O'Toole Date: Fri, 16 Sep 2016 00:53:31 +0100 Subject: [PATCH 1/2] simple image sharing in chatrooms using Imgur --- imgur.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 imgur.py diff --git a/imgur.py b/imgur.py new file mode 100644 index 0000000..db74ea8 --- /dev/null +++ b/imgur.py @@ -0,0 +1,39 @@ +""" +Requires imgur API + pip install imgurpython + + +""" + +import prof + +from imgurpython import ImgurClient +from imgurpython.helpers.error import ImgurClientError + +client_id = '19c4ba3fc2c26ef' +client_secret = 'dd5b0918a2f5d36391d574bd2061c50b47bade63' +client = ImgurClient(client_id, client_secret) + + +def _cmd_imgur(arg): + try: + data = client.upload_from_path(arg, config=None, anon=True) + prof.send_line(data['link']) + except ImgurClientError as e: + prof.log_error('Could not upload to Imgur - ' + e.error_message) + prof.log_error('Imgur status code - ' + e.status_code) + + +def prof_init(version, status, account_name, fulljid): + synopsis = [ + "/imgur " + ] + description = "Upload an image to imgur and send the link" + args = [ + [ "", "full path to image file" ] + ] + examples = [ + "/imgur ~/images/cats.jpg" + ] + + prof.register_command("/imgur", 1, 1, synopsis, description, args, examples, _cmd_imgur) From b578b5d7203457d8c6dedbca32fe26867ac9ccc1 Mon Sep 17 00:00:00 2001 From: Dolan O'Toole Date: Mon, 19 Sep 2016 00:29:08 +0100 Subject: [PATCH 2/2] dealing with ~ in path and file not found --- imgur.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/imgur.py b/imgur.py index db74ea8..db5173b 100644 --- a/imgur.py +++ b/imgur.py @@ -6,6 +6,7 @@ Requires imgur API """ import prof +from os import path from imgurpython import ImgurClient from imgurpython.helpers.error import ImgurClientError @@ -15,10 +16,13 @@ client_secret = 'dd5b0918a2f5d36391d574bd2061c50b47bade63' client = ImgurClient(client_id, client_secret) -def _cmd_imgur(arg): +def _cmd_imgur(img_path): try: - data = client.upload_from_path(arg, config=None, anon=True) + expanded_path = path.expanduser(img_path) + data = client.upload_from_path(expanded_path, config=None, anon=True) prof.send_line(data['link']) + except IOError as ioe: + prof.cons_show('Could not find file at ' + expanded_path) except ImgurClientError as e: prof.log_error('Could not upload to Imgur - ' + e.error_message) prof.log_error('Imgur status code - ' + e.status_code)