Skip to content

Commit 52f6152

Browse files
authored
Merge pull request #14 from chclock/master
修改web相关
2 parents a98c9f2 + 9379db6 commit 52f6152

9 files changed

Lines changed: 4229 additions & 193 deletions

File tree

apps/web-demo.ss

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
(import (web libra))
22

33
(get! "/"
4-
(lambda (p) (view "../apps/index.html")))
4+
(lambda (p) (default-make-response "GET request")))
55

66
(post! "/"
77
(lambda (p) (default-make-response "POST request")))
88

99
(get! "/blog/:user/:age"
1010
(lambda (p)
11-
(define content (string-append "p是储存所有参数(路由/get请求)的hashtable\n" "User " (hashtable-ref p "user" "") ";Ages " (hashtable-ref p "age" "")))
12-
(hashtable-set! p "content" content)
13-
(default-make-json p)))
11+
(define content (string-append "p是储存所有参数(路由/请求)的关联表; " "User: " (params-ref p "user" "") ",Ages: " (params-ref p "age" "")))
12+
(default-make-json (cons (cons "content" content) p))))
1413

1514
(run 8080)

apps/web/app.ss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77

88
(get! "/"
99
(lambda (p)
10-
(if (and (hashtable-contains? p "id") (hashtable-contains? p "page"))
11-
(id/page->load (hashtable-ref p "id" "0") (hashtable-ref p "page" "-1") (get-option "app-path")))
10+
(if (and (params-ref p "id") (params-ref p "page"))
11+
(id/page->load (params-ref p "id") (params-ref p "page") (get-option "app-path")))
1212
(view "index")))
1313

