Download
(defgroup topsongs nil
"A simple songs database."
:tag "topsongs"
:prefix "topsongs-"
:group 'emms)
(defcustom topsongs-directory "~/.topsongs"
"*Metadata directory."
:group 'topsongs
:type 'string)
(defcustom topsongs-temporary "/tmp/topsongs.m3u"
"*Temporary m3u file (used for playing)."
:group 'topsongs
:type 'string)
(make-directory topsongs-directory t)
(defun emms-info-track-description-m3u (track)
"Return a m3u description of the current track."
(let ((artist (emms-track-get track 'info-artist "unknown"))
(title (emms-track-get track 'info-title "unknown"))
(time (emms-track-get track 'info-playing-time "")))
(format "#EXTINF:%s,%s - %s" time artist title)))
(defun topsongs-insert-current-selected-track ()
"Insert the current selected track into top-songs database."
(interactive)
(if emms-player-playing-p
(let ((track (emms-playlist-current-selected-track)))
(if track
(let ((song (emms-track-name track)))
(let ((file (format "%s/%s.ttd"
topsongs-directory
(sha1 song)))
(name (emms-track-get track
'info-title "unknown"))
(doit "inserted"))
(if (file-exists-p file)
(let ((prompt
(format "'%s' already in database, substitute? "
name)))
(setq doit
(if (y-or-n-p-with-timeout prompt 5 nil)
"replaced"
nil))))
(if doit
(progn
(write-region
(format "%s\n%s\n"
(emms-info-track-description-m3u track) song)
nil file nil 'quietly)
(message "'%s' has been %s into top-songs database"
name doit))
(message "top-songs operation on '%s' aborted"
name))))
(message "No track information available")))
(message "Nothing playing right now")))
(defun topsongs-remove-current-selected-track ()
"Remove the current selected track from the top-songs database."
(interactive)
(if emms-player-playing-p
(let ((track (emms-playlist-current-selected-track)))
(if track
(let ((song (emms-track-name track)))
(let ((file (format "%s/%s.ttd"
topsongs-directory
(sha1 song)))
(name (emms-track-get track
'info-title "unknown")))
(if (file-exists-p file)
(progn
(delete-file file)
(message "'%s' has been deleted from the top-songs database"
name))
(message "'%s' not in top-songs database" name))))
(message "No track information available")))
(message "Nothing playing right now")))
(defun topsongs-save (file)
"Convert the top-songs database in m3u format."
(interactive)
(save-excursion
(let ((temp-buf (generate-new-buffer "topsongs")))
(set-buffer temp-buf)
(bury-buffer temp-buf)
(insert "#EXTM3U\n")
(dolist (file (directory-files topsongs-directory nil "\.ttd$" t))
(insert-file-contents (format "%s/%s" topsongs-directory file)))
(write-file file nil))))
(defun topsongs-play ()
"Play the whole top-songs database."
(interactive)
(topsongs-save topsongs-temporary)
(emms-play-m3u-playlist topsongs-temporary))
(provide 'topsongs)