|
| 1 | +import ScreenCloud |
| 2 | +import json |
| 3 | +import traceback |
| 4 | +import urllib.request, urllib.error, urllib.parse |
| 5 | + |
| 6 | +from PythonQt.QtCore import QSettings, QByteArray, QBuffer, QIODevice, QFile |
| 7 | +from PythonQt.QtGui import QWidget, QDialog |
| 8 | +from PythonQt.QtUiTools import QUiLoader |
| 9 | + |
| 10 | +class TokenUploader(): |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + |
| 14 | + self.uil = QUiLoader() |
| 15 | + self.loadSettings() |
| 16 | + |
| 17 | + def showSettingsUI(self, parentWidget): |
| 18 | + |
| 19 | + self.parentWidget = parentWidget |
| 20 | + self.settingsDialog = self.uil.load(QFile(workingDir + "/settings.ui"), parentWidget) |
| 21 | + |
| 22 | + self.settingsDialog.group_screenshot.input_name.connect("textChanged(QString)", self.nameFormatEdited) |
| 23 | + self.settingsDialog.connect("accepted()", self.saveSettings) |
| 24 | + self.loadSettings() |
| 25 | + |
| 26 | + self.settingsDialog.group_url.input_token.text = self.url_token |
| 27 | + self.settingsDialog.group_url.input_address.text = self.url_address |
| 28 | + self.settingsDialog.group_screenshot.input_name.text = self.name_format |
| 29 | + |
| 30 | + self.settingsDialog.open() |
| 31 | + |
| 32 | + def nameFormatEdited(self, name_format): |
| 33 | + |
| 34 | + self.settingsDialog.group_screenshot.label_example.setText(ScreenCloud.formatFilename(name_format)) |
| 35 | + |
| 36 | + def loadSettings(self): |
| 37 | + |
| 38 | + settings = QSettings() |
| 39 | + |
| 40 | + settings.beginGroup("uploaders") |
| 41 | + settings.beginGroup("tokenupload") |
| 42 | + |
| 43 | + self.url_token = settings.value("url-token", "") |
| 44 | + self.url_address = settings.value("url-address", "") |
| 45 | + self.name_format = settings.value("name-format", "Screenshot (%Y-%m-%d %H-%M-%S)") |
| 46 | + |
| 47 | + settings.endGroup() |
| 48 | + settings.endGroup() |
| 49 | + |
| 50 | + def saveSettings(self): |
| 51 | + |
| 52 | + settings = QSettings() |
| 53 | + |
| 54 | + settings.beginGroup("uploaders") |
| 55 | + settings.beginGroup("tokenupload") |
| 56 | + |
| 57 | + settings.setValue("url-token", self.settingsDialog.group_url.input_token.text) |
| 58 | + settings.setValue("url-address", self.settingsDialog.group_url.input_address.text) |
| 59 | + settings.setValue("name-format", self.settingsDialog.group_screenshot.input_name.text) |
| 60 | + |
| 61 | + settings.endGroup() |
| 62 | + settings.endGroup() |
| 63 | + |
| 64 | + def isConfigured(self): |
| 65 | + |
| 66 | + self.loadSettings() |
| 67 | + return not(not self.url_token or not self.url_address) |
| 68 | + |
| 69 | + def getFilename(self): |
| 70 | + |
| 71 | + self.loadSettings() |
| 72 | + return ScreenCloud.formatFilename(self.name_format) |
| 73 | + |
| 74 | + def upload(self, screenshot, name): |
| 75 | + |
| 76 | + self.loadSettings() |
| 77 | + |
| 78 | + q_ba = QByteArray() |
| 79 | + q_buff = QBuffer(q_ba) |
| 80 | + |
| 81 | + q_buff.open(QIODevice.WriteOnly) |
| 82 | + screenshot.save(q_buff, ScreenCloud.getScreenshotFormat()) |
| 83 | + q_buff.close() |
| 84 | + |
| 85 | + image = q_ba.toBase64().data() |
| 86 | + data = urllib.parse.urlencode({'token': self.url_token, 'name': name, 'image': image.decode('utf-8')}) |
| 87 | + binary = data.encode('utf-8') |
| 88 | + |
| 89 | + try: |
| 90 | + |
| 91 | + reply = urllib.request.urlopen(self.url_address, binary) |
| 92 | + |
| 93 | + response = reply.read().decode('utf-8') |
| 94 | + data = json.loads(response) |
| 95 | + error = data.get('error') |
| 96 | + url = data.get('href') |
| 97 | + |
| 98 | + if error: |
| 99 | + |
| 100 | + raise Exception(error) |
| 101 | + |
| 102 | + ScreenCloud.setUrl(url) |
| 103 | + |
| 104 | + except urllib.error.HTTPError as e: |
| 105 | + |
| 106 | + ScreenCloud.setError("Error while connecting to: " + self.url_address + "\nError:\n" + e.fp.read()) |
| 107 | + return False |
| 108 | + |
| 109 | + except Exception as e: |
| 110 | + |
| 111 | + try: |
| 112 | + |
| 113 | + ScreenCloud.setError("Could not upload to: " + self.url_address + "\nError: {0}".format(str(e))) |
| 114 | + |
| 115 | + except AttributeError: |
| 116 | + |
| 117 | + ScreenCloud.setError("Unexpected error while uploading:\n" + traceback.format_exc()) |
| 118 | + |
| 119 | + return False |
| 120 | + |
| 121 | + return True |
0 commit comments