]> gitweb.factorcode.org Git - factor.git/blob - extra/robots/robots.factor
factor: trim using lists
[factor.git] / extra / robots / robots.factor
1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs calendar.parser combinators
4 combinators.short-circuit globs http.client kernel math.parser
5 namespaces present regexp regexp.combinators sequences splitting
6 splitting.monotonic unicode urls ;
7 IN: robots
8
9 ! visit-time is GMT, request-rate is pages/second
10 ! crawl-rate is seconds
11
12 SYMBOL: robot-identities
13 robot-identities [ { "FactorSpider" } ] initialize
14
15 TUPLE: robots site sitemap rules rules-quot ;
16
17 : <robots> ( site sitemap rules -- robots )
18     \ robots new
19         swap >>rules
20         swap >>sitemap
21         swap >>site ;
22
23 TUPLE: rules user-agents allows disallows
24 visit-time request-rate crawl-delay unknowns ;
25
26 <PRIVATE
27
28 : >robots.txt-url ( url -- url' )
29     >url URL" robots.txt" derive-url ;
30
31 : get-robots.txt ( url -- robots.txt )
32     >robots.txt-url http-get nip ;
33
34 : normalize-robots.txt ( string -- sitemaps seq )
35     split-lines
36     [ [ unicode:blank? ] trim ] map
37     [ "#" head? ] reject harvest
38     [ ":" split1 [ [ unicode:blank? ] trim ] bi@ [ >lower ] dip  ] { } map>assoc
39     [ first "sitemap" = ] partition [ values ] dip
40     [
41         {
42             [ [ first "user-agent" = ] both? ]
43             [ nip first "user-agent" = not ]
44         } 2||
45     ] monotonic-split ;
46
47 : <rules> ( -- rules )
48     rules new
49         V{ } clone >>user-agents
50         V{ } clone >>allows
51         V{ } clone >>disallows
52         H{ } clone >>unknowns ;
53
54 : add-user-agent ( rules agent -- rules ) over user-agents>> push ;
55 : add-allow ( rules allow -- rules ) >url over allows>> push ;
56 : add-disallow ( rules disallow -- rules ) >url over disallows>> push ;
57
58 : parse-robots.txt-line ( rules seq -- rules )
59     first2 swap {
60         { "user-agent" [ add-user-agent ] }
61         { "allow" [ add-allow ] }
62         { "disallow" [ add-disallow ] }
63         { "crawl-delay" [ string>number >>crawl-delay ] }
64         { "request-rate" [ string>number >>request-rate ] }
65         {
66             "visit-time" [ "-" split1 [ hhmm>duration ] bi@ 2array
67             >>visit-time
68         ] }
69         [ pick unknowns>> push-at ]
70     } case ;
71
72 : derive-urls ( url seq -- seq' )
73     [ derive-url present ] with { } map-as ;
74
75 : robot-rules-quot ( robots -- quot )
76     [
77         [ site>> ] [ rules>> allows>> ] bi
78         derive-urls [ <glob> ] map
79         <or>
80     ] [
81         [ site>> ] [ rules>> disallows>> ] bi
82         derive-urls [ <glob> ] map <and> <not>
83     ] bi 2array <or> '[ _ matches? ] ;
84
85 : relevant-rules ( robots -- rules )
86     [
87         user-agents>> [
88             robot-identities get [ swap glob-matches? ] with any?
89         ] any?
90     ] filter ;
91
92 PRIVATE>
93
94 : parse-robots.txt ( string -- sitemaps rules-seq )
95     normalize-robots.txt [
96         [ <rules> dup ] dip [ parse-robots.txt-line drop ] with each
97     ] map ;
98
99 : url>robots ( url -- robots )
100     >url dup get-robots.txt parse-robots.txt <robots> ;