Created
August 2, 2013 09:47
-
-
Save valvallow/6138734 to your computer and use it in GitHub Desktop.
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/env gosh | |
(use file.util) | |
(use net.twitter) | |
(use gauche.threads) | |
(use gauche.parseopt) | |
(define (usage cmd) | |
(print "Usage: " (sys-basename cmd) " [option ...] message") | |
(print " h|help : Show this help") | |
(print " n|number : Number of continuous tweets (default:endress)") | |
(print " k|key-file : The path of the file for which have listed the Consumer-key and Access-token") | |
(print " ex) consumer-key1 consumer-secret1 access-token1 access-token-secret1") | |
(print " consumer-key2 consumer-secret2 access-token2 access-token-secret2") | |
(print " ...") | |
(print " consumer-keyn consumer-secretn access-tokenn access-token-secretn") | |
(print " (default: " +RC_FILE_PATH+ ")") | |
(print " i|interval :Interval (default: 0.1)") | |
(print " s|suffix :Message suffix (default: !)") | |
(print " d|debug :Debug mode. Print message to console, Twitter not update") | |
(exit)) | |
(define +RC_FILE_PATH+ "~/.tbbbrc") | |
(define *threads* '()) | |
;; C-c to exit | |
(set-signal-handler! SIGINT (^ _ (exit))) | |
;; n=1, msg=hoge => "hoge " | |
;; n=2, msg=hoge => "hoge " | |
;; n=3, msg=hoge => "hoge " | |
(define (make-nth-tweet n msg suffix) | |
(apply string-append msg (make-list n suffix))) | |
;; file spec: refer to the help option k|key-file | |
(define (file->credentials file) | |
(if (not (file-exists? (expand-path file))) | |
'() | |
(map (^(line) | |
(apply make <twitter-cred> | |
(append-map list | |
(list :consumer-key | |
:consumer-secret | |
:access-token | |
:access-token-secret) | |
(string-split line #\space)))) | |
(file->string-list (expand-path file))))) | |
(define (main args) | |
(let-args (cdr args) | |
((help "h|help" => (cut usage (car args))) | |
(number "n|number=i" +inf.0) | |
(keys "k|keys=s" +RC_FILE_PATH+) | |
(interval "i|interval=f" 0.1) | |
(suffix "s|suffix=s" "!") | |
(debug "d|debug") | |
(else (opt . _) | |
(print "Unknown option : " opt) | |
(usage (car args))) | |
. rest) | |
(when debug | |
(set! twitter-update | |
(^(cred msg) | |
(print cred " " msg)))) | |
(let1 credentials (file->credentials keys) | |
(when (or (null? (car rest)) ; massage | |
(null? credentials)) | |
(usage (car args))) | |
(let/cc break | |
(dotimes (i number) | |
(let1 msg (make-nth-tweet i (car rest) suffix) | |
(when (< 140 (string-length msg)) | |
(break)) | |
(for-each (^c (thread-sleep! interval) | |
(let1 thread (make-thread | |
(^ _ (twitter-update c msg) | |
(print i " done!"))) | |
(push! *threads* thread) | |
(thread-start! thread))) | |
credentials)))) | |
(when debug | |
(dotimes (i 5) | |
(display ".") | |
(flush) | |
(thread-sleep! 1)) | |
(print) | |
(for-each (.$ print (pa$ thread-state)) *threads*)) | |
))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment