-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
executable file
·487 lines (365 loc) · 17.1 KB
/
api.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!../bin/python3
import os, sys, subprocess, requests, urllib.parse, time, logging
from threading import Thread
import oceaneyes as oe
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
from fastapi import FastAPI
from fastapi.responses import RedirectResponse
from fastapi.responses import PlainTextResponse
from fastapi.responses import JSONResponse
from fastapi.responses import FileResponse
app = FastAPI()
global sip, ip, url, pid_fm
sip = oe.get_serverip()
settings, url, ip = oe.init()
def isfloat(sin):
if sin.replace(".", "").isnumeric():
return True
else:
return False
@app.get("/v1/{device}/activate", status_code=200)
async def switch(device):
if device.isnumeric():
global settings, url, ip
settings, url, ip = oe.init(device)
return '{"result": 200, "status": "active", "message": "Active device switched to radio "' + str(device) + ', "ipaddr": ' + str(ip) + '}'
else:
return '{"result": 500, "status": "error", "message": "Device index needs to be a number", "ipaddr": 0.0.0.0}'
@app.get("/v1/{device}/status", status_code=200)
async def online(device):
settings, surl, sip = oe.init(device)
if oe.is_online() and device.isnumeric():
logging.info(f"[i] Status of radio@{ip}: online")
return '{"result": 200, "status": "online", "ipaddr": ' + str(sip) + '}'
else:
logging.info(f"[i] Status of radio@{ip}: offline")
return '{"result": 400, "status": "offline, "ipaddr": ' + str(sip) + '}'
@app.get("/v1/{device}/playing", status_code=200)
async def status(device):
settings, settings_url, settings_ip = oe.switch(device)
if oe.is_online(settings_ip) and device.isnumeric():
code, status, playing = oe.get_status(settings_ip)
if code == 200:
logging.info(f"[i] Status of radio@{settings_ip}: playing - {playing} [{code}]")
return '{"result": 200, "status": "playing", "message": "' + str(playing) + '", "ipaddr": ' + str(ip) + '}'
else:
logging.info(f"[i] Status of radio@{settings_ip}: stopped (or FM/DAB playing)")
return '{"result": 200, "status": "stopped", "message": "None or FM/DAB", "ipaddr": ' + str(ip) + '}'
else:
return '{"result": 500, "status": "error", "message": "Device index needs to be a number", "ipaddr": 0.0.0.0}'
@app.post("/v1/fav/add", status_code=200)
async def fav_add(n, u):
if "http" in u:
c = "1;1;-1"
g = "2;68"
code, station = oe.add(str(n), str(u), str(c), str(g), False)
if code == 200:
return '{"result": ' + str(code) + ', "message": "Added: ' + str(station) + '"}'
elif code == 404:
return '{"result": ' + str(code) + ', "message": "Not Found: ' + str(station) + '"}'
else:
return '{"result": ' + str(code) + ', "message": "Failed to add: ' + str(station) + '"}'
else:
return '{"result": 500, "message": "Failed to add: ' + str(station) + '"}'
@app.get("/v1/device/scan", status_code=200)
async def scan():
hosts, devices = oe.scan()
radios = len(devices)
radiol = ""
if radios > 0:
for i in range(0,int(radios)):
radiol += "Radio " + str(i+1) + "@" + str(devices[i]) + ","
return '{"result": 200, "message": "' + str(radios) + ' radio(s) found on this network ", "devices": "' + str(radiol) + '", "webhosts": "' + str(hosts) + '"}'
else:
return '{"result": 404, "message": "No radios found on this network", "webhosts": "' + str(hosts) + '"}'
@app.get("/v1/{device}/app", status_code=200)
async def loadapp_device(device):
settings, settings_url, settings_ip = oe.switch(device)
if oe.is_online(settings_ip) and device.isnumeric():
logging.info(f"[i] Loading the native webapp of radio@{settings_ip}")
response = RedirectResponse(url=str(settings_url))
return response
else:
return '{"result": 200, "status": "offline", "message": "Please turn on the radio", "ipaddr": ' + str(ip) + '}'
@app.get("/v1/{device}/volume/up", status_code=200)
async def volume_up(device):
settings, settings_url, settings_ip = oe.switch(device)
return str(oe.volume(int(device), "up"))
@app.get("/v1/{device}/volume/down", status_code=200)
async def volume_dn(device):
settings, settings_url, settings_ip = oe.switch(device)
return str(oe.volume(int(device), "down"))
#@app.get("/v1/{device}/volume/toggle", status_code=200)
#async def volume_tg(device):
# settings, settings_url, settings_ip = oe.switch(device)
# return str(oe.volume(int(device), "toggle"))
@app.get("/v1/{device}/volume/mute", status_code=200)
async def volume_mu(device):
settings, settings_url, settings_ip = oe.switch(device)
return str(oe.volume(int(device), "mute"))
@app.get("/v1/{device}/volume/unmute", status_code=200)
async def volume_um(device):
settings, settings_url, settings_ip = oe.switch(device)
return str(oe.volume(int(device), "unmute"))
@app.get("/v1/{device}/fav/vacant", status_code=200)
async def fav_remaining(device):
settings, settings_url, settings_ip = oe.switch(device)
return '{"value": "' + str(oe.get_remaining(int(device), "fav")) + '"}'
@app.get("/v1/{device}/fav/engaged", status_code=200)
async def fav_total(device):
settings, settings_url, settings_ip = oe.switch(device)
return '{"value": "' + str(oe.get_total(int(device), "fav")) + '"}'
@app.get("/v1/{device}/fav/save-current", status_code=200)
async def fav_save(device):
settings, settings_url, settings_ip = oe.switch(device)
code, station = oe.add_current(int(device))
if code == 200:
return '{"result": 200", "message": "' + str(station) + ' added to favourites"}'
else:
return '{"result": 400, "message": "Currently playing station not saved to favourites"}'
@app.get("/v1/{device}/fav/backup", status_code=200)
async def fav_backup(device):
settings, settings_url, settings_ip = oe.switch(device)
list = oe.get_list(int(device), "backup", False)
message = "Exported from radio@" + str(settings_ip) + " to local database ~/python-apps/oceaneyes/app/stations_device" + str(device) + ".db"
logging.info(f"[i] : {message}")
return '{"result": 200, "message": "' + str(message) + '", "stations:", "' + str(list) + '"}'
@app.get("/v1/{device}/fav/play/{c}", status_code=200)
async def fav_play(device = 1, c = 1):
if c.isnumeric():
settings, settings_url, settings_ip = oe.switch(device)
logging.info(f"[i] Request to play preset {str(c)} on radio@{settings_ip} ... checking stream")
chid, chname, churl, country_str, genre_str = oe.info_get(int(device), str(c))
#streamable = oe.is_streamable(str(churl), False)
streamable = True
if streamable:
code, message, playing = oe.play(int(device), str(c))
if code == 200:
logging.info(f"[i] Playing preset {str(int(chid) + 1)} on radio@{settings_ip}")
logging.info(f"--- {churl}")
return '{"result": ' + str(code) + ', "message": "' + str(message) + '"}'
else:
logging.info(f"[i] Cannot play preset {str(int(chid) + 1)} on radio@{settings_ip} ... Preset out of range")
return '{"result": ' + str(code) + ', "message": "Preset may be out of range"}'
else:
logging.info(f"[i] Cannot play preset {str(int(chid) + 1)} on radio@{settings_ip}")
logging.info(f"--- {churl}")
return '{"result": 404, "message": "Station not streamable"}'
else:
return '{"result": 500, "message": "Preset not a number, try requesting with an integer value"}'
@app.get("/v1/{device}/streamto/vlc/{chid}", status_code=200)
async def fav_stream_vlc(device = 1, chid = 1):
if chid.isnumeric():
settings, settings_url, settings_ip = oe.switch(device)
code, message = oe.listen(int(device), str(chid))
if code == 200:
return '{"result": ' + str(code) + ', "message": "' + str(message) + '"}'
else:
return '{"result": ' + str(code) + ', "message": "Preset may be out of range"}'
else:
return '{"result": 500, "message": "Preset index not a number, try requesting with an integer value"}'
@app.get("/v1/{device}/streamto/browser/{chid}", status_code=200)
async def fav_stream_browser(device = 1, chid = 1):
if chid.isnumeric():
settings, settings_url, settings_ip = oe.switch(device)
ichid = chid
chname = "not set"
churl = "not set"
country_str = "not set"
genre_str = "not set"
if oe.is_online(settings_ip):
chid, chname, churl, country_str, genre_str = oe.info_get(int(device), str(int(chid)))
else:
chid, chname, churl, chcountry, chgenre = oe.info_cached(int(device), str(int(chid)))
if "http" in str(churl):
logging.info(f"[i] Listening to preset {str(int(chid) + 1)} from radio@{settings_ip} on a local browser")
logging.info(f"--- proxyto={churl}")
response = RedirectResponse(url=str(churl))
return response
else:
logging.info(f"[i] Listening to preset {chid} from radio@{settings_ip} not possible or is masked by skytune")
return '{"result": 400, "message": "Prest may not be playable"}'
else:
logging.info(f"[i] Attempting to listen to preset {ichid} and input is not a number, try an integer")
return '{"result": 500, "message": "Preset index not a number, try requesting with an integer value"}'
@app.get("/v1/{device}/fav/playlist/{format}", status_code=200)
async def fav_playlist(device = 1, format = "m3u"):
settings, settings_url, settings_ip = oe.switch(device)
if format == "pls":
list = oe.get_list(int(device), "pls")
elif format == "m3u":
list = oe.get_list(int(device), "m3u")
elif format == "m3u-yt":
list = oe.get_list(int(device), "m3u-yt")
elif format == "json":
list = oe.get_list(int(device), "json")
list = str(list).replace("'", "")
response = PlainTextResponse(content=list, status_code=200)
response.headers["content-type"] = "application/json"
return response
elif format == "json-yt":
list = oe.get_list(int(device), "json-yt")
response = PlainTextResponse(content=list, status_code=200)
response.headers["content-type"] = "application/json"
return response
elif format == "csv":
list = oe.get_list(int(device), "csv")
elif format == "ssv":
list = oe.get_list(int(device), "ssv")
elif format == "plain":
list = oe.get_list(int(device), "plain")
else:
list = oe.get_list(int(device), "m3u")
response = PlainTextResponse(content=str(list), status_code=200)
return response
#installed_cinnamon = subprocess.getoutput('cinnamon --version')
#if "cinnamon" in installed_cinnamon.lower():
@app.get("/v1/fav/sync/linux-mint", status_code=200)
async def fav_rpp(format = "json-rpp"):
message = oe.get_list("json-rpp")
code = 200
return '{"result": ' + str(code) + ', "message": ' + str(message) + '}'
@app.get("/v1/fav/sync/radioplusplus", status_code=200)
async def fav_rpp(format = "json-rpp-out"):
message = oe.get_list("json-rpp-out")
#code = 200
#return '{"result": ' + str(code) + ', "message": ' + str(message) + '}'
response = PlainTextResponse(content=message, status_code=200)
response.headers["content-type"] = "application/json"
return response
# INPUT FORMAT - from:to for example move channel 20 to 25 ... /v1/fav/move/20:25
@app.get("/v1/{device}/fav/move/{r}", status_code=200)
async def fav_move(device = 1, r = "1:2"):
settings, settings_url, settings_ip = oe.switch(device)
if ":" in r:
r_bits = r.split(":")
if r_bits[0].isnumeric() and r_bits[1].isnumeric() and r_bits[0].isdigit() and r_bits[1].isdigit():
code, message = oe.move(int(device), str(r_bits[0]), str(r_bits[1]))
if code == 200:
logging.info(f"[i] Moved preset from {r_bits[0]} to {r_bits[1]}")
return '{"result": ' + str(code) + ', "message": ' + str(message) + '}'
else:
logging.info(f"[!] Preset move failed")
return '{"result": ' + str(code) + ', "message": ' + str(message) + '}'
else:
logging.info(f"[!] Preset move failed, input given needed to be integers")
return '{"result": 500, "message": "To move a preset to another position supply in this format 2:20 to move from 2 to 20"}'
else:
logging.info(f"[!] To move a preset to another position supply in this format 2:20 to move from 2 to 20")
return '{"result": ' + str(code) + ', "message": "To move a preset to another position supply in this format 2:20 to move from 2 to 20"}'
@app.get("/v1/listen/fmradio/{f}", status_code=200)
async def listen_fmradio(f = "90.3"):
if isfloat(str(f)):
url_rdio = "http://"+ str(sip) +":3345/fmradio-" + str(f)
url_rm3u = "http://"+ str(sip) +":3345/fmradio" + str(f) + ".m3u"
thread_fm = Thread(target=lambda: oe.fmradio(str(f), f"Local FM Radio - {f}Mhz", "3345", "rdo"))
thread_fm.start()
time.sleep(8)
pid_fm = os.system('ps aux | grep -i "rtl_fm" | head -n1 | cut -d " " -f9')
logging.info(f"[i] FM Tuner PID: {pid_fm}")
response = RedirectResponse(url=str(url_rdio))
return response
else:
return '{"result": 500, "message": "Supply the FM frequency for example listen/fmradio/90.3"}'
@app.get("/v1/listen/wxradio/{f}", status_code=200)
async def listen_wxradio(f = "162.50"):
if isfloat(str(f)):
url_rdio = "http://"+ str(sip) +":3345/wxradio"
url_rm3u = "http://"+ str(sip) +":3345/wxradio.m3u"
thread_wx = Thread(target=lambda: oe.wxradio(str(f), f"Local WX Radio - {f}Mhz", "3345", "rdo"))
thread_wx.start()
time.sleep(8)
pid_wx = os.system('ps aux | grep -i "rtl_fm" | head -n1 | cut -d " " -f9')
logging.info(f"[i] FM Tuner PID: {pid_wx}")
response = RedirectResponse(url=str(url_rdio))
return response
else:
return '{"result": 500, "message": "Supply the FM frequency for example listen/fmradio/90.3"}'
@app.get("/v1/listen/hdradio/{c}/{p}", status_code=200)
async def listen_hdradio(c = "90.3", p = "0" ):
if isfloat(str(c)) and p.isnumeric():
url_rdio = "http://"+ str(sip) +":3345/hdradio-" + str(c) + "-" + str(p)
url_rm3u = "http://"+ str(sip) +":3345/hdradio-" + str(c) + "-" + str(p) + ".m3u"
thread_fm = Thread(target=lambda: oe.hdradio(str(c), str(p), f"Local HD Radio - {c}Mhz P:{p}", "3345", "rdo"))
thread_fm.start()
time.sleep(15)
response = RedirectResponse(url=str(url_rdio))
return response
else:
return '{"result": 500, "message": "Supply the FM frequency and then the hybrid program index for example listen/hdradio/90.3/0"}'
# For listening to newer stream technology such as HLS or FLAC on older equipment (transcoding to Vorbis to help retain quality)
@app.get("/v1/listen/cdradio", status_code=200)
async def listen_cdradio(s = "https://hls.somafm.com/hls/groovesalad/FLAC/program.m3u8"):
if "http" in str(s).lower():
url_prox = "http://"+ str(sip) +":3372/cdradio.ogg"
os.system("cvlc " + str(s) +" --prefetch-buffer-size=8000 --sout-mux-caching=8000 --sout '#transcode{acodec=vorb,ab=320}:standard{access=http,mux=ogg,dst=" + str(sip) + ":3372/cdradio.ogg}'")
time.sleep(3)
return '{"result": 200, "message": "Add this url to your radio device: "' + str(url_prox) + '}'
else:
return '{"result": 500, "message": "Supply the url with protocol for example https://hls.somafm.com/hls/groovesalad/FLAC/program.m3u8"}'
@app.get("/v1/listen/ytradio/{c}/{p}/{n}", status_code=200)
async def listen_ytradio(c = "jfKfPfyJRdk", p = "91", n = "YouTube Radio" ):
if p.isnumeric():
thread_yt = Thread(target=lambda: oe.ytradio(str(c), str(p), str(n), "3345", "rdo"))
thread_yt.start()
time.sleep(10)
url_rdio = "http://" + str(sip) + ":3345/ytradio-" + str(c)
url_rm3u = "http://" + str(sip) + ":3345/ytradio-" + str(c) + ".m3u"
response = RedirectResponse(url=str(url_rdio))
return response
#return '{"result": 200, "message": "Add this url to your radio device: "' + str(url_prox) + '"}'
else:
return '{"result": 500, "message": "Supply the youtube stream ID as an integer number for example 91"}'
@app.get("/v1/listen/bbcradio/{c}", status_code=200)
async def listen_bbcradio(c = "m001p1n9"):
thread_bbc = Thread(target=lambda: oe.bbcradio(str(c), "3345", "rdo"))
thread_bbc.start()
time.sleep(15)
url_rdio = "http://" + str(sip) + ":3345/bbcradio-" + str(c)
url_rm3u = "http://" + str(sip) + ":3345/bbcradio-" + str(c) + ".m3u"
response = RedirectResponse(url=str(url_rdio))
return response
@app.get("/v1/listen/tv/{c}", status_code=200)
async def listen_hdhomerun(c):
url_iptv = "http://192.168.2.221:5004/auto/v"
url_rdio = "http://"+ str(sip) +":3345/hdhomerun"
url_rm3u = "http://"+ str(sip) +":3345/hdhomerun.m3u"
if c:
chid = c
else:
chid = 2.1
os.system(f"ffmpeg -re -i {url_iptv}{chid} -vn -codec:a libmp3lame -b:a 192k -f mp3 -content_type audio/mpeg icecast://source:rdo@{sip}:3345/hdhomerun &")
time.sleep(10)
#playlist = requests.get(url_rm3u)
#response = PlainTextResponse(content=str(playlist), status_code=200)
#response = RedirectResponse(url=str(url_rdio))
response = RedirectResponse(url=str(url_rm3u))
return response
@app.get("/v1/search/radiobrowser/{keywords}", status_code=200)
async def search(keywords):
code, result = oe.search(str(keywords), "RadioBrowser", False)
if code == 200:
return result
else:
return '{"chname": "Station Not Found", "churl": "https://streaming.radio.co/s5c5da6a36/listen"}'
@app.get("/v1/search/tunein/{keywords}", status_code=200)
async def search(keywords):
code, result = oe.search(str(keywords), "TuneIn", False)
if code == 200:
return result
else:
return '{"chname": "Station Not Found", "churl": "https://streaming.radio.co/s5c5da6a36/listen"}'
def main():
global ip
settings, stations = oe.init("192.168.2.20")
ip = settings["ipaddress"]
print("main")
if oe.is_online():
print(oe.status())
fav_remaining = oe.get_remaining("fav")
fav_total = oe.get_total("fav")
#print("Presets: Total=" + str(fav_total) + " Remaining=" + str(fav_remaining))
else:
x=1+1
if __name__ == "__main__":
main()