-
Notifications
You must be signed in to change notification settings - Fork 0
/
favrand
executable file
·151 lines (131 loc) · 5.1 KB
/
favrand
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/local/bin/gosh
(use srfi-1) ; remove
(use srfi-19) ; date
(use srfi-43) ; vector
(use rfc.822) ; rfc822-date->date
(use rfc.http) ; http-get
(use rfc.json) ; parse-json-string
(use util.list) ; assoc-ref
(use util.list) ; intersperse
(use math.mt-random) ; <mersenne-twister>, mt-random-integer
(use gauche.parseopt) ; let-args
(use gauche.process) ; run-process
(define *twitter-api-server* "api.twitter.com")
(define (usage)
(print "usage: favrand [options ...] <twitter-id>")
(print " - j|json : print random tweet json")
(print " - u|user : twitter use id")
(print " - a|auto : auto refresh (millisecond)")
(exit 0))
(define rand
(let1 m (make <mersenne-twister> :seed (sys-time))
(^n (mt-random-integer m n))))
;; http://valvallow.blogspot.com/2011/02/query-string.html
(define (query-compose query)
(string-join (map (cut string-join <> "=") query) "&"))
(define (twitter-api-get server url params)
(receive (status header body)
(http-get server
(string-append url "." "json"
"?" (query-compose params)))
(rlet1 r (parse-json-string body)
(when (and (not (vector? r))
(string=? "error" (caar r)))
(raise "user not found."))
)))
(define (twitter-api-user-show id)
(twitter-api-get *twitter-api-server*
"/1/users/show" `(("id" ,id))))
(define (twitter-api-random-favorite-page-number id)
(let* ((total-fav-count (assoc-ref (twitter-api-user-show id)
"favourites_count"))
(pages (quotient total-fav-count 20)))
(rand (+ pages 1)))) ;; favorite page index started 1;
(define (twitter-api-favorites id :optional (page 1))
(twitter-api-get *twitter-api-server*
"/1/favorites"
`(("id" ,id)("page" ,(x->string page)))))
(define (twitter-api-random-favorite id)
(let ((favs (twitter-api-favorites
id (twitter-api-random-favorite-page-number id))))
(when (equal? favs #())
(raise #`",id has no favorite."))
(vector-ref favs (rand (vector-length favs)))))
(define (twitter-date->date str)
(define (list-join delim ls)
(apply string-append (intersperse delim ls)))
(let* ((ls (string-split str " "))
(date-string (list-join " " (map (pa$ list-ref ls)
'(0 2 1 5 3 4)))))
(rfc822-date->date date-string)))
(define (print-favorite fav)
(define (twitter-date-format str)
(date->string (twitter-date->date str) "~Y/~m/~d ~H:~M:~S"))
(let* ((fav-id (assoc-ref fav "id"))
(text (assoc-ref fav "text"))
(user (assoc-ref fav "user"))
(screen-name (assoc-ref user "screen_name"))
(name (assoc-ref user "name"))
(retweets (assoc-ref fav "retweet_count"))
(created (assoc-ref fav "created_at")))
(let ((fav-url (format "http://twitter.com/~a/status/~a"
screen-name fav-id)))
(print #`"id : ,name (@,screen-name)")
(print #`"url : ,fav-url")
(print #`"date : ,(twitter-date-format created)")
(print #`"retweet count : ,retweets")
(print text))))
(define (path-info->alist str keys)
(and str (map cons keys (cdr (string-split str "/")))))
(define (print-random-favorite id)
(guard (e (else (print e)))
(print-favorite (twitter-api-random-favorite id))))
(define (walk-json-alist alist :key (key-fun identity)(val-fun identity))
(define (junction x)
(cond ((pair? x)(walk-json-alist x :key-fun key-fun :val-fun val-fun))
((vector? x)(vector-case x))
(else (val-fun x))))
(define (vector-case v)
(vector-map (^ (idx e)
(junction e))
v))
(map (^l (cons (key-fun (car l))
(junction (cdr l))))
alist))
(define (transform-tweet-json alist)
(walk-json-alist
alist
:val-fun (lambda (s)
(if (string? s)
(regexp-replace-all #/\"/ s "\\\\\"")
s))))
(define (print-random-favorite-auto-refresh user interval)
(dynamic-wind
(^ _ (run-process '(tput "civis"))
(run-process '(tput "clear")))
(^ _ (let1 tweet (twitter-api-random-favorite user)
(while #t
(print-favorite tweet)
(print)
(sys-nanosleep (* interval 1000000))
(set! tweet (twitter-api-random-favorite user))
(flush))))
(^ _ (run-process '(tput "cnorm")))))
(define (num-format cur max)
(format "~3d%" (round->exact (/. (* cur 100) max))))
(define (main args)
(let-args (cdr args)
((json "j|json")
(user "u|user=s")
(auto-refresh "a|auto=i")
(help "h|help" => usage)
. rest)
(let ((user (or user
(and (not (null? rest))
(car rest))
(read-line))))
(cond (json (print (construct-json-string
(transform-tweet-json
(twitter-api-random-favorite user)))))
(auto-refresh (print-random-favorite-auto-refresh user auto-refresh))
(else (print-random-favorite user))))))