fix: tracks and playlist db interation through data-model
This commit is contained in:
parent
559187df2e
commit
a1fa5b0b51
113
asteroid.lisp
113
asteroid.lisp
|
|
@ -51,16 +51,16 @@
|
||||||
(require-authentication)
|
(require-authentication)
|
||||||
(with-error-handling
|
(with-error-handling
|
||||||
(let ((tracks (with-db-error-handling "select"
|
(let ((tracks (with-db-error-handling "select"
|
||||||
(db:select "tracks" (db:query :all)))))
|
(dm:get "tracks" (db:query :all)))))
|
||||||
(api-output `(("status" . "success")
|
(api-output `(("status" . "success")
|
||||||
("tracks" . ,(mapcar (lambda (track)
|
("tracks" . ,(mapcar (lambda (track)
|
||||||
`(("id" . ,(gethash "_id" track))
|
`(("id" . ,(dm:id track))
|
||||||
("title" . ,(first (gethash "title" track)))
|
("title" . ,(dm:field track "title"))
|
||||||
("artist" . ,(first (gethash "artist" track)))
|
("artist" . ,(dm:field track "artist"))
|
||||||
("album" . ,(first (gethash "album" track)))
|
("album" . ,(dm:field track "album"))
|
||||||
("duration" . ,(first (gethash "duration" track)))
|
("duration" . ,(dm:field track "duration"))
|
||||||
("format" . ,(first (gethash "format" track)))
|
("format" . ,(dm:field track "format"))
|
||||||
("bitrate" . ,(first (gethash "bitrate" track)))))
|
("bitrate" . ,(dm:field track "bitrate"))))
|
||||||
tracks)))))))
|
tracks)))))))
|
||||||
|
|
||||||
;; Playlist API endpoints
|
;; Playlist API endpoints
|
||||||
|
|
@ -73,26 +73,19 @@
|
||||||
(playlists (get-user-playlists user-id)))
|
(playlists (get-user-playlists user-id)))
|
||||||
(api-output `(("status" . "success")
|
(api-output `(("status" . "success")
|
||||||
("playlists" . ,(mapcar (lambda (playlist)
|
("playlists" . ,(mapcar (lambda (playlist)
|
||||||
(let ((name-val (gethash "name" playlist))
|
(let* ((track-ids (dm:field playlist "track-ids"))
|
||||||
(desc-val (gethash "description" playlist))
|
;; Calculate track count from comma-separated string
|
||||||
(track-ids-val (gethash "track-ids" playlist))
|
;; Handle nil, empty string, or list containing empty string
|
||||||
(created-val (gethash "created-date" playlist))
|
(track-count (if (and track-ids
|
||||||
(id-val (gethash "_id" playlist)))
|
(stringp track-ids)
|
||||||
;; Calculate track count from comma-separated string
|
(not (string= track-ids "")))
|
||||||
;; Handle nil, empty string, or list containing empty string
|
(length (cl-ppcre:split "," track-ids))
|
||||||
(let* ((track-ids-str (if (listp track-ids-val)
|
0)))
|
||||||
(first track-ids-val)
|
`(("id" . ,(dm:id playlist))
|
||||||
track-ids-val))
|
("name" . ,(dm:field playlist "name"))
|
||||||
(track-count (if (and track-ids-str
|
("description" . ,(dm:field playlist "description"))
|
||||||
(stringp track-ids-str)
|
("track-count" . ,track-count)
|
||||||
(not (string= track-ids-str "")))
|
("created-date" . ,(dm:field playlist "created-date")))))
|
||||||
(length (cl-ppcre:split "," track-ids-str))
|
|
||||||
0)))
|
|
||||||
`(("id" . ,(if (listp id-val) (first id-val) id-val))
|
|
||||||
("name" . ,(if (listp name-val) (first name-val) name-val))
|
|
||||||
("description" . ,(if (listp desc-val) (first desc-val) desc-val))
|
|
||||||
("track-count" . ,track-count)
|
|
||||||
("created-date" . ,(if (listp created-val) (first created-val) created-val))))))
|
|
||||||
playlists)))))))
|
playlists)))))))
|
||||||
|
|
||||||
(define-api asteroid/playlists/create (name &optional description) ()
|
(define-api asteroid/playlists/create (name &optional description) ()
|
||||||
|
|
@ -124,23 +117,19 @@
|
||||||
(let* ((id (parse-integer playlist-id :junk-allowed t))
|
(let* ((id (parse-integer playlist-id :junk-allowed t))
|
||||||
(playlist (get-playlist-by-id id)))
|
(playlist (get-playlist-by-id id)))
|
||||||
(if playlist
|
(if playlist
|
||||||
(let* ((track-ids-raw (gethash "tracks" playlist))
|
(let* ((track-ids (dm:field playlist "tracks"))
|
||||||
(track-ids (if (listp track-ids-raw) track-ids-raw (list track-ids-raw)))
|
|
||||||
(tracks (mapcar (lambda (track-id)
|
(tracks (mapcar (lambda (track-id)
|
||||||
(let ((track-list (db:select "tracks" (db:query (:= "_id" track-id)))))
|
(dm:get-one "tracks" (db:query (:= '_id track-id))))
|
||||||
(when (> (length track-list) 0)
|
|
||||||
(first track-list))))
|
|
||||||
track-ids))
|
track-ids))
|
||||||
(valid-tracks (remove nil tracks)))
|
(valid-tracks (remove nil tracks)))
|
||||||
(api-output `(("status" . "success")
|
(api-output `(("status" . "success")
|
||||||
("playlist" . (("id" . ,id)
|
("playlist" . (("id" . ,id)
|
||||||
("name" . ,(let ((n (gethash "name" playlist)))
|
("name" . ,(dm:field playlist "name"))
|
||||||
(if (listp n) (first n) n)))
|
|
||||||
("tracks" . ,(mapcar (lambda (track)
|
("tracks" . ,(mapcar (lambda (track)
|
||||||
`(("id" . ,(gethash "_id" track))
|
`(("id" . ,(dm:id track))
|
||||||
("title" . ,(gethash "title" track))
|
("title" . ,(dm:field track "title"))
|
||||||
("artist" . ,(gethash "artist" track))
|
("artist" . ,(dm:field track "artist"))
|
||||||
("album" . ,(gethash "album" track))))
|
("album" . ,(dm:field track "album"))))
|
||||||
valid-tracks)))))))
|
valid-tracks)))))))
|
||||||
(api-output `(("status" . "error")
|
(api-output `(("status" . "error")
|
||||||
("message" . "Playlist not found"))
|
("message" . "Playlist not found"))
|
||||||
|
|
@ -152,15 +141,15 @@
|
||||||
(require-authentication)
|
(require-authentication)
|
||||||
(with-error-handling
|
(with-error-handling
|
||||||
(let ((tracks (with-db-error-handling "select"
|
(let ((tracks (with-db-error-handling "select"
|
||||||
(db:select "tracks" (db:query :all)))))
|
(dm:get "tracks" (db:query :all)))))
|
||||||
(api-output `(("status" . "success")
|
(api-output `(("status" . "success")
|
||||||
("tracks" . ,(mapcar (lambda (track)
|
("tracks" . ,(mapcar (lambda (track)
|
||||||
`(("id" . ,(gethash "_id" track))
|
`(("id" . ,(dm:id track))
|
||||||
("title" . ,(gethash "title" track))
|
("title" . ,(dm:field track "title"))
|
||||||
("artist" . ,(gethash "artist" track))
|
("artist" . ,(dm:field track "artist"))
|
||||||
("album" . ,(gethash "album" track))
|
("album" . ,(dm:field track "album"))
|
||||||
("duration" . ,(gethash "duration" track))
|
("duration" . ,(dm:field track "duration"))
|
||||||
("format" . ,(gethash "format" track))))
|
("format" . ,(dm:field track "format"))))
|
||||||
tracks)))))))
|
tracks)))))))
|
||||||
|
|
||||||
;; Stream Control API Endpoints
|
;; Stream Control API Endpoints
|
||||||
|
|
@ -173,9 +162,9 @@
|
||||||
("queue" . ,(mapcar (lambda (track-id)
|
("queue" . ,(mapcar (lambda (track-id)
|
||||||
(let ((track (get-track-by-id track-id)))
|
(let ((track (get-track-by-id track-id)))
|
||||||
`(("id" . ,track-id)
|
`(("id" . ,track-id)
|
||||||
("title" . ,(gethash "title" track))
|
("title" . ,(dm:field track "title"))
|
||||||
("artist" . ,(gethash "artist" track))
|
("artist" . ,(dm:field track "artist"))
|
||||||
("album" . ,(gethash "album" track)))))
|
("album" . ,(dm:field track "album")))))
|
||||||
queue)))))))
|
queue)))))))
|
||||||
|
|
||||||
(define-api asteroid/stream/queue/add (track-id &optional (position "end")) ()
|
(define-api asteroid/stream/queue/add (track-id &optional (position "end")) ()
|
||||||
|
|
@ -235,17 +224,7 @@
|
||||||
|
|
||||||
(defun get-track-by-id (track-id)
|
(defun get-track-by-id (track-id)
|
||||||
"Get a track by its ID - handles type mismatches"
|
"Get a track by its ID - handles type mismatches"
|
||||||
;; Try direct query first
|
(dm:get-one "tracks" (db:query (:= '_id track-id))))
|
||||||
(let ((tracks (db:select "tracks" (db:query (:= "_id" track-id)))))
|
|
||||||
(if (> (length tracks) 0)
|
|
||||||
(first tracks)
|
|
||||||
;; If not found, search manually (ID might be stored as list)
|
|
||||||
(let ((all-tracks (db:select "tracks" (db:query :all))))
|
|
||||||
(find-if (lambda (track)
|
|
||||||
(let ((stored-id (gethash "_id" track)))
|
|
||||||
(or (equal stored-id track-id)
|
|
||||||
(and (listp stored-id) (equal (first stored-id) track-id)))))
|
|
||||||
all-tracks)))))
|
|
||||||
|
|
||||||
(defun get-mime-type-for-format (format)
|
(defun get-mime-type-for-format (format)
|
||||||
"Get MIME type for audio format"
|
"Get MIME type for audio format"
|
||||||
|
|
@ -263,8 +242,8 @@
|
||||||
(track (get-track-by-id id)))
|
(track (get-track-by-id id)))
|
||||||
(unless track
|
(unless track
|
||||||
(signal-not-found "track" id))
|
(signal-not-found "track" id))
|
||||||
(let* ((file-path (first (gethash "file-path" track)))
|
(let* ((file-path (dm:field track "file-path"))
|
||||||
(format (first (gethash "format" track)))
|
(format (dm:field track "format"))
|
||||||
(file (probe-file file-path)))
|
(file (probe-file file-path)))
|
||||||
(unless file
|
(unless file
|
||||||
(error 'not-found-error
|
(error 'not-found-error
|
||||||
|
|
@ -276,8 +255,8 @@
|
||||||
(setf (radiance:header "Accept-Ranges") "bytes")
|
(setf (radiance:header "Accept-Ranges") "bytes")
|
||||||
(setf (radiance:header "Cache-Control") "public, max-age=3600")
|
(setf (radiance:header "Cache-Control") "public, max-age=3600")
|
||||||
;; Increment play count
|
;; Increment play count
|
||||||
(db:update "tracks" (db:query (:= '_id id))
|
(setf (dm:field track "play-count") (1+ (dm:field track "play-count")))
|
||||||
`(("play-count" ,(1+ (first (gethash "play-count" track))))))
|
(data-model-save track)
|
||||||
;; Return file contents
|
;; Return file contents
|
||||||
(alexandria:read-file-into-byte-vector file)))))
|
(alexandria:read-file-into-byte-vector file)))))
|
||||||
|
|
||||||
|
|
@ -333,8 +312,8 @@
|
||||||
(api-output `(("status" . "success")
|
(api-output `(("status" . "success")
|
||||||
("message" . "Playback started")
|
("message" . "Playback started")
|
||||||
("track" . (("id" . ,id)
|
("track" . (("id" . ,id)
|
||||||
("title" . ,(first (gethash "title" track)))
|
("title" . ,(dm:field track "title"))
|
||||||
("artist" . ,(first (gethash "artist" track)))))
|
("artist" . ,(dm:field track "artist"))))
|
||||||
("player" . ,(get-player-status)))))))
|
("player" . ,(get-player-status)))))))
|
||||||
|
|
||||||
(define-api asteroid/player/pause () ()
|
(define-api asteroid/player/pause () ()
|
||||||
|
|
@ -524,7 +503,7 @@
|
||||||
"Admin dashboard"
|
"Admin dashboard"
|
||||||
(require-authentication)
|
(require-authentication)
|
||||||
(let ((track-count (handler-case
|
(let ((track-count (handler-case
|
||||||
(length (db:select "tracks" (db:query :all)))
|
(length (dm:get "tracks" (db:query :all)))
|
||||||
(error () 0))))
|
(error () 0))))
|
||||||
(clip:process-to-string
|
(clip:process-to-string
|
||||||
(load-template "admin")
|
(load-template "admin")
|
||||||
|
|
|
||||||
|
|
@ -157,7 +157,7 @@
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
(with-db-error-handling \"select\"
|
(with-db-error-handling \"select\"
|
||||||
(db:select 'tracks (db:query :all)))"
|
(dm:get 'tracks (db:query :all)))"
|
||||||
`(handler-case
|
`(handler-case
|
||||||
(progn ,@body)
|
(progn ,@body)
|
||||||
(error (e)
|
(error (e)
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
(db:create "playlists" '((name :text)
|
(db:create "playlists" '((name :text)
|
||||||
(description :text)
|
(description :text)
|
||||||
(created-date :integer)
|
(created-date :integer)
|
||||||
|
(user-id :integer)
|
||||||
(track-ids :text))))
|
(track-ids :text))))
|
||||||
|
|
||||||
(unless (db:collection-exists-p "USERS")
|
(unless (db:collection-exists-p "USERS")
|
||||||
|
|
|
||||||
|
|
@ -10,94 +10,72 @@
|
||||||
(unless (db:collection-exists-p "playlists")
|
(unless (db:collection-exists-p "playlists")
|
||||||
(error "Playlists collection does not exist in database"))
|
(error "Playlists collection does not exist in database"))
|
||||||
|
|
||||||
(let ((playlist-data `(("user-id" ,user-id)
|
(let ((playlist (dm:hull "playlists")))
|
||||||
("name" ,name)
|
(setf (dm:field playlist "user-id") user-id)
|
||||||
("description" ,(or description ""))
|
(setf (dm:field playlist "name") name)
|
||||||
("track-ids" "") ; Empty string for text field
|
(setf (dm:field playlist "description") (or description ""))
|
||||||
("created-date" ,(local-time:timestamp-to-unix (local-time:now))))))
|
(setf (dm:field playlist "track-ids") "") ; Empty string for text field
|
||||||
|
(setf (dm:field playlist "created-date") (local-time:timestamp-to-unix (local-time:now)))
|
||||||
(format t "Creating playlist with user-id: ~a (type: ~a)~%" user-id (type-of user-id))
|
(format t "Creating playlist with user-id: ~a (type: ~a)~%" user-id (type-of user-id))
|
||||||
(format t "Playlist data: ~a~%" playlist-data)
|
(format t "Playlist data: ~a~%" (data-model-as-alist playlist))
|
||||||
(db:insert "playlists" playlist-data)
|
(dm:insert playlist)
|
||||||
t))
|
t))
|
||||||
|
|
||||||
(defun get-user-playlists (user-id)
|
(defun get-user-playlists (user-id)
|
||||||
"Get all playlists for a user"
|
"Get all playlists for a user"
|
||||||
(format t "Querying playlists with user-id: ~a (type: ~a)~%" user-id (type-of user-id))
|
(format t "Querying playlists with user-id: ~a (type: ~a)~%" user-id (type-of user-id))
|
||||||
(let ((all-playlists (db:select "playlists" (db:query :all))))
|
(let ((all-playlists (dm:get "playlists" (db:query :all))))
|
||||||
(format t "Total playlists in database: ~a~%" (length all-playlists))
|
(format t "Total playlists in database: ~a~%" (length all-playlists))
|
||||||
(when (> (length all-playlists) 0)
|
(when (> (length all-playlists) 0)
|
||||||
(let ((first-playlist (first all-playlists)))
|
(let* ((first-playlist (first all-playlists))
|
||||||
|
(first-playlist-user (dm:field first-playlist "user-id")))
|
||||||
(format t "First playlist user-id: ~a (type: ~a)~%"
|
(format t "First playlist user-id: ~a (type: ~a)~%"
|
||||||
(gethash "user-id" first-playlist)
|
first-playlist-user
|
||||||
(type-of (gethash "user-id" first-playlist)))))
|
(type-of first-playlist-user))))
|
||||||
;; Filter manually since DB stores user-id as a list (2) instead of 2
|
;; Filter manually since DB stores user-id as a list (2) instead of 2
|
||||||
(remove-if-not (lambda (playlist)
|
(remove-if-not (lambda (playlist)
|
||||||
(let ((stored-user-id (gethash "user-id" playlist)))
|
(let ((stored-user-id (dm:field playlist "user-id")))
|
||||||
(or (equal stored-user-id user-id)
|
(equal stored-user-id user-id)))
|
||||||
(and (listp stored-user-id)
|
|
||||||
(equal (first stored-user-id) user-id)))))
|
|
||||||
all-playlists)))
|
all-playlists)))
|
||||||
|
|
||||||
(defun get-playlist-by-id (playlist-id)
|
(defun get-playlist-by-id (playlist-id)
|
||||||
"Get a specific playlist by ID"
|
"Get a specific playlist by ID"
|
||||||
(format t "get-playlist-by-id called with: ~a (type: ~a)~%" playlist-id (type-of playlist-id))
|
(format t "get-playlist-by-id called with: ~a (type: ~a)~%" playlist-id (type-of playlist-id))
|
||||||
;; Try direct query first
|
(dm:get-one "playlists" (db:query (:= '_id playlist-id))))
|
||||||
(let ((playlists (db:select "playlists" (db:query (:= "_id" playlist-id)))))
|
|
||||||
(if (> (length playlists) 0)
|
|
||||||
(progn
|
|
||||||
(format t "Found via direct query~%")
|
|
||||||
(first playlists))
|
|
||||||
;; If not found, search manually (ID might be stored as list)
|
|
||||||
(let ((all-playlists (db:select "playlists" (db:query :all))))
|
|
||||||
(format t "Searching through ~a playlists manually~%" (length all-playlists))
|
|
||||||
(find-if (lambda (playlist)
|
|
||||||
(let ((stored-id (gethash "_id" playlist)))
|
|
||||||
(format t "Checking playlist _id: ~a (type: ~a)~%" stored-id (type-of stored-id))
|
|
||||||
(or (equal stored-id playlist-id)
|
|
||||||
(and (listp stored-id) (equal (first stored-id) playlist-id)))))
|
|
||||||
all-playlists)))))
|
|
||||||
|
|
||||||
(defun add-track-to-playlist (playlist-id track-id)
|
(defun add-track-to-playlist (playlist-id track-id)
|
||||||
"Add a track to a playlist"
|
"Add a track to a playlist"
|
||||||
(let ((playlist (get-playlist-by-id playlist-id)))
|
(db:with-transaction ()
|
||||||
(when playlist
|
(let ((playlist (get-playlist-by-id playlist-id)))
|
||||||
(let* ((current-track-ids-raw (gethash "track-ids" playlist))
|
(when playlist
|
||||||
;; Handle database storing as list - extract string
|
(let* ((current-track-ids (dm:field playlist "track-ids"))
|
||||||
(current-track-ids (if (listp current-track-ids-raw)
|
;; Parse comma-separated string into list
|
||||||
(first current-track-ids-raw)
|
(tracks-list (if (and current-track-ids
|
||||||
current-track-ids-raw))
|
(stringp current-track-ids)
|
||||||
;; Parse comma-separated string into list
|
(not (string= current-track-ids "")))
|
||||||
(tracks-list (if (and current-track-ids
|
(mapcar #'parse-integer
|
||||||
(stringp current-track-ids)
|
(cl-ppcre:split "," current-track-ids))
|
||||||
(not (string= current-track-ids "")))
|
nil))
|
||||||
(mapcar #'parse-integer
|
(new-tracks (append tracks-list (list track-id)))
|
||||||
(cl-ppcre:split "," current-track-ids))
|
;; Convert back to comma-separated string
|
||||||
nil))
|
(track-ids-str (format nil "~{~a~^,~}" new-tracks)))
|
||||||
(new-tracks (append tracks-list (list track-id)))
|
(format t "Adding track ~a to playlist ~a~%" track-id playlist-id)
|
||||||
;; Convert back to comma-separated string
|
(format t "Current track-ids raw: ~a (type: ~a)~%" current-track-ids-raw (type-of current-track-ids-raw))
|
||||||
(track-ids-str (format nil "~{~a~^,~}" new-tracks)))
|
(format t "Current track-ids: ~a~%" current-track-ids)
|
||||||
(format t "Adding track ~a to playlist ~a~%" track-id playlist-id)
|
(format t "Tracks list: ~a~%" tracks-list)
|
||||||
(format t "Current track-ids raw: ~a (type: ~a)~%" current-track-ids-raw (type-of current-track-ids-raw))
|
(format t "New tracks: ~a~%" new-tracks)
|
||||||
(format t "Current track-ids: ~a~%" current-track-ids)
|
(format t "Track IDs string: ~a~%" track-ids-str)
|
||||||
(format t "Tracks list: ~a~%" tracks-list)
|
;; Update using track-ids field (defined in schema)
|
||||||
(format t "New tracks: ~a~%" new-tracks)
|
(setf (dm:field playlist "track-ids") track-ids-str)
|
||||||
(format t "Track IDs string: ~a~%" track-ids-str)
|
(data-model-save playlist)
|
||||||
;; Update using track-ids field (defined in schema)
|
(format t "Update complete~%")
|
||||||
(db:update "playlists"
|
t)))))
|
||||||
(db:query (:= "_id" playlist-id))
|
|
||||||
`(("track-ids" ,track-ids-str)))
|
|
||||||
(format t "Update complete~%")
|
|
||||||
t))))
|
|
||||||
|
|
||||||
(defun remove-track-from-playlist (playlist-id track-id)
|
(defun remove-track-from-playlist (playlist-id track-id)
|
||||||
"Remove a track from a playlist"
|
"Remove a track from a playlist"
|
||||||
(let ((playlist (get-playlist-by-id playlist-id)))
|
(let ((playlist (get-playlist-by-id playlist-id)))
|
||||||
(when playlist
|
(when playlist
|
||||||
(let* ((current-track-ids-raw (gethash "track-ids" playlist))
|
(let* ((current-track-ids (dm:field playlist "track-ids"))
|
||||||
;; Handle database storing as list - extract string
|
|
||||||
(current-track-ids (if (listp current-track-ids-raw)
|
|
||||||
(first current-track-ids-raw)
|
|
||||||
current-track-ids-raw))
|
|
||||||
;; Parse comma-separated string into list
|
;; Parse comma-separated string into list
|
||||||
(tracks-list (if (and current-track-ids
|
(tracks-list (if (and current-track-ids
|
||||||
(stringp current-track-ids)
|
(stringp current-track-ids)
|
||||||
|
|
@ -108,28 +86,11 @@
|
||||||
(new-tracks (remove track-id tracks-list :test #'equal))
|
(new-tracks (remove track-id tracks-list :test #'equal))
|
||||||
;; Convert back to comma-separated string
|
;; Convert back to comma-separated string
|
||||||
(track-ids-str (format nil "~{~a~^,~}" new-tracks)))
|
(track-ids-str (format nil "~{~a~^,~}" new-tracks)))
|
||||||
(db:update "playlists"
|
(setf (dm:field playlist "track-ids") track-ids-str)
|
||||||
(db:query (:= "_id" playlist-id))
|
(data-model-save playlist)
|
||||||
`(("track-ids" ,track-ids-str)))
|
|
||||||
t))))
|
t))))
|
||||||
|
|
||||||
(defun delete-playlist (playlist-id)
|
(defun delete-playlist (playlist-id)
|
||||||
"Delete a playlist"
|
"Delete a playlist"
|
||||||
(db:remove "playlists" (db:query (:= "_id" playlist-id)))
|
(dm:delete "playlists" (db:query (:= '_id playlist-id)))
|
||||||
t)
|
t)
|
||||||
|
|
||||||
(defun ensure-playlists-collection ()
|
|
||||||
"Ensure playlists collection exists in database"
|
|
||||||
(unless (db:collection-exists-p "playlists")
|
|
||||||
(format t "Creating playlists collection...~%")
|
|
||||||
(db:create "playlists"))
|
|
||||||
|
|
||||||
;; Debug: Print the actual structure
|
|
||||||
(format t "~%=== PLAYLISTS COLLECTION STRUCTURE ===~%")
|
|
||||||
(format t "Structure: ~a~%~%" (db:structure "playlists"))
|
|
||||||
|
|
||||||
;; Debug: Check existing playlists
|
|
||||||
(let ((playlists (db:select "playlists" (db:query :all))))
|
|
||||||
(when playlists
|
|
||||||
(format t "Sample playlist fields: ~{~a~^, ~}~%~%"
|
|
||||||
(alexandria:hash-table-keys (first playlists))))))
|
|
||||||
|
|
|
||||||
|
|
@ -45,11 +45,8 @@
|
||||||
"Add all tracks from a playlist to the stream queue"
|
"Add all tracks from a playlist to the stream queue"
|
||||||
(let ((playlist (get-playlist-by-id playlist-id)))
|
(let ((playlist (get-playlist-by-id playlist-id)))
|
||||||
(when playlist
|
(when playlist
|
||||||
(let* ((track-ids-raw (gethash "track-ids" playlist))
|
(let* ((track-ids-str (dm:field playlist "track-ids"))
|
||||||
(track-ids-str (if (listp track-ids-raw)
|
(track-ids (if (and track-ids-str
|
||||||
(first track-ids-raw)
|
|
||||||
track-ids-raw))
|
|
||||||
(track-ids (if (and track-ids-str
|
|
||||||
(stringp track-ids-str)
|
(stringp track-ids-str)
|
||||||
(not (string= track-ids-str "")))
|
(not (string= track-ids-str "")))
|
||||||
(mapcar #'parse-integer
|
(mapcar #'parse-integer
|
||||||
|
|
@ -65,10 +62,7 @@
|
||||||
"Get the file path for a track by ID"
|
"Get the file path for a track by ID"
|
||||||
(let ((track (get-track-by-id track-id)))
|
(let ((track (get-track-by-id track-id)))
|
||||||
(when track
|
(when track
|
||||||
(let ((file-path (gethash "file-path" track)))
|
(dm:field track "file-path"))))
|
||||||
(if (listp file-path)
|
|
||||||
(first file-path)
|
|
||||||
file-path)))))
|
|
||||||
|
|
||||||
(defun convert-to-docker-path (host-path)
|
(defun convert-to-docker-path (host-path)
|
||||||
"Convert host file path to Docker container path"
|
"Convert host file path to Docker container path"
|
||||||
|
|
@ -101,11 +95,10 @@
|
||||||
(asdf:system-source-directory :asteroid))))
|
(asdf:system-source-directory :asteroid))))
|
||||||
(if (null *stream-queue*)
|
(if (null *stream-queue*)
|
||||||
;; If queue is empty, generate from all tracks (fallback)
|
;; If queue is empty, generate from all tracks (fallback)
|
||||||
(let ((all-tracks (db:select "tracks" (db:query :all))))
|
(let ((all-tracks (dm:get "tracks" (db:query :all))))
|
||||||
(generate-m3u-playlist
|
(generate-m3u-playlist
|
||||||
(mapcar (lambda (track)
|
(mapcar (lambda (track)
|
||||||
(let ((id (gethash "_id" track)))
|
(dm:id track))
|
||||||
(if (listp id) (first id) id)))
|
|
||||||
all-tracks)
|
all-tracks)
|
||||||
playlist-path))
|
playlist-path))
|
||||||
;; Generate from queue
|
;; Generate from queue
|
||||||
|
|
@ -115,11 +108,8 @@
|
||||||
"Export a user playlist to an M3U file"
|
"Export a user playlist to an M3U file"
|
||||||
(let ((playlist (get-playlist-by-id playlist-id)))
|
(let ((playlist (get-playlist-by-id playlist-id)))
|
||||||
(when playlist
|
(when playlist
|
||||||
(let* ((track-ids-raw (gethash "track-ids" playlist))
|
(let* ((track-ids-str (dm:field playlist "track-ids"))
|
||||||
(track-ids-str (if (listp track-ids-raw)
|
(track-ids (if (and track-ids-str
|
||||||
(first track-ids-raw)
|
|
||||||
track-ids-raw))
|
|
||||||
(track-ids (if (and track-ids-str
|
|
||||||
(stringp track-ids-str)
|
(stringp track-ids-str)
|
||||||
(not (string= track-ids-str "")))
|
(not (string= track-ids-str "")))
|
||||||
(mapcar #'parse-integer
|
(mapcar #'parse-integer
|
||||||
|
|
@ -128,7 +118,6 @@
|
||||||
(generate-m3u-playlist track-ids output-path)))))
|
(generate-m3u-playlist track-ids output-path)))))
|
||||||
|
|
||||||
;;; Stream History Management
|
;;; Stream History Management
|
||||||
|
|
||||||
(defun add-to-stream-history (track-id)
|
(defun add-to-stream-history (track-id)
|
||||||
"Add a track to the stream history"
|
"Add a track to the stream history"
|
||||||
(push track-id *stream-history*)
|
(push track-id *stream-history*)
|
||||||
|
|
@ -145,12 +134,11 @@
|
||||||
|
|
||||||
(defun build-smart-queue (genre &optional (count 20))
|
(defun build-smart-queue (genre &optional (count 20))
|
||||||
"Build a smart queue based on genre"
|
"Build a smart queue based on genre"
|
||||||
(let ((tracks (db:select "tracks" (db:query :all))))
|
(let ((tracks (dm:get "tracks" (db:query :all))))
|
||||||
;; For now, just add random tracks
|
;; For now, just add random tracks
|
||||||
;; TODO: Implement genre filtering when we have genre metadata
|
;; TODO: Implement genre filtering when we have genre metadata
|
||||||
(let ((track-ids (mapcar (lambda (track)
|
(let ((track-ids (mapcar (lambda (track)
|
||||||
(let ((id (gethash "_id" track)))
|
(dm:id track))
|
||||||
(if (listp id) (first id) id)))
|
|
||||||
tracks)))
|
tracks)))
|
||||||
(setf *stream-queue* (subseq (alexandria:shuffle track-ids)
|
(setf *stream-queue* (subseq (alexandria:shuffle track-ids)
|
||||||
0
|
0
|
||||||
|
|
@ -160,18 +148,16 @@
|
||||||
|
|
||||||
(defun build-queue-from-artist (artist-name &optional (count 20))
|
(defun build-queue-from-artist (artist-name &optional (count 20))
|
||||||
"Build a queue from tracks by a specific artist"
|
"Build a queue from tracks by a specific artist"
|
||||||
(let ((tracks (db:select "tracks" (db:query :all))))
|
(let ((tracks (dm:get "tracks" (db:query :all))))
|
||||||
(let ((matching-tracks
|
(let ((matching-tracks
|
||||||
(remove-if-not
|
(remove-if-not
|
||||||
(lambda (track)
|
(lambda (track)
|
||||||
(let ((artist (gethash "artist" track)))
|
(let ((artist (dm:field track "artist")))
|
||||||
(when artist
|
(when artist
|
||||||
(let ((artist-str (if (listp artist) (first artist) artist)))
|
(search artist-name artist :test #'char-equal))))
|
||||||
(search artist-name artist-str :test #'char-equal)))))
|
|
||||||
tracks)))
|
tracks)))
|
||||||
(let ((track-ids (mapcar (lambda (track)
|
(let ((track-ids (mapcar (lambda (track)
|
||||||
(let ((id (gethash "_id" track)))
|
(dm:id track))
|
||||||
(if (listp id) (first id) id)))
|
|
||||||
matching-tracks)))
|
matching-tracks)))
|
||||||
(setf *stream-queue* (subseq track-ids 0 (min count (length track-ids))))
|
(setf *stream-queue* (subseq track-ids 0 (min count (length track-ids))))
|
||||||
(regenerate-stream-playlist)
|
(regenerate-stream-playlist)
|
||||||
|
|
@ -192,7 +178,7 @@
|
||||||
(let* ((m3u-path (merge-pathnames "stream-queue.m3u"
|
(let* ((m3u-path (merge-pathnames "stream-queue.m3u"
|
||||||
(asdf:system-source-directory :asteroid)))
|
(asdf:system-source-directory :asteroid)))
|
||||||
(track-ids '())
|
(track-ids '())
|
||||||
(all-tracks (db:select "tracks" (db:query :all))))
|
(all-tracks (dm:get "tracks" (db:query :all))))
|
||||||
|
|
||||||
(when (probe-file m3u-path)
|
(when (probe-file m3u-path)
|
||||||
(with-open-file (stream m3u-path :direction :input)
|
(with-open-file (stream m3u-path :direction :input)
|
||||||
|
|
@ -206,14 +192,12 @@
|
||||||
;; Find track by file path
|
;; Find track by file path
|
||||||
(let ((track (find-if
|
(let ((track (find-if
|
||||||
(lambda (trk)
|
(lambda (trk)
|
||||||
(let ((fp (gethash "file-path" trk)))
|
(let ((file-path (dm:field trk "file-path")))
|
||||||
(let ((file-path (if (listp fp) (first fp) fp)))
|
(string= file-path host-path)))
|
||||||
(string= file-path host-path))))
|
|
||||||
all-tracks)))
|
all-tracks)))
|
||||||
(when track
|
(when track
|
||||||
(let ((id (gethash "_id" track)))
|
(push (dm:id track) track-ids))))))))
|
||||||
(push (if (listp id) (first id) id) track-ids)))))))))
|
|
||||||
|
|
||||||
;; Reverse to maintain order from file
|
;; Reverse to maintain order from file
|
||||||
(setf track-ids (nreverse track-ids))
|
(setf track-ids (nreverse track-ids))
|
||||||
(setf *stream-queue* track-ids)
|
(setf *stream-queue* track-ids)
|
||||||
|
|
|
||||||
|
|
@ -62,16 +62,17 @@
|
||||||
(defun track-exists-p (file-path)
|
(defun track-exists-p (file-path)
|
||||||
"Check if a track with the given file path already exists in the database"
|
"Check if a track with the given file path already exists in the database"
|
||||||
;; Try direct query first
|
;; Try direct query first
|
||||||
(let ((existing (db:select "tracks" (db:query (:= "file-path" file-path)))))
|
(let ((existing (dm:get "tracks" (db:query (:= "file-path" file-path)))))
|
||||||
(if (> (length existing) 0)
|
(if (> (length existing) 0)
|
||||||
t
|
t
|
||||||
;; If not found, search manually (file-path might be stored as list)
|
;; If not found, search manually (file-path might be stored as list)
|
||||||
(let ((all-tracks (db:select "tracks" (db:query :all))))
|
(let ((all-tracks (dm:get "tracks" (db:query :all))))
|
||||||
(some (lambda (track)
|
(some (lambda (track)
|
||||||
(let ((stored-path (gethash "file-path" track)))
|
(let ((stored-path (dm:field track "file-path")))
|
||||||
(or (equal stored-path file-path)
|
(or (equal stored-path file-path)
|
||||||
(and (listp stored-path) (equal (first stored-path) file-path)))))
|
(and (listp stored-path) (equal (first stored-path) file-path)))))
|
||||||
all-tracks)))))
|
all-tracks)
|
||||||
|
))))
|
||||||
|
|
||||||
(defun insert-track-to-database (metadata)
|
(defun insert-track-to-database (metadata)
|
||||||
"Insert track metadata into database if it doesn't already exist"
|
"Insert track metadata into database if it doesn't already exist"
|
||||||
|
|
@ -83,17 +84,17 @@
|
||||||
(let ((file-path (getf metadata :file-path)))
|
(let ((file-path (getf metadata :file-path)))
|
||||||
(if (track-exists-p file-path)
|
(if (track-exists-p file-path)
|
||||||
nil
|
nil
|
||||||
(progn
|
(let ((track (dm:hull "tracks")))
|
||||||
(db:insert "tracks"
|
(setf (dm:field track "title") (getf metadata :title))
|
||||||
(list (list "title" (getf metadata :title))
|
(setf (dm:field track "artist") (getf metadata :artist))
|
||||||
(list "artist" (getf metadata :artist))
|
(setf (dm:field track "album") (getf metadata :album))
|
||||||
(list "album" (getf metadata :album))
|
(setf (dm:field track "duration") (getf metadata :duration))
|
||||||
(list "duration" (getf metadata :duration))
|
(setf (dm:field track "file-path") file-path)
|
||||||
(list "file-path" file-path)
|
(setf (dm:field track "format") (getf metadata :format))
|
||||||
(list "format" (getf metadata :format))
|
(setf (dm:field track "bitrate") (getf metadata :bitrate))
|
||||||
(list "bitrate" (getf metadata :bitrate))
|
(setf (dm:field track "added-date") (local-time:timestamp-to-unix (local-time:now)))
|
||||||
(list "added-date" (local-time:timestamp-to-unix (local-time:now)))
|
(setf (dm:field track "play-count") 0)
|
||||||
(list "play-count" 0)))
|
(dm:insert track)
|
||||||
t))))
|
t))))
|
||||||
|
|
||||||
(defun scan-music-library (&optional (directory *music-library-path*))
|
(defun scan-music-library (&optional (directory *music-library-path*))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue