Created
March 17, 2013 22:04
-
-
Save anonymous/5183846 to your computer and use it in GitHub Desktop.
musicsig.scm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/guile \ | |
-e main -s | |
!# | |
(use-modules (ice-9 getopt-long) | |
(ice-9 rw) | |
(ice-9 regex) | |
(ice-9 popen) | |
(ice-9 format)) | |
(define file-regexp (make-regexp "#EXTINF:([0-9]+),([^\n]+)[\n ]+(http:[^\n]+)")) | |
(define (file-contents file) | |
(call-with-input-file file | |
(lambda (p) | |
(let* ((size (stat:size (stat p))) | |
(buf (make-string size))) | |
(read-string!/partial buf p) | |
buf)))) | |
(define (get-files file path debug) | |
(define (download match count) | |
(let* ((num (string-trim-both (match:substring match 1))) | |
(fname (string-trim-both (match:substring match 2))) | |
(url (string-trim-both (match:substring match 3))) | |
(ext (car (reverse (string-split url #\.)))) | |
(esc (regexp-substitute/global #f "\"" fname 'pre "\\\"" 'post)) | |
(fpath (format #f "\"~a~a.~a.~a\"" path num esc ext)) | |
(cmd (format #f "wget \"~a\" -O ~a || rm -f ~a" url fpath fpath))) | |
(if debug | |
(format #t "~a~%" cmd) | |
(system cmd)) | |
(1+ count))) | |
(format #t | |
"Downloaded ~a files~%" | |
(fold-matches file-regexp (file-contents file) 0 download))) | |
(define (main args) | |
(let* ((option-spec '((debug (single-char #\d) (value #f)) | |
(file (single-char #\f) (value #t)) | |
(path (single-char #\p) (value #t)) | |
(help (single-char #\h) (value #f)))) | |
(options (getopt-long args option-spec)) | |
(show-help (option-ref options 'help #f)) | |
(file (option-ref options 'file #f)) | |
(debug (option-ref options 'debug #f)) | |
(path (option-ref options 'path ""))) | |
(if (or show-help (not file)) | |
(format #t "\ | |
Usage: [options] | |
\t-p|--path\tDownload directory (default: \"\") | |
\t-d|--debug\tDebug | |
\t-h|--help\t | |
\t-f|--file\tData file~%") | |
(get-files file path debug)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment