-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
172 lines (133 loc) · 4.43 KB
/
helpers.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import time
import socket
import sys
import traceback
from urllib.request import urlopen
from PyQt5.QtCore import QObject, QRunnable, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QPixmap, QImage
from errors import ChallengeError
from instagram_web_api.errors import ClientError
import webbrowser
from functools import wraps
REMOTE_SERVER = "www.google.com"
def check_instagram_errors(func):
def decorator(*args, **kwargs):
try:
result = func(*args, **kwargs)
except ChallengeError as ce:
print(ce)
webbrowser.open(ce.msg)
return ce
except ClientError as ce:
print(ce)
return ce
return result
return decorator
def subscription_required(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
if not args[0].subscription_key:
raise ClientError('Method requires authentication.', 403)
return fn(*args, **kwargs)
return wrapper
def img_from_url(url: str) -> QPixmap:
try:
data = urlopen(url).read()
image = QImage()
image.loadFromData(data)
except ValueError:
return QPixmap(QImage('src/profile_picture.jpg'))
return QPixmap(image)
def validate_input(value: str = '') -> str:
"""
:rtype: str
:param value: string to be validate
:return: validated string or empty if not valid
"""
if not isinstance(value, str):
return ''
value = value.strip()
if len(value) < 5:
return ''
return value
def is_connected(hostname):
try:
# see if we can resolve the host name -- tells us if there is
# a DNS listening
host = socket.gethostbyname(hostname)
# connect to the host -- tells us if the host is actually
# reachable
s = socket.create_connection((host, 80), 2)
s.close()
return 1
except:
pass
return 0
def check_internet_all_time(progress_callback):
while True:
progress_callback.emit(is_connected(REMOTE_SERVER))
time.sleep(2)
class CheckConnectivity:
def __init__(self):
self.msg = ''
self.check_net_worker = Worker(check_internet_all_time)
# self.check_net_worker.signals.result.connect(self.l_result)
# self.check_net_worker.signals.finished.connect(self._finished)
self.check_net_worker.signals.progress.connect(self.process_result)
def process_result(self, net_status):
if net_status:
self.msg = "You are connected to the Internet."
else:
self.msg = "You are not connected to the Internet."
class WorkerSignals(QObject):
"""
Defines the signals available from a running worker thread.
Supported signals are:
finished
No data
error
`tuple` (exctype, value, traceback.format_exc() )
result
`object` data returned from processing, anything
progress
`int` indicating % progress
"""
finished = pyqtSignal()
error = pyqtSignal(tuple)
result = pyqtSignal(object)
progress = pyqtSignal(object)
class Worker(QRunnable):
"""
Worker thread
Inherits from QRunnable to handler worker thread setup, signals and wrap-up.
:param callback: The function callback to run on this worker thread. Supplied args and
kwargs will be passed through to the runner.
:type callback: function
:param args: Arguments to pass to the callback function
:param kwargs: Keywords to pass to the callback function
"""
def __init__(self, fn, *args, **kwargs):
super(Worker, self).__init__()
# Store constructor arguments (re-used for processing)
self.fn = fn
self.args = args
self.kwargs = kwargs
self.signals = WorkerSignals()
# Add the callback to our kwargs
self.kwargs['progress_callback'] = self.signals.progress
@pyqtSlot()
def run(self):
"""
Initialise the runner function with passed args, kwargs.
"""
# Retrieve args/kwargs here; and fire processing using them
try:
result = self.fn(*self.args, **self.kwargs)
except:
traceback.print_exc()
exctype, value = sys.exc_info()[:2]
self.signals.error.emit((exctype, value, traceback.format_exc()))
else:
self.signals.result.emit(result) # Return the result of the processing
finally:
self.signals.finished.emit() # Done