File: robotparser2.py

package info (click to toggle)
linkchecker 4.9-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,308 kB
  • ctags: 3,736
  • sloc: python: 21,328; lex: 1,114; yacc: 781; ansic: 551; makefile: 244; sh: 100; sql: 19; awk: 4
file content (558 lines) | stat: -rwxr-xr-x 19,019 bytes parent folder | download
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# -*- coding: iso-8859-1 -*-
# Copyright (C) 2000-2008 Bastian Kleineidam
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"""
Robots.txt parser.

The robots.txt Exclusion Protocol is implemented as specified in
http://www.robotstxt.org/wc/norobots-rfc.html
"""

import urlparse
import httplib
import urllib
import urllib2
import time
import socket
import re
import zlib
import sys
import cStringIO as StringIO
import linkcheck
import configuration
import log
from linkcheck import gzip2 as gzip

__all__ = ["RobotFileParser"]


class PasswordManager (object):
    """
    Simple password manager storing username and password. Suitable
    for use as an AuthHandler instance in urllib2.
    """

    def __init__ (self, user, password):
        """
        Store given username and password.
        """
        self.user = user
        self.password = password

    def add_password (self, realm, uri, user, passwd):
        """
        Does nothing since username and password are already stored.

        @return: None
        """
        pass

    def find_user_password (self, realm, authuri):
        """
        Get stored username and password.

        @return: A tuple (user, password)
        @rtype: tuple
        """
        return self.user, self.password


