-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebconfigeditor.py
280 lines (227 loc) · 10.9 KB
/
webconfigeditor.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from http.server import BaseHTTPRequestHandler
import time
import json
from urllib import parse
_fileConfig = 'autoplayer_config.json'
class WebConfigEditor():
#def __init__(self, *args):
# BaseHTTPRequestHandler.__init__(self, *args)
_sourceTemplateUrlNextPvr = "http://{hostip}/live?channel={channel}&client={clientid}"
_sourceTemplateImageNextPvr = "http://{hostip}/service?method=channel.icon&channel_id={programme}"
def __GetPath(self, webself):
selfpath = webself.path.lower()
try:
selfpath = selfpath[:selfpath.index('?')]
except Exception as ex:
selfpath = webself.path.lower()
if selfpath.startswith('/configeditor'):
selfpath = selfpath[13:]
if selfpath.startswith('/'):
selfpath = selfpath[1:]
return selfpath
def do_GET(self, webself: BaseHTTPRequestHandler):
"""Handles Web Server Requests"""
selfpath = self.__GetPath(webself)
if selfpath == '':
self.__Get_Menu(webself)
def do_POST(self, webself: BaseHTTPRequestHandler):
"""Handles Web Server Posts"""
selfpath = self.__GetPath(webself)
content_length = int(webself.headers["Content-Length"])
post_data = webself.rfile.read(content_length)
json_data = json.loads(post_data)
retValue = False
retMessage = 'Nothing Done'
# Do Action
if selfpath.startswith('schedule'):
retValue, retMessage = self.__ProcessSchedule(webself.command.upper(), json_data)
elif selfpath.startswith('source'):
retValue, retMessage = self.__ProcessSource(webself.command.upper(), json_data)
# Return to browser
returnjson = { "dt": int(time.time()), "action": retValue, "message": retMessage, "return": True}
webself.wfile.write(bytes(json.dumps(returnjson), "utf-8"))
# Return to caller
return retValue
def __Get_Menu(self, webself: BaseHTTPRequestHandler):
with open('web/config-home.htm', 'rb') as file:
webself.wfile.write(file.read())
def __ProcessSchedule(self, post_command, json_post_data):
# print("WebHandler.__ProcessSchedule() ", post_command, json_post_data)
if (post_command == 'PUT'):
return self.__Schedules_Save(json_post_data)
elif (post_command == 'DELETE'):
return self.__Schedules_Dele(json_post_data)
else:
return False, "Schedule Not coded"
def __Schedules_Save(self, json_post_data):
""" Schedules - Save (Add or Edit)"""
global _fileConfig
retMessage = "Issue"
with open(_fileConfig) as fp:
json_config = json.load(fp)
json_post_data_sid = int(json_post_data['sid'])
if (json_post_data_sid == 0):
#Add
newItemDays = self.__Schedules_ProcessDay(json_post_data)
newItem = {
"day": newItemDays,
"start": json_post_data['Start'],
"stop": json_post_data['Stop'],
"source": int(json_post_data['Source'])
}
json_config['schedules'].append(newItem)
retMessage = "Schedule Added"
else:
#Edit
newItemDays = self.__Schedules_ProcessDay(json_post_data)
json_post_data_sid = json_post_data_sid - 1
json_config['schedules'][json_post_data_sid]['day'] = newItemDays
json_config['schedules'][json_post_data_sid]['start'] = json_post_data['Start']
json_config['schedules'][json_post_data_sid]['stop'] = json_post_data['Stop']
json_config['schedules'][json_post_data_sid]['source'] = int(json_post_data['Source'])
retMessage = "Schedule Item Updated"
with open(_fileConfig, 'w') as fp:
json.dump(json_config, fp)
return True, retMessage
def __Schedules_Dele(self, json_post_data):
""" Schedules - Delete"""
global _fileConfig
retMessage = "Issue"
with open(_fileConfig) as fp:
json_config = json.load(fp)
json_post_data_sid = int(json_post_data['sid'])
if (json_post_data_sid > 0):
#delete
json_post_data_sid = json_post_data_sid - 1
json_config['schedules'].pop(json_post_data_sid)
retMessage = "Schedule Item Removed"
with open(_fileConfig, 'w') as fp:
json.dump(json_config, fp)
return True, retMessage
def __Schedules_ProcessDay(self, json_post_data):
""" Schedules - Process Posted days into output"""
newItemDays = []
if (json_post_data['Days']['0']):
newItemDays.append(0)
if (json_post_data['Days']['1']):
newItemDays.append(1)
if (json_post_data['Days']['2']):
newItemDays.append(2)
if (json_post_data['Days']['3']):
newItemDays.append(3)
if (json_post_data['Days']['4']):
newItemDays.append(4)
if (json_post_data['Days']['5']):
newItemDays.append(5)
if (json_post_data['Days']['6']):
newItemDays.append(6)
return newItemDays
def __ProcessSource(self, post_command, json_post_data):
# print("WebHandler.__ProcessSource() ", post_command, json_post_data)
if (post_command == 'PUT'):
return self.__Source_Save(json_post_data)
elif (post_command == 'DELETE'):
return self.__Source_Dele(json_post_data)
else:
return False, "Source Not coded"
def __Source_Save(self, json_post_data):
""" Source - Save (Add or Edit)"""
global _fileConfig
retMessage = "Issue"
with open(_fileConfig) as fp:
json_config = json.load(fp)
if (json_post_data['Name'] == ""):
return False, "Name not set"
json_post_data_sid = int(json_post_data['sid'])
if (json_post_data_sid == 0):
#Add
newItem = {
"name": json_post_data['Name'],
"url": None,
"image": None,
"programme": None
}
if (json_post_data['Type'] == 'nextpvr'):
if (json_post_data['ChannelID'] == ""):
return False, "Channel ID not set"
if (json_post_data['ProgrammeID'] == ""):
return False, "Programme ID not set"
newItem['url'] = self._sourceTemplateUrlNextPvr.format(hostip=json_config['template-nextpvr']['hostip'],
channel=json_post_data['ChannelID'],
clientid=json_config['template-nextpvr']['clientid'])
newItem['image'] = self._sourceTemplateImageNextPvr.format(hostip=json_config['template-nextpvr']['hostip'],
programme=json_post_data['ProgrammeID'])
newItem['programme'] = {
"nextpvr": {
"hostip": json_config['template-nextpvr']['hostip'],
"hostport": json_config['template-nextpvr']['hostport'],
"pin": json_config['template-nextpvr']['pin'],
"channel_id": json_post_data['ProgrammeID']
}
}
else:
if (json_post_data['Url'] == ""):
return False, "Url not set"
if (json_post_data['Image'] != ""):
newItem['image'] = json_post_data['Image']
newItem['url'] = json_post_data['Url']
# Get new ID
newItemId = 1
for sourceitem in json_config['sources']:
if int(sourceitem['id']) > newItemId:
newItemId = int(sourceitem['id'])
newItem['id'] = newItemId + 1
json_config['sources'].append(newItem)
retMessage = "Source Added"
else:
#Edit
for sourceitem in json_config['sources']:
if int(sourceitem['id']) == json_post_data_sid:
sourceitem['name'] = json_post_data['Name']
if (json_post_data['Type'] == 'nextpvr'):
if (json_post_data['ChannelID'] == ""):
return False, "Channel ID not set"
if (json_post_data['ProgrammeID'] == ""):
return False, "Programme ID not set"
sourceitem['url'] = self._sourceTemplateUrlNextPvr.format(hostip=json_config['template-nextpvr']['hostip'],
channel=json_post_data['ChannelID'],
clientid=json_config['template-nextpvr']['clientid'])
sourceitem['image'] = self._sourceTemplateImageNextPvr.format(hostip=json_config['template-nextpvr']['hostip'],
programme=json_post_data['ProgrammeID'])
sourceitem['programme'] = {
"nextpvr": {
"hostip": json_config['template-nextpvr']['hostip'],
"hostport": json_config['template-nextpvr']['hostport'],
"pin": json_config['template-nextpvr']['pin'],
"channel_id": json_post_data['ProgrammeID']
}
}
else:
sourceitem['image'] = None
sourceitem['programme'] = None
if (json_post_data['Url'] == ""):
return False, "Url not set"
if (json_post_data['Image'] != ""):
sourceitem['image'] = json_post_data['Image']
sourceitem['url'] = json_post_data['Url']
retMessage = "Source Item Updated"
with open(_fileConfig, 'w') as fp:
json.dump(json_config, fp)
return True, retMessage
def __Source_Dele(self, json_post_data):
""" Source - Delete"""
global _fileConfig
retMessage = "Issue"
with open(_fileConfig) as fp:
json_config = json.load(fp)
json_post_data_sid = int(json_post_data['sid'])
if (json_post_data_sid > 0):
#delete
for sourceitem in json_config['sources']:
if int(sourceitem['id']) == json_post_data_sid:
json_config['sources'].remove(sourceitem)
break
retMessage = "Source Item Removed"
with open(_fileConfig, 'w') as fp:
json.dump(json_config, fp)
return True, retMessage