1414
(get! "/spider"
1515
(lambda (p)
16-
(define url (hashtable-ref p "key" "http://www.mm131.com/xinggan"))
17-
(define type (hashtable-ref p "type" "xinggan"))
16+
(define url (params-ref p "key" "http://www.mm131.com/xinggan"))
17+
(define type (params-ref p "type" "xinggan"))
1818
(if (eq? #f (string-index url #\_))
1919
(set! url (string-append url "/")))
2020
(default-make-json (url->id/page url type (get-option "app-path")))))

apps/web/spider.ss

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,57 @@
11
(import (net curl)
2-
(regex regex)
2+
(irregex irregex)
33
(sqlite sqlite))
44

55
;; 获取图片列表html
66
(define (get-images-html html)
7-
(define pattern "<(dl*)\\b[^>]*>(.*?)</dl>")
8-
(define rst (regex-match pattern html))
9-
(if (null? rst)
7+
(define pattern "dl[\\s\\S]*?dl")
8+
(define match (irregex-search pattern html))
9+
(if match
10+
(substring html (vector-ref match 4) (vector-ref match 6))
1011
""
11-
(car rst))
12+
)
1213
)
1314

1415
;; 获取id列表
1516
(define (html->ids html)
16-
(define pattern "<dd><a target.+?www.mm131.com/\\w+?/(\\d+?).html\">[^>]+?>(.+?)</a>")
17-
(define rst (regex-matches pattern html))
17+
(define pattern "<img.+?pic.(\\d+?)...jpg")
18+
(define len (string-length html))
1819
(define ids '())
19-
(map (lambda (lst)
20-
(set! ids (cons (cadr lst) ids)))
21-
rst)
20+
(let loop ([match (irregex-search pattern html 0 len)])
21+
(when match
22+
(set! ids (cons (substring html (vector-ref match 8) (vector-ref match 10)) ids))
23+
(loop (irregex-search pattern html (vector-ref match 6)))
24+
)
25+
)
2226
(reverse ids)
2327
)
2428

2529
;; 获取一组图片数目
2630
(define (get-max-page html)
27-
(define pattern "<span class=\"page-ch\">.+?(\\d+).+?</span>")
28-
(define rst (regex-match pattern html))
29-
(if (null? rst)
30-
0
31-
(string->number (cadr rst)))
31+
(define pattern "page-ch[^\\d]+?(\\d*?)[^\\d]+?span")
32+
(define match (irregex-search pattern html))
33+
(if match
34+
(string->number (substring html (vector-ref match 8) (vector-ref match 10)))
35+
0)
3236
)
3337

3438
;; 获取某id图片页码
3539
(define (id->page id type path)
3640
(define rst (sqlite-exec (string-append "select * from ImageInfo where id=" id)))
3741
(define max-page (if (null? rst) 0 (string->number (cadr (car rst)))))
38-
(display id)
3942
(if (= max-page 0)
4043
(begin
4144
(set! max-page (get-max-page (url->html (string-append "http://www.mm131.com/" type "/" id ".html"))))
42-
(if (not (file-exists? (string-append path "/content/images/mm/" id "-1.jpg")))
43-
(url->file
44-
(string-append "http://img1.mm131.com/pic/" id "/1.jpg")
45-
(string-append path "/content/images/mm/" id "-1.jpg")))
4645
(if (> max-page 0)
4746
(sqlite-exec (string-append "INSERT INTO ImageInfo VALUES (" id "," (number->string max-page) ");")))
4847
)
4948
)
49+
(unless (file-exists? (string-append path "/content/images/mm/" id "-1.jpg"))
50+
(url->file
51+
(string-append "http://img1.mm131.me/pic/" id "/1.jpg")
52+
(string-append path "/content/images/mm/" id "-1.jpg")))
53+
(display (string-append "http://img1.mm131.me/pic/" id "/1.jpg"))
54+
(newline)
5055
max-page
5156
)
5257

@@ -74,7 +79,7 @@
7479
(begin
7580
(if (not (file-exists? file-path))
7681
(url->file
77-
(string-append "http://img1.mm131.com/pic/" id "/" (number->string index) ".jpg")
82+
(string-append "http://img1.mm131.me/pic/" id "/" (number->string index) ".jpg")
7883
file-path))
7984
(loop (+ 1 index) (string-append path "/content/images/mm/" id "-" (number->string (+ 1 index)) ".jpg"))
8085
)

packages/irregex/README

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
irregex
3+
-------
4+
5+
Portable Efficient IrRegular Expressions
6+
7+
http://synthcode.com/scheme/irregex/
8+
9+
A fully portable and efficient R[4567]RS implementation of regular
10+
expressions, supporting both POSIX syntax with various (irregular)
11+
PCRE extensions, as well as SCSH's SRE syntax. DFA matching is used
12+
when possible, otherwise a closure-compiled NFA approach is used.
13+
14+
Documentation is in the file irregex.html.

packages/irregex/irregex-utils.scm

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
;;;; irregex-utils.scm
2+
;;
3+
;; Copyright (c) 2010 Alex Shinn. All rights reserved.
4+
;; BSD-style license: http://synthcode.com/license.txt
5+
6+
(define rx-special-chars
7+
"\\|[](){}.*+?^$#")
8+
9+
(define (string-scan-char str c . o)
10+
(let ((end (string-length str)))
11+
(let scan ((i (if (pair? o) (car o) 0)))
12+
(cond ((= i end) #f)
13+
((eqv? c (string-ref str i)) i)
14+
(else (scan (+ i 1)))))))
15+
16+
(define (irregex-quote str)
17+
(list->string
18+
(let loop ((ls (string->list str)) (res '()))
19+
(if (null? ls)
20+
(reverse res)
21+
(let ((c (car ls)))
22+
(if (string-scan-char rx-special-chars c)
23+
(loop (cdr ls) (cons c (cons #\\ res)))
24+
(loop (cdr ls) (cons c res))))))))
25+
26+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27+
28+
(define (irregex-opt ls)
29+
(define (make-alt ls)
30+
(cond ((null? (cdr ls)) (car ls))
31+
((every char? ls) (list (list->string ls)))
32+
(else (cons 'or ls))))
33+
(define (make-seq ls)
34+
(cond ((null? (cdr ls)) (car ls))
35+
((every (lambda (x) (or (string? x) (char? x))) ls)
36+
(apply string-append (map (lambda (x) (if (char? x) (string x) x)) ls)))
37+
(else (cons 'seq ls))))
38+
(cond
39+
((null? ls) "")
40+
((null? (cdr ls)) (car ls))
41+
(else
42+
(let ((chars (make-vector 256 '())))
43+
(let lp1 ((ls ls) (empty? #f))
44+
(if (null? ls)
45+
(let lp2 ((i 0) (res '()))
46+
(if (= i 256)
47+
(let ((res (make-alt (reverse res))))
48+
(if empty? `(? ,res) res))
49+
(let ((c (integer->char i))
50+
(opts (vector-ref chars i)))
51+
(lp2 (+ i 1)
52+
(cond
53+
((null? opts) res)
54+
((equal? opts '("")) `(,c ,@res))
55+
(else `(,(make-seq (list c (irregex-opt opts)))
56+
,@res)))))))
57+
(let* ((str (car ls))
58+
(len (string-length str)))
59+
(if (zero? len)
60+
(lp1 (cdr ls) #t)
61+
(let ((i (char->integer (string-ref str 0))))
62+
(vector-set!
63+
chars
64+
i
65+
(cons (substring str 1 len) (vector-ref chars i)))
66+
(lp1 (cdr ls) empty?))))))))))
67+
68+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
69+
70+
(define (cset->string ls)
71+
(let ((out (open-output-string)))
72+
(let lp ((ls ls))
73+
(cond
74+
((pair? ls)
75+
(cond
76+
((pair? (car ls))
77+
(display (irregex-quote (string (caar ls))) out)
78+
(write-char #\- out)
79+
(display (irregex-quote (string (cdar ls))) out))
80+
(else (display (irregex-quote (string (car ls))) out)))
81+
(lp (cdr ls)))))
82+
(get-output-string out)))
83+
84+
(define (sre->string obj)
85+
(let ((out (open-output-string)))
86+
(let lp ((x obj))
87+
(cond
88+
((pair? x)
89+
(case (car x)
90+
((: seq)
91+
(cond
92+
((and (pair? (cdr x)) (pair? (cddr x)) (not (eq? x obj)))
93+
(display "(?:" out) (for-each lp (cdr x)) (display ")" out))
94+
(else (for-each lp (cdr x)))))
95+
((submatch)
96+
(display "(" out) (for-each lp (cdr x)) (display ")" out))
97+
((submatch-named)
98+
(display "(?<" out) (display (cadr x) out) (display ">" out)
99+
(for-each lp (cddr x)) (display ")" out))
100+
((or)
101+
(display "(?:" out)
102+
(lp (cadr x))
103+
(for-each (lambda (x) (display "|" out) (lp x)) (cddr x))
104+
(display ")" out))
105+
((* + ? *? ??)
106+
(cond
107+
((pair? (cddr x))
108+
(display "(?:" out) (for-each lp (cdr x)) (display ")" out))
109+
(else (lp (cadr x))))
110+
(display (car x) out))
111+
((not)
112+
(cond
113+
((and (pair? (cadr x)) (eq? 'cset (caadr x)))
114+
(display "[^" out)
115+
(display (cset->string (cdadr x)) out)
116+
(display "]" out))
117+
(else (error "can't represent general 'not' in strings" x))))
118+
((cset)
119+
(display "[" out)
120+
(display (cset->string (cdr x)) out)
121+
(display "]" out))
122+
((- & / ~)
123+
(cond
124+
((or (eq? #\~ (car x))
125+
(and (eq? '- (car x)) (pair? (cdr x)) (eq? 'any (cadr x))))
126+
(display "[^" out)
127+
(display (cset->string (if (eq? #\~ (car x)) (cdr x) (cddr x))) out)
128+
(display "]" out))
129+
(else
130+
(lp `(cset ,@(sre->cset x))))))
131+
((w/case w/nocase)
132+
(display "(?" out)
133+
(if (eq? (car x) 'w/case) (display "-" out))
134+
(display ":" out)
135+
(for-each lp (cdr x))
136+
(display ")" out))
137+
(else
138+
(if (string? (car x))
139+
(lp `(cset ,@(string->list (car x))))
140+
(error "unknown sre operator" x)))))
141+
((symbol? x)
142+
(case x
143+
((bos bol) (display "^" out))
144+
((eos eol) (display "$" out))
145+
((any nonl) (display "." out))
146+
(else (error "unknown sre symbol" x))))
147+
((string? x)
148+
(display (irregex-quote x) out))
149+
((char? x)
150+
(display (irregex-quote (string x)) out))
151+
(else
152+
(error "unknown sre pattern" x))))
153+
(get-output-string out)))
154+

0 commit comments

Comments
 (0)