-
Notifications
You must be signed in to change notification settings - Fork 0
/
PasteToUpload.py
97 lines (83 loc) · 2.76 KB
/
PasteToUpload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import sys
import urllib
import urllib2
import json
import re
from PySide.QtGui import QFont, QLabel, QLineEdit, QMainWindow, QApplication, QKeySequence
from PySide.QtCore import QBuffer, QByteArray, QIODevice, QThread, Qt
# Your Imgur API developer key
API_KEY = ''
class PasteToUpload(QMainWindow):
def __init__(self):
super(PasteToUpload, self).__init__()
self.initUI()
def initUI(self):
self.resize(290, 150)
self.setWindowTitle('PasteToUpload')
font = QFont('Helvetica', 16)
self.label = QLabel('Ctrl+V', self)
self.edit = QLineEdit(self)
self.label.setFont(font)
self.label.move(45, 25)
self.edit.move(45, 85)
self.label.setFixedWidth(250)
self.edit.setFixedWidth(200)
self.edit.setReadOnly(True)
self.edit.setFocusPolicy(Qt.NoFocus)
self.show()
def __sendPost(self, base64):
value = {
'key': API_KEY,
'image': base64
}
data = urllib.urlencode(value)
f = urllib2.urlopen(
url='http://api.imgur.com/2/upload.json',
data=data
)
return json.load(f)
def keyPressEvent(self, e):
if e.matches(QKeySequence.Paste):
clipboard = QApplication.clipboard()
mimeData = clipboard.mimeData()
if mimeData.hasImage():
image = clipboard.image()
byteArray = QByteArray()
buf = QBuffer(byteArray)
buf.open(QIODevice.WriteOnly)
image.save(buf, "PNG")
self.label.setText('Uploading')
self.thread = NetThread(str(byteArray.toBase64()))
self.thread.finished.connect(self.onThreadEnd)
self.thread.start()
else:
self.label.setText('No picture in clipboard')
def onThreadEnd(self):
url = self.thread.getResult()
self.edit.setText(url)
QApplication.clipboard().setText(url)
self.label.setText('Finish (URL in clipboard)')
class NetThread(QThread):
"""docstring for NetThread"""
def __init__(self, base64):
super(NetThread, self).__init__()
self.base64 = base64
def run(self):
value = {
'key': API_KEY,
'image': self.base64
}
data = urllib.urlencode(value)
f = urllib2.urlopen(
url='http://api.imgur.com/2/upload.json',
data=data
)
response = json.load(f)
urlRaw = response[u'upload'][u'links'][u'original']
self.__url = re.sub(r'\\/', '/', urlRaw)
def getResult(self):
return self.__url
if __name__ == '__main__':
app = QApplication(sys.argv)
ptu = PasteToUpload()
sys.exit(app.exec_())