Partial Revert "refactor: Implement Lispy improvements - templates, strings, and error handling"
This reverts commit 5882141cfa.
This commit is contained in:
parent
d9f2ac58fc
commit
2fa72117cd
|
|
@ -1,6 +1,11 @@
|
||||||
^(in-package :asteroid)
|
(in-package :asteroid)
|
||||||
|
|
||||||
(defun icecast-now-playing (icecast-base-url)
|
(defun icecast-now-playing (icecast-base-url)
|
||||||
|
"Fetch now-playing information from Icecast server.
|
||||||
|
|
||||||
|
ICECAST-BASE-URL - Base URL of the Icecast server (e.g. http://localhost:8000)
|
||||||
|
|
||||||
|
Returns a plist with :listenurl, :title, and :listeners, or NIL on error."
|
||||||
(let* ((icecast-url (format nil "~a/admin/stats.xml" icecast-base-url))
|
(let* ((icecast-url (format nil "~a/admin/stats.xml" icecast-base-url))
|
||||||
(response (drakma:http-request icecast-url
|
(response (drakma:http-request icecast-url
|
||||||
:want-stream nil
|
:want-stream nil
|
||||||
|
|
@ -9,29 +14,32 @@
|
||||||
(let ((xml-string (if (stringp response)
|
(let ((xml-string (if (stringp response)
|
||||||
response
|
response
|
||||||
(babel:octets-to-string response :encoding :utf-8))))
|
(babel:octets-to-string response :encoding :utf-8))))
|
||||||
;; Simple XML parsing to extract source information
|
;; Extract total listener count from root <listeners> tag (sums all mount points)
|
||||||
;; Look for <source mount="/asteroid.mp3"> sections and extract title, listeners, etc.
|
;; Extract title from asteroid.mp3 mount point
|
||||||
(multiple-value-bind (match-start match-end)
|
(let* ((total-listeners (multiple-value-bind (match groups)
|
||||||
(cl-ppcre:scan "<source mount=\"/asteroid\\.mp3\">" xml-string)
|
(cl-ppcre:scan-to-strings "<listeners>(\\d+)</listeners>" xml-string)
|
||||||
|
(if (and match groups)
|
||||||
(if match-start
|
(parse-integer (aref groups 0) :junk-allowed t)
|
||||||
(let* ((source-section (subseq xml-string match-start
|
0)))
|
||||||
(or (cl-ppcre:scan "</source>" xml-string :start match-start)
|
;; Get title from asteroid.mp3 mount point
|
||||||
(length xml-string))))
|
(mount-start (cl-ppcre:scan "<source mount=\"/asteroid\\.mp3\">" xml-string))
|
||||||
(titlep (cl-ppcre:all-matches "<title>" source-section))
|
(title (if mount-start
|
||||||
(listenersp (cl-ppcre:all-matches "<listeners>" source-section))
|
(let* ((source-section (subseq xml-string mount-start
|
||||||
(title (if titlep (cl-ppcre:regex-replace-all ".*<title>(.*?)</title>.*" source-section "\\1") "Unknown"))
|
(or (cl-ppcre:scan "</source>" xml-string :start mount-start)
|
||||||
(listeners (if listenersp (cl-ppcre:regex-replace-all ".*<listeners>(.*?)</listeners>.*" source-section "\\1") "0")))
|
(length xml-string)))))
|
||||||
`((:listenurl . ,(format nil "~a/asteroid.mp3" *stream-base-url*))
|
(multiple-value-bind (match groups)
|
||||||
(:title . ,title)
|
(cl-ppcre:scan-to-strings "<title>(.*?)</title>" source-section)
|
||||||
(:listeners . ,(parse-integer listeners :junk-allowed t))))
|
(if (and match groups)
|
||||||
`((:listenurl . ,(format nil "~a/asteroid.mp3" *stream-base-url*))
|
(aref groups 0)
|
||||||
(:title . "Unknown")
|
"Unknown")))
|
||||||
(:listeners . "Unknown"))))))))
|
"Unknown")))
|
||||||
|
`((:listenurl . ,(format nil "~a/asteroid.mp3" *stream-base-url*))
|
||||||
|
(:title . ,title)
|
||||||
|
(:listeners . ,total-listeners)))))))
|
||||||
|
|
||||||
(define-api asteroid/partial/now-playing () ()
|
(define-api asteroid/partial/now-playing () ()
|
||||||
"Get Partial HTML with live status from Icecast server"
|
"Get Partial HTML with live status from Icecast server"
|
||||||
(handler-case
|
(with-error-handling
|
||||||
(let ((now-playing-stats (icecast-now-playing *stream-base-url*)))
|
(let ((now-playing-stats (icecast-now-playing *stream-base-url*)))
|
||||||
(if now-playing-stats
|
(if now-playing-stats
|
||||||
(progn
|
(progn
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue