From 6b67a713e5ba4f2c7bb406be06f8e8e335c3f48e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucio=20Andr=C3=A9s=20Illanes=20Albornoz?= Date: Sun, 7 Jan 2018 23:49:57 +0100 Subject: [PATCH] MiRCART{CanvasStore,Frame}.py: implements export to Imgur (from bitmap via PNG.) --- MiRCARTCanvasStore.py | 46 ++++++++++++++++++----- MiRCARTFrame.py | 87 ++++++++++++++++++++++++++----------------- 2 files changed, 90 insertions(+), 43 deletions(-) diff --git a/MiRCARTCanvasStore.py b/MiRCARTCanvasStore.py index 8a65cd2..e41b35a 100644 --- a/MiRCARTCanvasStore.py +++ b/MiRCARTCanvasStore.py @@ -22,7 +22,7 @@ # SOFTWARE. # -import io +import io, os, tempfile try: import wx @@ -37,7 +37,7 @@ except ImportError: haveMiRCARTToPngFile = False try: - import requests, urllib.request + import base64, json, requests, urllib.request haveUrllib = True except ImportError: haveUrllib = False @@ -62,6 +62,32 @@ class MiRCARTCanvasStore(): PS_COLOUR_DIGIT0 = 2 PS_COLOUR_DIGIT1 = 3 + # {{{ _exportFileToImgur(self, apiKey, imgName, imgTitle, pathName): upload single PNG file to Imgur + def _exportFileToImgur(self, apiKey, imgName, imgTitle, pathName): + requestImageData = open(pathName, "rb").read() + requestData = { \ + "image": base64.b64encode(requestImageData), \ + "key": apiKey, \ + "name": imgName, \ + "title": imgTitle, \ + "type": "base64"} + requestHeaders = {"Authorization": "Client-ID " + apiKey} + responseHttp = requests.post( \ + "https://api.imgur.com/3/upload.json", \ + data=requestData, headers=requestHeaders) + responseDict = json.loads(responseHttp.text) + if responseHttp.status_code == 200: + return [200, responseDict.get("data").get("link")] + else: + return [responseHttp.status_code, ""] + # }}} + # {{{ _flipCellStateBit(self, cellState, bit): XXX + def _flipCellStateBit(self, cellState, bit): + if cellState & bit: + return cellState & ~bit + else: + return cellState | bit + # }}} # {{{ _parseCharAsColourSpec(self, colourSpec, curColours): XXX def _parseCharAsColourSpec(self, colourSpec, curColours): if len(colourSpec) > 0: @@ -74,18 +100,20 @@ class MiRCARTCanvasStore(): else: return (15, 1) # }}} - # {{{ _flipCellStateBit(self, cellState, bit): XXX - def _flipCellStateBit(self, cellState, bit): - if cellState & bit: - return cellState & ~bit - else: - return cellState | bit - # }}} # {{{ exportBitmapToPngFile(self, canvasBitmap, outPathName, outType): XXX def exportBitmapToPngFile(self, canvasBitmap, outPathName, outType): return canvasBitmap.ConvertToImage().SaveFile(outPathName, outType) # }}} + # {{{ exportBitmapToImgur(self, apiKey, canvasBitmap, imgName, imgTitle, imgType): XXX + def exportBitmapToImgur(self, apiKey, canvasBitmap, imgName, imgTitle, imgType): + tmpPathName = tempfile.mkstemp() + os.close(tmpPathName[0]) + canvasBitmap.ConvertToImage().SaveFile(tmpPathName[1], imgType) + imgurResult = self._exportFileToImgur(apiKey, imgName, imgTitle, tmpPathName[1]) + os.remove(tmpPathName[1]) + return imgurResult + # }}} # {{{ exportPastebin(self, apiDevKey, canvasMap, canvasSize, pasteName="", pastePrivate=0): XXX def exportPastebin(self, apiDevKey, canvasMap, canvasSize, pasteName="", pastePrivate=0): if haveUrllib: diff --git a/MiRCARTFrame.py b/MiRCARTFrame.py index fae9d0d..9c6870e 100644 --- a/MiRCARTFrame.py +++ b/MiRCARTFrame.py @@ -42,39 +42,41 @@ class MiRCARTFrame(MiRCARTGeneralFrame): CID_SAVEAS = (0x103, TID_COMMAND, "Save As...", "Save &As...", wx.ART_FILE_SAVE_AS, None) CID_EXPORT_AS_PNG = (0x104, TID_COMMAND, "Export as PNG...", \ "Export as PN&G...", None, None) - CID_EXPORT_PASTEBIN = (0x105, TID_COMMAND, "Export to Pastebin...", \ + CID_EXPORT_IMGUR = (0x105, TID_COMMAND, "Export to Imgur...", \ + "Export to I&mgur...", None, None, haveUrllib) + CID_EXPORT_PASTEBIN = (0x106, TID_COMMAND, "Export to Pastebin...", \ "Export to Pasteb&in...", None, None, haveUrllib) - CID_EXIT = (0x106, TID_COMMAND, "Exit", "E&xit", None, None) - CID_UNDO = (0x107, TID_COMMAND, "Undo", "&Undo", wx.ART_UNDO, (wx.ACCEL_CTRL, ord("Z")), False) - CID_REDO = (0x108, TID_COMMAND, "Redo", "&Redo", wx.ART_REDO, (wx.ACCEL_CTRL, ord("Y")), False) - CID_CUT = (0x109, TID_COMMAND, "Cut", "Cu&t", wx.ART_CUT, None, False) - CID_COPY = (0x10a, TID_COMMAND, "Copy", "&Copy", wx.ART_COPY, None, False) - CID_PASTE = (0x10b, TID_COMMAND, "Paste", "&Paste", wx.ART_PASTE, None, False) - CID_DELETE = (0x10c, TID_COMMAND, "Delete", "De&lete", wx.ART_DELETE, None, False) - CID_INCRBRUSH = (0x10d, TID_COMMAND, "Increase brush size", \ + CID_EXIT = (0x107, TID_COMMAND, "Exit", "E&xit", None, None) + CID_UNDO = (0x108, TID_COMMAND, "Undo", "&Undo", wx.ART_UNDO, (wx.ACCEL_CTRL, ord("Z")), False) + CID_REDO = (0x109, TID_COMMAND, "Redo", "&Redo", wx.ART_REDO, (wx.ACCEL_CTRL, ord("Y")), False) + CID_CUT = (0x10a, TID_COMMAND, "Cut", "Cu&t", wx.ART_CUT, None, False) + CID_COPY = (0x10b, TID_COMMAND, "Copy", "&Copy", wx.ART_COPY, None, False) + CID_PASTE = (0x10c, TID_COMMAND, "Paste", "&Paste", wx.ART_PASTE, None, False) + CID_DELETE = (0x10d, TID_COMMAND, "Delete", "De&lete", wx.ART_DELETE, None, False) + CID_INCRBRUSH = (0x10e, TID_COMMAND, "Increase brush size", \ "&Increase brush size", wx.ART_PLUS, None) - CID_DECRBRUSH = (0x10e, TID_COMMAND, "Decrease brush size", \ + CID_DECRBRUSH = (0x10f, TID_COMMAND, "Decrease brush size", \ "&Decrease brush size", wx.ART_MINUS, None) - CID_SOLID_BRUSH = (0x10f, TID_SELECT, "Solid brush", "&Solid brush", None, None, True) - CID_RECT = (0x110, TID_SELECT, "Rectangle", "&Rectangle", None, None, True) - CID_CIRCLE = (0x111, TID_SELECT, "Circle", "&Circle", None, None, False) - CID_LINE = (0x112, TID_SELECT, "Line", "&Line", None, None, False) - CID_COLOUR00 = (0x113, TID_COMMAND, "Colour #00", "Colour #00", None, None) - CID_COLOUR01 = (0x114, TID_COMMAND, "Colour #01", "Colour #01", None, None) - CID_COLOUR02 = (0x115, TID_COMMAND, "Colour #02", "Colour #02", None, None) - CID_COLOUR03 = (0x116, TID_COMMAND, "Colour #03", "Colour #03", None, None) - CID_COLOUR04 = (0x117, TID_COMMAND, "Colour #04", "Colour #04", None, None) - CID_COLOUR05 = (0x118, TID_COMMAND, "Colour #05", "Colour #05", None, None) - CID_COLOUR06 = (0x119, TID_COMMAND, "Colour #06", "Colour #06", None, None) - CID_COLOUR07 = (0x11a, TID_COMMAND, "Colour #07", "Colour #07", None, None) - CID_COLOUR08 = (0x11b, TID_COMMAND, "Colour #08", "Colour #08", None, None) - CID_COLOUR09 = (0x11c, TID_COMMAND, "Colour #09", "Colour #09", None, None) - CID_COLOUR10 = (0x11d, TID_COMMAND, "Colour #10", "Colour #10", None, None) - CID_COLOUR11 = (0x11e, TID_COMMAND, "Colour #11", "Colour #11", None, None) - CID_COLOUR12 = (0x11f, TID_COMMAND, "Colour #12", "Colour #12", None, None) - CID_COLOUR13 = (0x120, TID_COMMAND, "Colour #13", "Colour #13", None, None) - CID_COLOUR14 = (0x121, TID_COMMAND, "Colour #14", "Colour #14", None, None) - CID_COLOUR15 = (0x122, TID_COMMAND, "Colour #15", "Colour #15", None, None) + CID_SOLID_BRUSH = (0x110, TID_SELECT, "Solid brush", "&Solid brush", None, None, True) + CID_RECT = (0x111, TID_SELECT, "Rectangle", "&Rectangle", None, None, True) + CID_CIRCLE = (0x112, TID_SELECT, "Circle", "&Circle", None, None, False) + CID_LINE = (0x113, TID_SELECT, "Line", "&Line", None, None, False) + CID_COLOUR00 = (0x114, TID_COMMAND, "Colour #00", "Colour #00", None, None) + CID_COLOUR01 = (0x115, TID_COMMAND, "Colour #01", "Colour #01", None, None) + CID_COLOUR02 = (0x116, TID_COMMAND, "Colour #02", "Colour #02", None, None) + CID_COLOUR03 = (0x117, TID_COMMAND, "Colour #03", "Colour #03", None, None) + CID_COLOUR04 = (0x118, TID_COMMAND, "Colour #04", "Colour #04", None, None) + CID_COLOUR05 = (0x119, TID_COMMAND, "Colour #05", "Colour #05", None, None) + CID_COLOUR06 = (0x11a, TID_COMMAND, "Colour #06", "Colour #06", None, None) + CID_COLOUR07 = (0x11b, TID_COMMAND, "Colour #07", "Colour #07", None, None) + CID_COLOUR08 = (0x11c, TID_COMMAND, "Colour #08", "Colour #08", None, None) + CID_COLOUR09 = (0x11d, TID_COMMAND, "Colour #09", "Colour #09", None, None) + CID_COLOUR10 = (0x11e, TID_COMMAND, "Colour #10", "Colour #10", None, None) + CID_COLOUR11 = (0x11f, TID_COMMAND, "Colour #11", "Colour #11", None, None) + CID_COLOUR12 = (0x120, TID_COMMAND, "Colour #12", "Colour #12", None, None) + CID_COLOUR13 = (0x121, TID_COMMAND, "Colour #13", "Colour #13", None, None) + CID_COLOUR14 = (0x122, TID_COMMAND, "Colour #14", "Colour #14", None, None) + CID_COLOUR15 = (0x123, TID_COMMAND, "Colour #15", "Colour #15", None, None) # }}} # {{{ Non-items NID_MENU_SEP = (0x200, TID_NOTHING) @@ -83,7 +85,7 @@ class MiRCARTFrame(MiRCARTGeneralFrame): # {{{ Menus MID_FILE = (0x300, TID_MENU, "File", "&File", ( \ CID_NEW, CID_OPEN, CID_SAVE, CID_SAVEAS, NID_MENU_SEP, \ - CID_EXPORT_PASTEBIN, CID_EXPORT_AS_PNG, NID_MENU_SEP, \ + CID_EXPORT_AS_PNG, CID_EXPORT_IMGUR, CID_EXPORT_PASTEBIN, NID_MENU_SEP, \ CID_EXIT)) MID_EDIT = (0x301, TID_MENU, "Edit", "&Edit", ( \ CID_UNDO, CID_REDO, NID_MENU_SEP, \ @@ -164,17 +166,32 @@ class MiRCARTFrame(MiRCARTGeneralFrame): # {{{ canvasExportAsPng(self): XXX def canvasExportAsPng(self): - with wx.FileDialog(self, self.CID_SAVEAS[2], os.getcwd(), "", \ + with wx.FileDialog(self, self.CID_SAVEAS[2], os.getcwd(), "", \ "*.png", wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT) as dialog: if dialog.ShowModal() == wx.ID_CANCEL: return False else: outPathName = dialog.GetPath() - self.panelCanvas.canvasStore.exportBitmapToPngFile( \ - self.panelCanvas.canvasBitmap, outPathName, \ + self.panelCanvas.canvasStore.exportBitmapToPngFile( \ + self.panelCanvas.canvasBitmap, outPathName, \ wx.BITMAP_TYPE_PNG) return True # }}} + # {{{ canvasExportImgur(self): XXX + def canvasExportImgur(self): + imgurResult = self.panelCanvas.canvasStore.exportBitmapToImgur( \ + "c9a6efb3d7932fd", self.panelCanvas.canvasBitmap, "", "", wx.BITMAP_TYPE_PNG) + if imgurResult[0] == 200: + if not wx.TheClipboard.IsOpened(): + wx.TheClipboard.Open() + wx.TheClipboard.SetData(wx.TextDataObject(imgurResult[1])) + wx.TheClipboard.Close() + wx.MessageBox("Exported to Imgur: " + imgurResult[1], \ + "Export to Imgur", wx.OK|wx.ICON_INFORMATION) + else: + wx.MessageBox("Failed to export to Imgur: " + imgurResult[1], \ + "Export to Imgur", wx.OK|wx.ICON_EXCLAMATION) + # }}} # {{{ canvasExportPastebin(self): XXX def canvasExportPastebin(self): pasteStatus, pasteResult = \ @@ -292,6 +309,8 @@ class MiRCARTFrame(MiRCARTGeneralFrame): self.canvasSaveAs() elif cid == self.CID_EXPORT_AS_PNG[0]: self.canvasExportAsPng() + elif cid == self.CID_EXPORT_IMGUR[0]: + self.canvasExportImgur() elif cid == self.CID_EXPORT_PASTEBIN[0]: self.canvasExportPastebin() elif cid == self.CID_EXIT[0]: