-
Notifications
You must be signed in to change notification settings - Fork 0
/
jyoseki.lisp
150 lines (143 loc) · 6.65 KB
/
jyoseki.lisp
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
(defpackage #:rakugokyokai-parser/jyoseki
(:use #:cl
#:rakugokyokai-parser/utils
#:rakugobot-utils
#:lquery)
(:import-from #:local-time)
(:import-from #:cl-ppcre)
(:import-from #:assoc-utils
#:aget)
(:export #:parse-jyoseki))
(in-package #:rakugokyokai-parser/jyoseki)
(defparameter *base-uri*
"http://rakugo-kyokai.jp/program/suzumoto/index.php")
(defun normalize-month (month)
(cond
((ignore-errors (parse-integer month)))
((aget '(("1" . 1)
("2" . 2)
("3" . 3)
("4" . 4)
("5" . 5)
("6" . 6)
("7" . 7)
("8" . 8)
("9" . 9))
month))
((equal month "正") 1)
(t (error "Invalid month: ~S" month))))
(defun get-year-of-month (month)
(let ((today (local-time:today)))
(if (< month (local-time:timestamp-month today))
(1+ (local-time:timestamp-year today))
(local-time:timestamp-year today))))
(defun parse-jyoseki-period (month period part)
(let ((year (get-year-of-month month)))
(cond
((or (string= period "上")
(string= period "初"))
(values (if (equal part "後半")
6
1)
(if (equal part "前半")
5
10)))
((or (string= period "中")
(string= period "二之"))
(values (if (equal part "後半")
16
11)
(if (equal part "前半")
15
20)))
((string= period "下")
(values (if (equal part "後半")
26
21)
(if (equal part "前半")
25
(if (= month 2)
(local-time:days-in-month month year)
30))))
(t (error "Unexpected period format: ~A" period)))))
(defun parse-title (title)
(let ((year (nth-value 5 (decode-universal-time (get-universal-time)))))
(cond
;; 本日の寄席
((ppcre:register-groups-bind (hall (#'parse-integer mon day))
("^(.+):(\\d{1,2})月(\\d{1,2})日\\(.\\)$" title)
`(("title" . ,(format nil "~A ~2,'0D月~2,'0D日"
hall mon day))
("hall" . ,hall)
("date" . ,(format nil "~D-~2,'0D-~2,'0D"
year mon day)))))
;; 黒門亭
((ppcre:register-groups-bind (hall (#'parse-integer mon day))
("^(.+)(?: |\\s+?)(\\d{1,2})月(\\d{1,2})日" title)
`(("title" . ,(format nil "~A ~2,'0D月~2,'0D日" hall mon day))
("hall" . ,hall)
("date" . ,(format nil "~D-~2,'0D-~2,'0D"
year mon day)))))
;; 定席番組
((ppcre:register-groups-bind (hall (#'normalize-month mon) period part)
("^(.+)(?: |\\s+?)(\\d{1,2}|[123456789]|正)月(上|中|下|初|二之)席(?:((前半|後半)))?$" title)
(let ((year (get-year-of-month mon)))
(multiple-value-bind (start-day end-day) (parse-jyoseki-period mon period part)
`(("title" . ,(format nil "~A ~A月~A席~:[~;~:*(~A)~]" hall mon period part))
("hall" . ,hall)
("date-from" . ,(format nil "~D-~2,'0D-~2,'0D" year mon start-day))
("date-to" . ,(format nil "~D-~2,'0D-~2,'0D" year mon end-day)))))))
(t
`(("title" . ,title))))))
(defun %parse-jyoseki-html (body)
(let ((boxes ($ (initialize body) ".main .contents .member-detail")))
(loop for box across boxes
append
(coerce
($ box (combine "h2" ".inner")
(map-apply (lambda (h2 inner)
`(,@(parse-title ($1 h2 (render-text)))
("tables"
. ,(coerce
($ inner ".time-table table"
(map (lambda (table)
`(("subtitle" . ,(remove #\Space ($1 table "caption" (render-text))))
("entertainers"
. ,(coerce
($ table ".name a"
(map
(lambda (name)
($1 name (combine (render-text) (attr "href"))
(map-apply (lambda (name uri)
`(("name" . ,name)
("uri" . ,(merge-uris uri *base-uri*)))))))))
'list))))))
'list))))))
'list))))
(defun parse-jyoseki (body)
(let ((res (%parse-jyoseki-html body)))
(loop for schedule in res
for tables = (aget schedule "tables")
append (let ((title (aget schedule "title"))
(hall (normalize-hall-name (aget schedule "hall"))))
(loop for table in tables
collect (let ((subtitle (aget table "subtitle")))
(append
`(("title" . ,(format nil "~A ~A" title subtitle))
("place" . ,hall)
("address" . ,(hall-address hall))
("start-date" . ,(aget schedule "date-from"))
("end-date" . ,(aget schedule "date-to")))
(cond
((equal subtitle "昼席")
(multiple-value-bind (start-time end-time)
(jyoseki-time hall 0)
`(("start-time" . ,start-time)
("end-time" . ,end-time))))
((equal subtitle "夜席")
(multiple-value-bind (start-time end-time)
(jyoseki-time hall 1)
`( ("start-time" . ,start-time)
("end-time" . ,end-time))))
(t (error "Invalid subtitle: ~A" subtitle)))
`(("performers" . ,(aget table "entertainers"))))))))))