class RobotFileParser (object):
    """
    This class provides a set of methods to read, parse and answer
    questions about a single robots.txt file.
    """

    def __init__ (self, url='', user=None, password=None):
        """
        Initialize internal entry lists and store given url and
        credentials.
        """
        self.set_url(url)
        self.user = user
        self.password = password
        self._reset()

    def _reset (self):
        """
        Reset internal flags and entry lists.
        """
        self.entries = []
        self.default_entry = None
        self.disallow_all = False
        self.allow_all = False
        self.last_checked = 0

    def mtime (self):
        """
        Returns the time the robots.txt file was last fetched.

        This is useful for long-running web spiders that need to
        check for new robots.txt files periodically.

        @return: last modified in time.time() format
        @rtype: number
        """
        return self.last_checked

    def modified (self):
        """
        Set the time the robots.txt file was last fetched to the
        current time.
        """
        self.last_checked = time.time()

    def set_url (self, url):
        "Set the URL referring to a robots.txt file."
        self.url = url
        self.host, self.path = urlparse.urlparse(url)[1:3]

    def get_opener (self):
        """
        Construct an URL opener object. It considers the given credentials
        from the __init__() method and supports proxies.

        @return: URL opener
        @rtype: urllib2.OpenerDirector
        """
        pwd_manager = PasswordManager(self.user, self.password)
        handlers = [
            urllib2.ProxyHandler(urllib.getproxies()),
            urllib2.UnknownHandler,
            HttpWithGzipHandler,
            urllib2.HTTPBasicAuthHandler(pwd_manager),
            urllib2.ProxyBasicAuthHandler(pwd_manager),
            urllib2.HTTPDigestAuthHandler(pwd_manager),
            urllib2.ProxyDigestAuthHandler(pwd_manager),
            urllib2.HTTPDefaultErrorHandler,
            urllib2.HTTPRedirectHandler,
        ]
        if hasattr(httplib, 'HTTPS'):
            handlers.append(HttpsWithGzipHandler)
        return urllib2.build_opener(*handlers)

    def read (self):
        "Read the robots.txt URL and feeds it to the parser."
        self._reset()
        headers = {
            'User-Agent': configuration.UserAgent,
            'Accept-Encoding' : 'gzip;q=1.0, deflate;q=0.9, identity;q=0.5',
        }
        req = urllib2.Request(self.url, None, headers)
        try:
            self._read_content(req)
        except urllib2.HTTPError, x:
            if x.code in (401, 403):
                self.disallow_all = True
                assert None == log.debug(linkcheck.LOG_CHECK,
                    "%s disallow all", self.url)
            else:
                self.allow_all = True
                assert None == log.debug(linkcheck.LOG_CHECK,
                    "%s allow all", self.url)
        except socket.timeout:
            raise
        except urllib2.URLError:
            x = sys.exc_info()[1]
            if isinstance(x.reason, socket.timeout):
                raise
            self.allow_all = True
            assert None == log.debug(linkcheck.LOG_CHECK,
                "%s allow all", self.url)
        except (socket.gaierror, socket.error):
            # no network
            self.allow_all = True
            assert None == log.debug(linkcheck.LOG_CHECK,
                "%s allow all", self.url)
        except IOError, msg:
            self.allow_all = True
            assert None == log.debug(linkcheck.LOG_CHECK,
                "%s allow all", self.url)
        except httplib.HTTPException:
            self.allow_all = True
            assert None == log.debug(linkcheck.LOG_CHECK,
                "%s allow all", self.url)
        except ValueError:
            # XXX bug workaround:
            # urllib2.AbstractDigestAuthHandler raises ValueError on
            # failed authorisation
            self.disallow_all = True
            assert None == log.debug(linkcheck.LOG_CHECK,
                "%s disallow all", self.url)

    def _read_content (self, req):
        """
        Read robots.txt content.
        @raise: urllib2.HTTPError on HTTP failure codes
        @raise: socket.gaierror, socket.error, urllib2.URLError on network
          errors
        @raise: httplib.HTTPException, IOError on HTTP errors
        @raise: ValueError on bad digest auth (a bug)
        """
        f = self.get_opener().open(req)
        ct = f.info().get("Content-Type")
        if ct and ct.lower().startswith("text/plain"):
            lines = []
            line = f.readline()
            while line:
                lines.append(line.strip().encode("ascii", "ignore"))
                line = f.readline()
            self.parse(lines)
        else:
            self.allow_all = True

    def _add_entry (self, entry):
        """
        Add a parsed entry to entry list.

        @return: None
        """
        if "*" in entry.useragents:
            # the default entry is considered last
            self.default_entry = entry
        else:
            self.entries.append(entry)

    def parse (self, lines):
        """
        Parse the input lines from a robot.txt file.
        We allow that a user-agent: line is not preceded by
        one or more blank lines.

        @return: None
        """
        assert None == log.debug(linkcheck.LOG_CHECK,
            "%s parse lines", self.url)
        state = 0
        linenumber = 0
        entry = Entry()

        for line in lines:
            linenumber += 1
            if not line:
                if state == 1:
                    assert None == log.debug(linkcheck.LOG_CHECK,
                         "%s line %d: allow or disallow directives without" \
                         " any user-agent line", self.url, linenumber)
                    entry = Entry()
                    state = 0
                elif state == 2:
                    self._add_entry(entry)
                    entry = Entry()
                    state = 0
            # remove optional comment and strip line
            i = line.find('#')
            if i >= 0:
                line = line[:i]
            line = line.strip()
            if not line:
                continue
            line = line.split(':', 1)
            if len(line) == 2:
                line[0] = line[0].strip().lower()
                line[1] = urllib.unquote(line[1].strip())
                if line[0] == "user-agent":
                    if state == 2:
                        assert None == log.debug(linkcheck.LOG_CHECK,
                          "%s line %d: missing blank line before user-agent" \
                          " directive", self.url, linenumber)
                        self._add_entry(entry)
                        entry = Entry()
                    entry.useragents.append(line[1])
                    state = 1
                elif line[0] == "disallow":
                    if state == 0:
                        assert None == log.debug(linkcheck.LOG_CHECK,
                          "%s line %d: missing user-agent directive before" \
                          " this line", self.url, linenumber)
                    else:
                        entry.rulelines.append(RuleLine(line[1], False))
                        state = 2
                elif line[0] == "allow":
                    if state == 0:
                        assert None == log.debug(linkcheck.LOG_CHECK,
                          "%s line %d: missing user-agent directive before" \
                          " this line", self.url, linenumber)
                    else:
                        entry.rulelines.append(RuleLine(line[1], True))
                        state = 2
                elif line[0] == "crawl-delay":
                    if state == 0:
                        assert None == log.debug(linkcheck.LOG_CHECK,
                          "%s line %d: missing user-agent directive before" \
                          " this line", self.url, linenumber)
                    else:
                        try:
                            entry.crawldelay = max(0, int(line[1]))
                            state = 2
                        except ValueError:
                            assert None == log.debug(linkcheck.LOG_CHECK,
                             "%s line %d: invalid delay number %r",
                             self.url, linenumber, line[1])
                            pass
                else:
                    assert None == log.debug(linkcheck.LOG_CHECK,
                             "%s line %d: unknown key %s",
                             self.url, linenumber, line[0])
            else:
                assert None == log.debug(linkcheck.LOG_CHECK,
                    "%s line %d: malformed line %s",
                    self.url, linenumber, line)
        if state in (1, 2):
            self.entries.append(entry)
        self.modified()
        assert None == log.debug(linkcheck.LOG_CHECK,
                            "Parsed rules:\n%s", str(self))

    def can_fetch (self, useragent, url):
        """
        Using the parsed robots.txt decide if useragent can fetch url.

        @return: True if agent can fetch url, else False
        @rtype: bool
        """
        assert None == log.debug(linkcheck.LOG_CHECK,
              "%s check allowance for:\n" \
              "  user agent: %r\n  url: %r", self.url, useragent, url)
        if not isinstance(useragent, str):
            useragent = useragent.encode("ascii", "ignore")
        if not isinstance(url, str):
            url = url.encode("ascii", "ignore")
        if self.disallow_all:
            return False
        if self.allow_all:
            return True
        # search for given user agent matches
        # the first match counts
        url = urllib.quote(urlparse.urlparse(urllib.unquote(url))[2]) or "/"
        for entry in self.entries:
            if entry.applies_to(useragent):
                return entry.allowance(url)
        # try the default entry last
        if self.default_entry is not None:
            return self.default_entry.allowance(url)
        # agent not found ==> access granted
        return True

    def get_crawldelay (self, useragent):
        """
        Look for a configured crawl delay.

        @return: crawl delay in seconds or zero
        @rtype: integer >= 0
        """
        for entry in self.entries:
            if entry.applies_to(useragent):
                return entry.crawldelay
        return 0

    def __str__ (self):
        """
        Constructs string representation, usable as contents of a
        robots.txt file.

        @return: robots.txt format
        @rtype: string
        """
        lines = [str(entry) for entry in self.entries]
        if self.default_entry is not None:
            lines.append(str(self.default_entry))
        return "\n\n".join(lines)


