summaryrefslogtreecommitdiff
path: root/zipper.lisp
blob: 215101917ed43ea6d563f0d7f9fdcbc6e0ccd1fe (plain)
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
;;;; zipper.lisp

(in-package :vampire)

(defun zip-playlist (playlist)
  "Copies all playlist tracks into a download directory and compresses them into a zip archive.
Returns the filename of the zip file."
  (with-temp-dir (tmpdir)
    (let* ((tracks (mapcar (lambda (track) (merge-pathnames tmpdir track))
                           (zip-playlist-filenames playlist)))
           (zip-dir (merge-pathnames "media/bundled-playlists" (static-directory *config*)))
           (out (format nil "~a/~a.zip" zip-dir (clean-slashes (playlist-title playlist)))))
      (ensure-directories-exist zip-dir)
      (copy-audio-files-for-download playlist tmpdir)
      (org.shirakumo.zippy:compress-zip tracks out :if-exists :overwrite)
      (format nil "/media/bundled-playlists/~a.zip" (clean-slashes (playlist-title playlist))))))

(defun copy-audio-files-for-download (playlist dir)
  (let ((audio-files (mapcar #'track-file (playlist-tracks playlist)))
	(dest-files (zip-playlist-filenames playlist)))
    (loop for file in audio-files
	  for dest in dest-files
	  do (uiop:copy-file file (format nil "~a~a" dir dest)))))

(defun zip-playlist-filenames (playlist)
   (loop for track in (playlist-tracks playlist)
	 for i from 1
	 collect (zip-track-filename track i)))

(defun zip-track-filename (track pos)
  "Return a filename for a track. `NN-ARTIST-ALBUM-TITLE.CODEC'"
  (with-slots (artist album title codec) track
    (format nil "~2,'0d-~a-~a-~a.~a" pos
	    (clean-slashes artist) (clean-slashes album) (clean-slashes title) codec)))

(defun clean-slashes (str)
  (cl-ppcre:regex-replace-all "/" str "-"))