diff options
Diffstat (limited to 'downloader.lisp')
-rw-r--r-- | downloader.lisp | 63 |
1 files changed, 34 insertions, 29 deletions
diff --git a/downloader.lisp b/downloader.lisp index 246bf29..3142277 100644 --- a/downloader.lisp +++ b/downloader.lisp @@ -2,23 +2,26 @@ (in-package #:vampire) -(defvar *dl-cluster* - nil) +(defvar *dl-cluster*) +(defvar *media-directory*) -(defun start-downloader-service (&key (count 5)) - (setf *dl-cluster* (or *dl-cluster* (legion:make-cluster count 'run-job))) +(defun start-downloader-service (config) + (let ((media-dir + (merge-pathnames "media/" (static-directory config)))) + (ensure-directories-exist media-dir) + (setf *dl-cluster* (legion:make-cluster (downloader-threads config) + (lambda (job) (funcall job media-dir))))) (legion:start *dl-cluster*)) -(defun run-job (thunk) - (funcall thunk)) - (defun add-fetch-track-job (url ok err) + "URL is a url to some audio track. OK is a function that accepts a + TRACK instance. ERR is a function accepting an error condition" (legion:add-job *dl-cluster* - (lambda () - (if-let (track (download-media url)) - (funcall ok track) - (funcall err url))))) + (lambda (media-dir) + (handler-case + (funcall ok (download-media url media-dir)) + (error (e) (funcall err e)))))) (defun trackinfo-file (dir name) (merge-pathnames @@ -53,22 +56,24 @@ :duration dur :thumb-url url)))) -(defun download-media (url) +(defun download-media (url media-dir) + "Download media and create a new track from its audio source, moving + the raw audio to the media-dir when done." (with-temp-dir (tmpdir) - (handler-case - (let* ((tmpname - (default-name "media")) - (trackinfo-file - (trackinfo-file tmpdir tmpname))) - (uiop:run-program - (format nil "youtube-dl --write-info-json -x -o \"~a/~a.%(ext)s\" ~a" - tmpdir tmpname url)) - (when (uiop:file-exists-p trackinfo-file) - (let ((info - (trackinfo trackinfo-file))) - (new-track - (trackmedia-file tmpdir) - info)))) - (error (e) - (format t "ERROR: ~a~%" e) - nil)))) + (let* ((tmpname + (default-name "media")) + (trackinfo-file + (trackinfo-file tmpdir tmpname))) + (uiop:run-program + (format nil "youtube-dl --write-info-json -x -o \"~a/~a.%(ext)s\" ~a" + tmpdir tmpname url)) + (let* ((info + (trackinfo trackinfo-file)) + (downloaded + (trackmedia-file tmpdir)) + (file + (merge-pathnames (format nil "~a.~a" + (nuid) (pathname-type downloaded)) + media-dir))) + (uiop:copy-file downloaded file) + (new-track file info))))) |