-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendNP.py
184 lines (153 loc) · 5.89 KB
/
sendNP.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
173
174
175
176
177
178
179
180
181
182
183
184
import json
import os
try:
import pysftp
except ImportError:
print("{ERROR} You need the 'PySFTP' module in order to run this program!")
exit(1)
import time
import traceback
try:
import getpass
has_getpass = True
except ImportError:
has_getpass = False
config = {}
default_config = """{
"imgPath" : "./image.png",
"textPath" : "./np.txt",
"font" : "arial.ttf",
"fontSize" : 15,
"colourFormat" : "RGBA",
"fontColour" : [0, 0, 0, 255],
"backgroundColour" : [0, 0, 0, 0],
"borderWidth" : 0,
"scanInterval" : 10,
"blankImageSize" : [1, 1],
"blankImageColourFormat" : "RGBA",
"blankImageBackground" : [0, 0, 0, 0],
"blankImageText" : [""],
"blankImageFont" : "arial.ttf",
"blankImageFontSize" : 15,
"blankImageFontColour" : [0, 0, 0, 255],
"blankImageBorderWidth": 0,
"hostname" : "example.org",
"port" : 22,
"username" : "username",
"remoteDir" : ".",
"SFTPscanInterval" : 10
}"""
def get_config():
global default_config
global config
conf_file = "./npImage.json"
if not os.path.isfile(conf_file):
with open(conf_file, "w", encoding="utf-8") as cf:
cf.write(default_config)
print("Generated new config file (npImage.json)")
exit(1)
with open(conf_file, encoding="utf-8") as cf:
config = json.loads(cf.read())
check_config()
def check_config():
global config
expected_types = {
"imgPath" : str,
"textPath" : str,
"font" : str,
"fontSize" : int,
"colourFormat" : str,
"fontColour" : list,
"backgroundColour" : list,
"borderWidth" : int,
"scanInterval" : int,
"blankImageSize" : list,
"blankImageColourFormat" : str,
"blankImageBackground" : list,
"blankImageText" : list,
"blankImageFont" : str,
"blankImageFontSize" : int,
"blankImageFontColour" : list,
"blankImageBorderWidth": int,
"hostname" : str,
"port" : int,
"username" : str,
"remoteDir" : str,
"SFTPscanInterval" : int
}
for option, expected_type in expected_types.items():
try:
if not isinstance(config[option], expected_type):
print("Invalid %s. Expected type %s, got %s." % (option, expected_type, type(config[option])))
exit(1)
except KeyError:
print("Missing option: %s" % option)
exit(1)
for channel in config["fontColour"]:
if not isinstance(channel, int):
print("Invalid fontColour value: %s. Expected type %s, got %s." % (channel, int, type(channel)))
exit(1)
for channel in config["backgroundColour"]:
if not isinstance(channel, int):
print("Invalid backgroundColour value: %s. Expected type %s, got %s." % (channel, int, type(channel)))
exit(1)
for channel in config["blankImageSize"]:
if not isinstance(channel, int):
print("Invalid blankImageSize value: %s. Expected type %s, got %s." % (channel, int, type(channel)))
exit(1)
for channel in config["blankImageBackground"]:
if not isinstance(channel, int):
print("Invalid blankImageBackground value: %s. Expected type %s, got %s." % (channel, int, type(channel)))
exit(1)
for channel in config["blankImageText"]:
if not isinstance(channel, str):
print("Invalid blankImageText value: %s. Expected type %s, got %s." % (channel, str, type(channel)))
exit(1)
for channel in config["blankImageFontColour"]:
if not isinstance(channel, int):
print("Invalid blankImageFontColour value: %s. Expected type %s, got %s." % (channel, int, type(channel)))
exit(1)
def main():
global config
get_config()
if has_getpass:
password = getpass.getpass("Password for %s@%s: " % (config["username"], config["hostname"]))
else:
password = input("Password for %s@%s (WILL BE VISIBLE - USE GETPASS): " % (config["username"], config["hostname"]))
while 1:
try:
oldmt = os.path.getmtime(config["imgPath"])
break
except FileNotFoundError:
print("Cannot find file: %s" % config["imgPath"])
time.sleep(config["SFTPscanInterval"])
send_file(password)
mainloop(oldmt, password)
def mainloop(oldmt, password):
global config
while 1:
while 1:
try:
mt = os.path.getmtime(config["imgPath"])
break
except FileNotFoundError:
print("Cannot find file: %s" % config["imgPath"])
time.sleep(config["SFTPscanInterval"])
# Check if modification time has changed
if mt > oldmt:
print("File has changed. %d > %d" % (mt, oldmt))
send_file(password)
oldmt = mt
time.sleep(config["SFTPscanInterval"])
def send_file(password):
global config
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
try:
with pysftp.Connection(config["hostname"], username = config["username"], password = password, port = config["port"], cnopts = cnopts) as sftp:
sftp.chdir(config["remoteDir"])
sftp.put(config["imgPath"])
print("File uploaded")
except:
print(traceback.format_exc())
main()