class RuleLine (object):
    """
    A rule line is a single "Allow:" (allowance==1) or "Disallow:"
    (allowance==0) followed by a path.
    """

    def __init__ (self, path, allowance):
        """
        Initialize with given path and allowance info.
        """
        if path == '' and not allowance:
            # an empty value means allow all
            allowance = True
            path = '/'
        self.path = urllib.quote(path)
        self.allowance = allowance

    def applies_to (self, path):
        """
        Look if given path applies to this rule.

        @return: True if pathname applies to this rule, else False
        @rtype: bool
        """
        return self.path == "*" or path.startswith(self.path)

    def __str__ (self):
        """
        Construct string representation in robots.txt format.

        @return: robots.txt format
        @rtype: string
        """
        return (self.allowance and "Allow" or "Disallow")+": "+self.path


class Entry (object):
    """
    An entry has one or more user-agents and zero or more rulelines.
    """

    def __init__ (self):
        """
        Initialize user agent and rule list.
        """
        self.useragents = []
        self.rulelines = []
        self.crawldelay = 0

    def __str__ (self):
        """
        string representation in robots.txt format.

        @return: robots.txt format
        @rtype: string
        """
        lines = ["User-agent: %s" % agent for agent in self.useragents]
        if self.crawldelay:
            lines.append("Crawl-delay: %d" % self.crawldelay)
        lines.extend([str(line) for line in self.rulelines])
        return "\n".join(lines)

    def applies_to (self, useragent):
        """
        Check if this entry applies to the specified agent.

        @return: True if this entry applies to the agent, else False.
        @rtype: bool
        """
        if not useragent:
            return True
        # split the name token and make it lower case unicode
        useragent = useragent.split("/")[0].lower()
        for agent in self.useragents:
            if agent == '*':
                # we have the catch-all agent
                return True
            agent = agent.lower()
            if useragent in agent:
                return True
        return False

    def allowance (self, path):
        """
        Preconditions:
        - our agent applies to this entry
        - filename is URL decoded

        Check if given filename is allowed to acces this entry.

        @return: True if allowed, else False
        @rtype: bool
        """
        for line in self.rulelines:
            assert None == log.debug(linkcheck.LOG_CHECK,
               "%s %s %s", path, str(line), line.allowance)
            if line.applies_to(path):
                return line.allowance
        return True

