-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
137 lines (123 loc) · 4.38 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
import time
import warnings
import requests
import ScreenCloud
from PythonQt.QtCore import QFile, QSettings, QStandardPaths
from PythonQt.QtUiTools import QUiLoader
class LutimUploader(object):
SHORTNAME = 'lutim'
def __init__(self):
self.uil = QUiLoader()
self.loadSettings()
def showSettingsUI(self, parentWidget):
self.parentWidget = parentWidget
self.settingsDialog = self.uil.load(
QFile(
os.path.join(
ScreenCloud.getPluginDir(), self.SHORTNAME, 'settings.ui'
)
),
parentWidget
)
self.settingsDialog.connect('accepted()', self.saveSettings)
self.updateUi()
self.settingsDialog.open()
def updateUi(self):
self.loadSettings()
self.settingsDialog.group_lutim.input_url.setText(self.url)
self.settingsDialog.group_lutim.input_delay.setValue(self.delay)
self.settingsDialog.group_lutim.input_firstview.setChecked(
self.delete_on_firstview)
self.settingsDialog.group_lutim.input_forever.setChecked(
self.keep_forever)
self.settingsDialog.group_lutim.verify_ssl.setChecked(self.verify_ssl)
self.settingsDialog.adjustSize()
def loadSettings(self):
settings = QSettings()
settings.beginGroup('lutim')
self.url = settings.value('url', 'https://lut.im/')
self.delay = int(settings.value('delay', 1))
self.delete_on_firstview = settings.value('firstview', False) \
in ['true', True]
self.keep_forever = settings.value('forever', False) in ['true', True]
self.verify_ssl = settings.value('verify_ssl', True) in ['true', True]
settings.endGroup()
def saveSettings(self):
settings = QSettings()
settings.beginGroup('lutim')
settings.setValue(
'url', self.settingsDialog.group_lutim.input_url.text)
settings.setValue(
'delay', self.settingsDialog.group_lutim.input_delay.value)
settings.setValue(
'firstview',
self.settingsDialog.group_lutim.input_firstview.isChecked()
)
settings.setValue(
'forever',
self.settingsDialog.group_lutim.input_forever.isChecked()
)
settings.setValue(
'verify_ssl',
self.settingsDialog.group_lutim.verify_ssl.isChecked()
)
settings.endGroup()
def isConfigured(self):
return bool(self.url)
def getFilename(self):
return time.time()
def upload(self, screenshot, name):
self.loadSettings()
url = self.url
# check url
if not url.startswith('http'):
ScreenCloud.setError('Invalid url!')
return False
# append / to url (if needed)
if not url.endswith('/'):
url += '/'
if self.keep_forever:
delay = 0
else:
delay = self.delay
# save to a temporary file
timestamp = time.time()
filename = ScreenCloud.formatFilename(str(timestamp))
tmpFilename = os.path.join(QStandardPaths.writableLocation(
QStandardPaths.TempLocation
), filename)
screenshot.save(QFile(tmpFilename), ScreenCloud.getScreenshotFormat())
def do_request():
return requests.post(
url,
data={
'delete-day': delay,
'format': 'json',
'first-view': int(self.delete_on_firstview)
},
files={
'file': open(tmpFilename, 'rb')
},
verify=self.verify_ssl
)
if not self.verify_ssl:
with warnings.catch_warnings():
warnings.simplefilter('ignore')
response = do_request()
else:
response = do_request()
if not response:
ScreenCloud.setError('No Lutim-Instance there')
return False
else:
response_data = response.json()
if response_data['success']:
display_url = url + response_data['msg']['short']
ScreenCloud.setUrl(display_url)
return True
else:
ScreenCloud.setError(
'Lutim reports an error: \n' + response_data['message']
)
return False