Add parallel downloads

This commit is contained in:
cælōrum spectātrīx 2025-11-21 21:44:43 +01:00
parent b765bd42b9
commit c606c71cb7

View file

@ -3,13 +3,16 @@
exec csi -ss "$0" "$@"
|#
(define parallel 1)
(import (chicken io)
(chicken format)
(chicken port)
(chicken process-context)
(html-parser)
(http-client)
(matchable))
(matchable)
(srfi-18))
(define (find-comic-image index)
(call-with-current-continuation
@ -27,16 +30,28 @@ exec csi -ss "$0" "$@"
(call-with-input-request (find-comic-image index)
#f
(lambda (in)
(copy-port in out))))))
(copy-port in out)))))
(format (current-error-port) "* downloaded comic image ~a~%" index))
(define (download-all indices)
(for-each download-comic-image indices))
(format (current-error-port) "* using ~a threads~%" parallel)
(for-each download-batch (chop indices parallel)))
(define (download-batch batch)
(let ((threads (map (lambda (index)
(make-thread
(lambda ()
(download-comic-image index))))
batch)))
(for-each thread-start! threads)
(for-each thread-join! threads)))
(define (usage)
(format (current-error-port) "usage: ~a [OPTION ...] [INDEX ...]~%" (program-name))
(format (current-error-port) "usage: ~a [OPTION ...] INDEX ...~%" (program-name))
(format (current-error-port) "options:~%")
(format (current-error-port) " -h|-help display this help message~%")
(format (current-error-port) " -v|-version display version information~%"))
(format (current-error-port) " -h|-help display this help message~%")
(format (current-error-port) " -v|-version display version information~%")
(format (current-error-port) " -j|-parallel N download N images in parallel~%"))
(define parse-command-line
(match-lambda
@ -44,6 +59,13 @@ exec csi -ss "$0" "$@"
(usage))
(((or "-v" "-version") . _)
(format (current-error-port) "rain.scm 1.0.0~%" (program-name)))
(((or "-j" "-parallel") n . rest)
(let ((nn (string->number n)))
(unless nn
(usage)
(exit 1))
(set! parallel nn)
(parse-command-line rest)))
(()
(usage))
(indices