###########################################################################
# urlutils.py - Simplified urllib handling
#
#   Written by Chris Lawrence <lawrencc@debian.org>
#   (C) 1999-2002 Chris Lawrence
#
# This program is freely distributable per the following license:
#
##  Permission to use, copy, modify, and distribute this software and its
##  documentation for any purpose and without fee is hereby granted,
##  provided that the above copyright notice appears in all copies and that
##  both that copyright notice and this permission notice appear in
##  supporting documentation.
##
##  I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
##  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I
##  BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
##  DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
##  WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
##  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
##  SOFTWARE.
def decode (page):
    """
    Gunzip or deflate a compressed page.
    """
    assert None == log.debug(linkcheck.LOG_CHECK,
      "robots.txt page info %d %s", page.code, str(page.info()))
    encoding = page.info().get("Content-Encoding")
    if encoding in ('gzip', 'x-gzip', 'deflate'):
        # cannot seek in socket descriptors, so must get content now
        content = page.read()
        try:
            if encoding == 'deflate':
                fp = StringIO.StringIO(zlib.decompress(content))
            else:
                fp = gzip.GzipFile('', 'rb', 9, StringIO.StringIO(content))
        except zlib.error, msg:
            assert None == log.debug(linkcheck.LOG_CHECK,
                 "uncompressing had error "
                 "%s, assuming non-compressed content", str(msg))
            fp = StringIO.StringIO(content)
        # remove content-encoding header
        headers = httplib.HTTPMessage(StringIO.StringIO(""))
        ceheader = re.compile(r"(?i)content-encoding:")
        for h in page.info().keys():
            if not ceheader.match(h):
                headers[h] = page.info()[h]
        newpage = urllib.addinfourl(fp, headers, page.geturl())
        if hasattr(page, "code"):
            # python 2.4 compatibility
            newpage.code = page.code
        if hasattr(page, "msg"):
            # python 2.4 compatibility
            newpage.msg = page.msg
        page = newpage
    return page


class HttpWithGzipHandler (urllib2.HTTPHandler):
    """
    Support gzip encoding.
    """
    def http_open (self, req):
        """
        Send request and decode answer.
        """
        return decode(urllib2.HTTPHandler.http_open(self, req))


if hasattr(httplib, 'HTTPS'):
    class HttpsWithGzipHandler (urllib2.HTTPSHandler):
        """
        Support gzip encoding.
        """

        def http_open (self, req):
            """
            Send request and decode answer.
            """
            return decode(urllib2.HTTPSHandler.http_open(self, req))

# end of urlutils.py routines
###########################################################################