*improved code for better error handling and more

*updated copyright year and author
*added arab.json
*updated religious, hardstyle and others..
*fixed a jsonParseError bug
*change quit return value to 0 when no error
*added better error handling around getCurrentSong()
*doesLinkWork matured for validating radio links
*song updater queued
*eventID checking bettered and actually works
*added some debug printf(s)
This commit is contained in:
bloomingchad 2024-03-17 23:09:00 +05:30
parent ac0e40ebd7
commit e2919e1add
6 changed files with 77 additions and 29 deletions

View File

@ -1,4 +1,4 @@
Copyright (C) 2021 antonl05
Copyright (C) 2021-2024 antonl05/bloomingchad
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

15
assets/arab.json Normal file
View File

@ -0,0 +1,15 @@
{
"pnimrp": [
"92.6 Fujairah",
"https://fujairah.fm/stream.php",
"mix FM arab",
"https://s1.voscast.com:11377/live.mp3",
"LiveQuranRadio",
"104.167.2.55:2199/tunein/qasiuran.pls",
"QuranVerse24",
"verse24.com:3344/listen.mp3"
]
}

View File

@ -3,7 +3,7 @@
"freeCodeCamp codeRadio",
"https://coderadio-admin-v2.freecodecamp.org//listen/coderadio/radio.mp3",
"freeCodeCamp codeRadio -data"
"freeCodeCamp codeRadio -data",
"https://coderadio-admin-v2.freecodecamp.org//listen/coderadio/low.mp3",
"Chill Step",

View File

@ -1,9 +1,9 @@
{
"pnimrp": [
"RealHardStyle.Nl",
"stream.realhardstyle.nl",
"https://stream.realhardstyle.nl",
"Chuck N Jane -data",
"listen.chucknjane.com/32kbps"
"https://listen.chucknjane.com/32kbps"
]
}

View File

@ -3,7 +3,10 @@
"Orthodox Chants",
"orthodox-chants.ru:8000/64kb",
"",
""
"RadioVaticana",
"https://radio.vaticannews.va/stream-en",
"OCR Rudder 24",
"radio.myocn.net:8080/stream"
]
}

View File

@ -30,7 +30,7 @@ proc sayBye(str, auth: string, line = -1) =
if line != -1:
error fmt"@ line: {line} in qoute.json"
quit QuitFailure
quit QuitSuccess
proc parseJ(x: string): JsonNode =
try:
@ -156,7 +156,7 @@ proc notes* =
say "PNimRP > " & sub
sayPos 0, ('-'.repeat int terminalWidth() / 8) & (
'>'.repeat int terminalWidth() / 12)
sayIter """PNimRP Copyright (C) 2021-2022 antonl05
sayIter """PNimRP Copyright (C) 2021-2024 antonl05/bloomingchad
This program comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome to redistribute
under certain conditions. press `t` for details"""
@ -225,8 +225,10 @@ proc getCurrentSong(linke: string): string =
link = "http://" & cleanLink link
try: #shoutcast
echo link
echo "getCurrentSong: ", link
return client.getContent(link & "/currentsong")
except ProtocolError: #ICY404 Resource Not Found?
return "notimplemented"
except HttpRequestError: #icecast
try:
return
@ -236,29 +238,46 @@ proc getCurrentSong(linke: string): string =
){"icestats"}{"source"}[1]{"yp_currently_playing"},
string
)
except HttpRequestError: return "notimplemented"
except HttpRequestError,
JsonParsingError, #different technique than implemented
ProtocolError, #connection refused?
KeyError:
return "notimplemented"
#[proc splitLink(str: string):seq[string] =
proc splitLink(str: string):seq[string] =
return rsplit(str, ":", maxSplit = 1)
proc doesLinkWork(link: string): bool =
var seq = splitLink link
try: newSocket().connect(seq[0], Port(uint16(seq[1])))
except HttpRequestError: return false
echo "doeslinkworkInit: " & link
var seq = splitLink cleanLink link
echo "doesLinkWorkSeq: ", seq
if seq.len == 1: return true #we cannot check w/o port
try:
newSocket().connect(
seq[0],
Port(uint16 parseInt seq[1]),
timeout = 3000)
echo "link dont cause except"
return true
except HttpRequestError:
warn "HttpRequestError. bad link?"
return false
except OSError:
warn "No Internet Connection, fellow."
warn "OSError. No Internet? ConnectionRefused?"
return false
except TimeoutError:
warn "timeout of 2s failed"
warn "timeout of 3s failed"
return false
]#
proc call*(sub: string; sect = ""; stat,
linke: string): Natural {.discardable.} =
var link = linke
if link == "": return 1
elif link.contains " ":
warn "link dont exist or is invalid"
if link == "": warn "link empty"
elif link.contains " ": warn "link dont exist or is invalid"
else:
clear()
if sect == "": say fmt"PNimRP > {sub} > {stat}"
@ -267,18 +286,24 @@ proc call*(sub: string; sect = ""; stat,
sayPos 0, '-'.repeat(int terminalWidth() / 8) &
'>'.repeat int terminalWidth() / 12
if not doesLinkWork link:
warn "no link work"
return
let ctx = create()
init link, ctx
var
echoPlay = true
#event = ctx.waitEvent 1000
event = ctx.waitEvent 1000 #pthread_mutex_lock
isPaused = false
nowPlayingExcept = false
echo "link in call() before while true: " & link
while true:
var event = ctx.waitEvent 1000
#if cast[EventID](Event.eventID) == IDEndFile:
if ord(event.eventID) == ord(IDEndFile):
if not isPaused: #pthread_mutex_lock:
#destroyer pause called on mutex that was destroyed.
var event = ctx.waitEvent 1000
if event.eventID in [IDEndFile, IDShutdown, IDIdle]:
warn "end of file? bad link?"
terminateDestroy ctx
break
@ -308,12 +333,16 @@ proc call*(sub: string; sect = ""; stat,
#if now().second - t0 >= 5:
# error "timeout of 5s"
#remove casting?
#[case cast[EventID](event):
of IDShutdown, IDIdle: break
else: discard]#
case getch():
of 'u', 'U':
#lyrics update func -> just to restart while true
#[cursorDown()
sayPos 4, "Updated"
eraseLine()
cursorUp()]#
discard
of 'p', 'm', 'P', 'M':
if isPaused:
if nowPlayingExcept != true:
@ -329,6 +358,7 @@ proc call*(sub: string; sect = ""; stat,
isPaused = false
else:
eraseLine()
warn "Paused/Muted", 4
cursorUp()
terminateDestroy ctx