Source

psilib / spider.py

   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
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
#! /usr/bin/env python

## Copyright (c) 1999 - 2003 L. C. Rees.  All rights reserved.
## See COPYRIGHT file for license terms.

__name__ = 'spider'
__version__ = '0.5'
__author__ = 'L.C. Rees (xanimal@users.sf.net)'
__all__ = ['ftpurls', 'ftppaths', 'weburls', 'ftpmirror', 'ftpspider',
    'webpaths', 'webreport', 'webmirror', 'webspider', 'urlreport',
    'badurlreport', 'badhtmreport', 'redireport', 'outreport', 'othereport']

'''Multithreaded crawling, reporting, and mirroring for Web and FTP.'''

from __future__ import generators


class Spider:

    '''HTTP and FTP crawling, reporting, and checking'''
    
    import os as _os    
    import urllib as _ulib
    import asyncore as _async
    import urlparse as _uparse    
    from os import path as _path 
    from ftplib import FTP as _ftp
    import mimetools as _mimetools
    import traceback as _traceback
    from time import time as _time    
    from socket import AF_INET as _afinet
    from time import strftime as _formtime
    from time import localtime as _localtime
    from ftplib import error_perm as _ftperr
    from cStringIO import StringIO as _stringio
    from socket import SOCK_STREAM as _sockstream
    from sgmllib import SGMLParseError as _sperror    
    from robotparser import RobotFileParser as _rparser    
    # Use threads if available 
    try: from threading import Thread as _thread
    except ImportError: pass
    
    _bdsig, _bfsig, _session, _newparser = None, None, None, None
    _bqsig = None
    # HTML tags with URLs
    _urltags = {'a':1, 'img':1, 'link':1, 'script':1, 'iframe':1, 'object':1,
        'embed':1, 'area':1, 'frame':1, 'applet':1, 'input':1, 'base':1,
        'div':1, 'layer':1, 'ilayer':1, 'bgsound':1}
    # Supported protocols
    _supported = {'HTTP':1, 'http':1, 'HTTPS':1, 'https':1, 'FTP':1, 'ftp':1}
    # HTML attributes with URLs
    _urlattrs = {'href':1, 'src':1, 'data':1}
    # HTTP redirect codes
    _rcodes = {'300':1, '301':1, '302':1, '303':1, '304':1, '305':1, '306':1}
    # Good HTTP codes
    _okcodes = {'100':1, '101':1, '200':1, '201':1, '202':1, '203':1, '204':1,
        '205':1, '206':1}

    def __init__(self, base=None, width=None, depth=None):
        '''Initializes a Spider instance and its base attributes

        Arguments:
        base -- URL to crawl (default: None)
        width -- maximum resources to crawl (default: None)
        depth -- how deep in a hierarchy to crawl (default: None)'''
        if base: self.base = base
        else: self.base = None
        if width: self.width = width
        else: self.width = None
        if depth: self.depth = depth
        else: self.depth = None

    def _defasync(self):        
        urlparse, stringio = self._uparse.urlparse, self._stringio
        sockstream, afinet = self._sockstream, self._afinet
        rcodes, okcodes = self._rcodes, self._okcodes
        badurl, badhtm = self._badurl, self._badhtm
        sperror, visited = self._sperror, self._visited
        badhtms, async, traceback = self.badhtms, self._async, self._traceback
        badurls = self.badurls
        mimetools, time = self._mimetools, self._time
        
        class AsyncHttp(async.dispatcher_with_send):
            
            def __init__(self, url, consumer, base=None, old=None, new=None):
                async.dispatcher_with_send.__init__(self)
                if base: self.base, consumer.base = base, base
                else: self.base = None
                if old: self.old, consumer.old = old, old
                else: self.old = None
                if new: self.new = new
                else: self.new = url
                consumer.url, consumer.new = url, self.new
                scheme, host, path, params, query, fragment = urlparse(url)
                assert scheme == 'http', 'only supports HTTP requests'
                if not path: path = '/'
                if params: path = ';'.join([path, params])
                if query: path = '?'.join([path, query])
                self.url, self.consumer, self.path = url, consumer, path
                self.host, self.port, self.status = host, 80, None                
                self.header, self.data, self.timestamp = None, '', time()
                self.bytes_in, self.bytes_out = 0, 0
                self.create_socket(afinet, sockstream)
                try: self.connect((self.host, self.port))
                except:
                    self.close()
                    traceback.print_exc()
                    
            def handle_connect(self):
                request = 'GET %s HTTP/1.0\r\nHost: %s\r\n\r\n'
                request = request % (self.path, self.host)
                self.send(request)
                self.bytes_out = self.bytes_out + len(request)

            def handle_error(self):
                self.close()
                traceback.print_exc()

            def handle_expt(self):
                self.close()
                try: http_header = self.consumer.http_header
                except AttributeError: pass
                else: http_header(self)

            def handle_read(self):
                data = self.recv(2048)
                self.bytes_in = self.bytes_in + len(data)
                if not self.header:
                    self.data = ''.join([self.data, data])                    
                    header = self.data.split('\r\n\r\n', 1)
                    if len(header) <= 1: return
                    header, data = header
                    fp = stringio(header)
                    self.status = fp.readline().split(' ', 2)
                    self.header = mimetools.Message(fp)
                    self.type = self.header['Content-Type']
                    self.consumer.type, self.data = self.type, ''  
                    try: http_header = self.consumer.http_header
                    except AttributeError: pass
                    else: http_header(self)
                    if not self.connected: return
                try:
                    if data: self.consumer.feed(data)
                except sperror:
                    self.close()
                    visited[self.url] = 1
                    if badhtm: badhtms[self.url] = 1
                
            def handle_close(self):
                self.consumer.close()
                self.close()

        class Manager:

            max_con = 20
            max_size = 100000000
            max_time = 40

            def __init__(self):
                self.queue = []

            def add(self, new, consumer, base, old):
                self.queue.append((new, consumer, base, old))

            def poll(self, timeout=0.1):
                while self.queue:
                    if len(self.queue) > self.max_con:
                        while len(async.socket_map) < self.max_con:                        
                            AsyncHttp(*self.queue.pop(0))
                    else:
                        while self.queue: AsyncHttp(*self.queue.pop(0))
                    now, count, length = time(), 0, 0
                    while async.socket_map:
                        if len(async.socket_map) == length:
                            count += 1
                            print 1, count
                            if count == 500:
                                a, b = async.socket_map.popitem()
                                b.close()
                                count = 0
                        else: count = 0
                        for channel in async.socket_map.values():
                            if channel.bytes_in > self.max_size:
                                channel.close()
                            if now - channel.timestamp > self.max_time:
                                channel.close()
                        async.poll(timeout)                       
                        length = len(async.socket_map)
                    
        class Redir:
            
            def __init__(self, consumer, stash):
                self.consumer, self.url, self.type = consumer, None, None
                self.badurl, self.old, self.base = None, None, None
                self.new, self.stash = None, stash
                
            def http_header(self, request):
                if request.status is None or request.status[1] not in rcodes:
                    try: http_header = self.consumer.http_header
                    except AttributeError: pass
                    else: return http_header(request)
                    if request.status[1] not in okcodes:
                        self.consumer.badurl = 1
                else:
                    if 'location' in request.header:
                        url = request.header['location']
                    elif 'uri' in request.header:
                        url = request.header['uri']
                    request.close()
                    AsyncHttp(url, self, self.base, self.old, self.url)

        class ExRedir(Redir):

            def feed(self, data):
                if 'text/html' in self.type: self.consumer.feed(data)

            def close(self):
                if self.consumer.badurl:
                    visited[self.url] = 1
                    if badurl:
                        badurls.append((self.base, self.old, self.url))
                elif self.consumer.urls:
                    self.stash[(self.new, self.url)] = self.consumer.urls
                else: self.stash[(self.new, self.url)] = []
                self.consumer.close()

        class BadRedir(Redir):

            def feed(self, data):
                self.consumer.feed(data)

            def close(self):
                self.stash.append(self.consumer.text)
                self.consumer.close()

        self._BadRedir, self._ExRedir = BadRedir, ExRedir
        self._AsyncHttp, self._Manager = AsyncHttp, Manager

    def _ftpopen(self, base, name='anonymous', password=None, attempts=3):
        '''Returns FTP client session

        Arguments:
        base -- FTP server URL
        name -- login name (default: 'anonymous')
        password -- login password (default: None)
        attempts -- number of login attempts to try (default: 3)'''

        def ftpprompt(tries=0):
            '''Prompts for FTP username and password

            Arguments:
            tries -- number of login attempts'''
            tries += tries
            try:
                self._name = raw_input('Enter login name: ')
                self._password = raw_input('Enter password: ')
                session = ftp(base, self._name, self._password)
                return session
            # If login attempt fails, retry login
            except ftperr:               
                if attempts >= tries:
                    session = ftpprompt(tries)
                    return session
                # Too many login attempts? End program
                elif attempts <= tries:
                    raise IOError, 'Permission denied.'
                    import sys
                    sys.exit(0)

        # Assignments
        self._name, self._password, ftperr = name, password, self._ftperr
        su, ftp = self._uparse.urlsplit(base), self._ftp
        # Set URL, path, and strip 'ftp://' off
        base, path = su[1], '/'.join([su[2], ''])
        try: session = ftp(base, name, password)
        # Prompt for username, password if initial arguments are incorrect
        except ftperr: session = ftpprompt()
        # Change to remote path if it exits
        if path: session.cwd(path)
        return session

    def ftpmirror(self, l, t=None, b=None, w=200, d=6, n='anonymous', p=None):
        '''Mirrors an FTP site on a local filesystem

        Arguments:
        l -- local filesystem path (default: None)
        b -- FTP server URL (default: None)
        t -- number of download threads (default: None)
        w -- maximum amount of resources to crawl (default: 200)
        d -- depth in hierarchy to crawl (default: 6)
        n -- login username (default: 'anonymous')
        p -- login password (default: None)'''
        if b: self.ftpspider(b, w, d, n, p)
        return self._mirror((self.paths, self.urls), l, t)

    def ftppaths(self, b=None, w=200, d=6, n='anonymous', p=None):
        '''Returns a list of FTP paths.

        Arguments:
        b -- FTP server URL (default: None)
        w -- maximum amount of resources to crawl (default: 200) 
        d -- depth in hierarchy to crawl (default: 6)
        n -- login username (default: 'anonymous')
        p -- login password (default: None)'''

        def sortftp(rdir):
            '''Returns a list of entries marked as files or directories

            Arguments:
            rdir -- remote directory list'''
            rlist = []
            rappend = rlist.append
            for rl in rdir:
                # Split remote file based on whitespace
                ri = rl.split()[-1]
                # Add tuple of remote item type, permissions & name to rlist
                if ri not in ('.', '..'): rappend((rl[0], rl[7], ri))
            return rlist

        def visitftp():
            '''Extracts contents of an FTP directory'''
            wd = pwd()
            if wd[-1] != '/': wd = '/'.join([wd, ''])
            # Add present working directory to visited directories
            dirs[wd], rlist = None, []
            # Get list of current directory's contents
            retr('LIST -a', rlist.append)
            for url in sortftp(rlist):
                # Test if remote item is a file (indicated by '-')
                if url[0] == '-':
                    # Resolve path of file
                    purl = ''.join([wd, url[2]])
                    # Ensure file list don't exceed max number of resources
                    if len(files) >= width: return None
                    # Add files to file dictionary
                    elif purl not in files: files[purl] = None
                # Test if it's a directory ('d') and allows scanning ('-')
                elif url[0] == 'd':
                    if url[1] != '-':
                        # Resolve path of directory
                        purl = ''.join([wd, url[2], '/'])
                        # Ensure no recursion beyond depth allowed
                        if len(purl.split('/')) >= depth: dirs[purl] = None
                        # Visit directory if it hasn't been visited yet 
                        elif purl not in dirs:
                            # Change to new directory
                            cwd(purl)
                            # Run 'visitftp' on new directory
                            visitftp()

        # Use classwide attributes if set
        if b: self.base = b
        else: b = self.base
        # Use classwide width if different from method default
        if self.width and w == 200: width = self.width
        else: width = w
        # Use classwide depth if different from method default
        if self.depth and d == 6: depth = self.depth + 1
        else: depth = d + 1
        # File and directory dicts
        files, dirs = {}, {}
        # Use existing FTP client session if present
        if self._session: ftp = self._session
        # Create new FTP client session if necessary
        else:
            ftp = self._ftpopen(b, n, p)
            self._session = ftp
        # Avoid outside namespace lookups
        cwd, pwd, retr = ftp.cwd, ftp.pwd, ftp.retrlines
        # Walk FTP site
        visitftp()
        # Make path list out of files' keys and return it
        self.paths = files.keys()
        self.paths.sort()
        return self.paths

    def ftpspider(self, b=None, w=200, d=6, n='anonymous', p=None):
        '''Returns lists of URLs and paths plus a live FTP client session

        Arguments:
        b -- FTP server URL (default: None)
        w -- maximum amount of resources to crawl (default: 200) 
        d -- depth in hierarchy to crawl (default: 6)
        n -- login username (default: 'anonymous')
        p -- login password (default: None)'''
        if b: ftppaths(b, w, d, n, p)
        return self.paths, ftpurls(), self._session

    def ftpurls(self, b=None, w=200, d=6, n='anonymous', p=None):
        '''Returns a list of FTP URLs

        Arguments:
        b -- FTP server URL (default: None)
        w -- maximum amount of resources to crawl (default: 200) 
        d -- depth in hierarchy to crawl (default: 6)
        n -- login username (default: 'anonymous')
        p -- login password (default: None)'''
        if b:
            ftppaths(b, w, d, n, p)
            # Get rid of trailing '/' in base if present before joining
            if b[-1] == '/': base = b[:-1]
        else:
            base = self.base
            # Get rid of trailing '/' in base if present before joining
            if base[-1] == '/': base = self.base[:-1]
        paths = self.paths
        # Add FTP URL
        self.urls = [''.join([base, i]) for i in paths]
        return self.urls

    def _parserpick(self, old=None):
        '''Returns a class using the sgmllib parser or the sgmlop parser

        Arguments:
        old -- use classic sgmllib SGMLParser'''
        # Assignments
        urltags, urlattrs = self._urltags, self._urlattrs
        # Lists for bad file and bad directory signatures
        self._bfsig, self._bdsig, self._bqsig = [], [], []
        bfsig, bdsig, bqsig = self._bfsig, self._bdsig, self._bqsig
        # Use faster SGMLParser if available
        try:
            from sgmlop import SGMLParser as newparser
            self._newparser = newparser
        # If unavailable, use classic SGML parser
        except ImportError:
            from sgmllib import SGMLParser as oldparser
            old = 1
        # Classes using classic sgmllib SGML Parser
        if old:
            from sgmllib import SGMLParser as oldparser
            # Remove sgmlop parser if present
            self._newparser = None
            # UrlExtract class using classic parser
            class UrlExtract(oldparser):
                '''Extracts URLs from a SGMLish document'''
                def reset(self):
                    '''Resets SGML parser and clears lists'''
                    oldparser.reset(self)
                    self.urls, self.text, self.badurl = [], [], None
                def handle_data(self, data):
                    '''Handles non-markup data'''
                    # Get first 5 lines of non-markup data
                    if len(self.text) <= 5: self.text.append(data)
                    # Compare signature of known bad URL to a new web page
                    if self.text == bfsig: self.badurl = 1
                    elif self.text == bdsig: self.badurl = 1
                    elif self.text == bqsig: self.badurl = 1
                def finish_starttag(self, tag, attrs):
                    '''Extracts URL bearing tags'''
                    if tag in urltags:
                        # Get key, vale in attributes if they match
                        url = [v for k, v in attrs if k in urlattrs]                        
                        if url: self.urls.extend(url)
            # BadUrl class using classic parser
            class BadUrl(oldparser):
                '''Collects results of intentionally incorrect URLs'''
                def reset(self):
                    '''Resets SGML parser and clears lists'''
                    oldparser.reset(self)
                    self.text = []
                def handle_data(self, data):
                    '''Collects lines to profile bad URLs'''
                    # Adds first 5 lines of non-markup data to text
                    if len(self.text) <= 5: self.text.append(data)
        # If no old flag, use SGMLParser from sgmlop and related classes
        else:
            # UrlExtract class using sgmlop parser
            class UrlExtract:
                '''Extracts URLs from a SGMLish document'''
                def __init__(self):
                    '''Resets SGML parser and clears lists'''
                    self.urls, self.text, self.badurl = [], [], None
                def handle_data(self, data):
                    '''Handles non-markup data'''
                    # Get first 5 lines of non-markup data
                    if len(self.text) <= 5: self.text.append(data)
                    # Compare signature of known bad URL to a new web page
                    if self.text == bfsig: self.badurl = 1
                    elif self.text == bdsig: self.badurl = 1
                    elif self.text == bqsig: self.badurl = 1
                def finish_starttag(self, tag, attrs):
                    '''Extracts URL bearing tags'''
                    if tag in urltags:
                        # Get key, vale in attributes if they match
                        url = [v for k, v in attrs if k in urlattrs]
                        if url: self.urls.extend(url)
            # BadUrl class using sgmlop parser
            class BadUrl:
                '''Collects results of intentionally incorrect URLs'''
                def __init__(self):
                    '''Resets SGML parser and clears lists'''
                    self.text = []
                def handle_data(self, data):
                    '''Collects lines to profile not found responses'''
                    # Adds first 5 lines of non-markup data to list 'text'
                    if len(self.text) <= 5: self.text.append(data)
        # Make resulting classes available class wide
        self._UrlExtract, self._BadUrl = UrlExtract, BadUrl

    def _webtest2(self):
        '''Generates signatures for identifying bad URLs'''
        # Assignments
        base = self.base
        BadUrl, BadRedir = self._BadUrl, self._BadRedir
        urljoin, AsyncHttp = self._uparse.urljoin, self._AsyncHttp
        stash, async, time = [], self._async, self._time
        # Generate random string of jibber
        from string import letters, digits
        from random import choice, randint
        jibber = ''.join([letters, digits])
        ru = ''.join([choice(jibber) for x in range(randint(1, 30))])
        # Builds signature of a bad URL for a file
        AsyncHttp(urljoin(base, '%s.html' % ru), BadRedir(BadUrl(), stash))
        # Builds signature of a bad URL for a directory
        AsyncHttp(urljoin(base, '%s/' % ru), BadRedir(BadUrl(), stash))
        # Builds signature of a bad URL for a query
        AsyncHttp(urljoin(base, '%s?%s' % (ru, ru)), BadRedir(BadUrl(), stash))
        max_time = 40
        now = time()
        while async.socket_map:                        
            for channel in async.socket_map.values():
                if now - channel.timestamp > max_time: channel.close()
            async.poll(0.01)
        # Store signatures
        self._bfsig.extend(stash[0])
        self._bdsig.extend(stash[1])
        self._bqsig.extend(stash[2])

    def _webtest(self):
        '''Generates signatures for identifying bad URLs'''

        def badurl(url):
            '''Returns first 5 lines of a bad URL

            Arguments:
            url -- Bad URL to open and parse'''
            # Use different classes if faster SGML Parser is available
            if self._newparser:
                # sgmlop parser must have a handler passed to it
                parser, urlget = self._newparser(), BadUrl()
                # Pass handler (sgmlop cannot be subclassed)
                parser.register(urlget)
                parser.feed(urlopen(url).read())
                parser.close()
            # Use classic parser
            else:
                urlget = BadUrl()
                urlget.feed(urlopen(url).read())
                urlget.close()
            # Return singature of bad URL
            return urlget.text

        # Make globals local
        base, urljoin = self.base, self._uparse.urljoin
        urlopen, BadUrl = self._ulib.urlopen, self._BadUrl
        # Generate random string of jibber
        from string import letters, digits
        from random import choice, randint
        jibber = ''.join([letters, digits])
        ru = ''.join([choice(jibber) for x in range(randint(1, 30))])
        # Builds signature of a bad URL for a file
        self._bfsig.extend(badurl(urljoin(base, '%s.html' % ru)))
        # Builds signature of a bad URL for a directory
        self._bdsig.extend(badurl(urljoin(base,'%s/' % ru)))
        # Builds signature of a bad URL for a query
        self._bqsig.extend(badurl(urljoin(base, '%s?%s' % (ru, ru))))

    def _webparser(self, html):
        '''Parses HTML and returns bad URL indicator and extracted URLs

        Arguments:
        html -- HTML data'''
        # Use different classes if faster SGML Parser is available
        if self._newparser:
            # Make instances of SGML parser and URL extracting handler
            parser, urlget = self._newparser(), self._UrlExtract()
            # Pass handler to parser
            parser.register(urlget)
            # Feed data to parser
            parser.feed(html)
            parser.close()
            # Return bad URL indicator and extracted URLs
        else:
            urlget = self._UrlExtract()
            urlget.feed(html)
            urlget.close()
        # Return badurl marker and list of child URLS
        return urlget.badurl, urlget.urls

    def _webopen(self, new, base, old):
        '''Returns real URL and extracted child URLs

        Arguments:
        new -- newly resolved URL
        base -- referring URL
        old - original URL'''
        try:
            # If webspiders can access URL, open it
            if self._robot.can_fetch('*', new):
                url = self._ulib.urlopen(new)
            # Otherwise, mark as visited and abort
            else:
                self._visited[new] = 1
                return 0, 0
        # If HTTP error, log bad URL and abort
        except IOError:
            self._visited[new] = 1
            if self._badurl: self.badurls.append((base, old, new))
            return 0, 0
        # Get real URL
        new = url.geturl()
        # URLs with mimetype 'text/html" scanned for URLs
        if url.headers.type == 'text/html':
            # Feed parser
            contents = url.read()
            try: badurl, urls = self._webparser(contents)
            # Log URL if SGML parser can't parse it 
            except self._sperror:
                self._visited[new] = 1
                if self._badhtm: self.badhtms[new] = 1
                return 0, 0
            url.close()
            # Return URL and extracted urls if it's good
            if not badurl: return new, urls
            # If the URL is bad (after BadUrl), stop processing and log URL
            else:
                self._visited[new] = 1
                if self._badurl: self.badurls.append((base, old, new))
                return 0, 0
        # Return URL of non-HTML resources and empty list
        else:
            url.close()
            return new, []

    def _genverify2(self, urllist, base):
        '''Returns a full URL relative to a base URL

        Arguments:
        urls -- list of raw URLs
        base -- referring URL'''
        # Assignments
        visited, webopen, others = self._visited, self._webopen, self.others
        urlsplit, urldefrag = self._uparse.urlsplit, self._uparse.urldefrag
        sb, depth, urljoin = self._sb[2], self.depth, self._uparse.urljoin        
        outs, redirs, supported = self.outs, self.redirs, self._supported
        redir, other, out = self._redir, self._other, self._out
        cache, toprocess, robot = self._cache, [], self._robot
        ExRedir = self._ExRedir
        UrlExtract, stash, manager = self._UrlExtract, {}, self._Manager()
        # Strip file off base URL for joining
        try: newbase = base.replace(base.split('/')[-1], '')
        except ValueError: newbase = base
        # Handle any child URLs
        for url in urllist:
            if url not in visited:
                # Remove whitespace from URL
                if url.find(' ') != -1:
                    visited[url], new = 1, url.replace(' ', '')
                    if url in visited: continue
                # Remove fragments i.e. 'http:foo/bar#frag'
                if url.find('#') != -1:
                    visited[url], new = 1, urldefrag(url)[0]
                    if url in visited: continue
                # Process full URLs i.e. 'http://foo/bar
                if url.find(':') != -1:
                    urlseg = urlsplit(url)
                    # Block non-FTP, HTTP URLs
                    if urlseg[0] not in supported:
                        visited[url] = 1
                        if other: others[url] = 1
                        continue
                    # If URL is not in root domain, block it
                    elif urlseg[1] not in sb:
                        visited[url] = 1
                        if out: outs[url] = base
                        continue
                    # Block duplicate root URLs
                    elif not urlseg[2] and urlseg[1] == sb:
                        visited[url] = 1
                        continue
                    new = url
                # Handle relative URLs i.e. ../foo/bar
                elif url.find(':') == -1:
                    # Join root domain and relative URL
                    visited[url], new = 1, urljoin(newbase, url)
                    if new in visited: continue
                toprocess.append((new, base, url))
        for new, base, url in toprocess:
            if robot.can_fetch('*', new):
                consumer = ExRedir(UrlExtract(), stash)
                manager.add(new, consumer, base, url)
            else: visited[new] = 1
        manager.poll()
        for turl, new in stash:
            if turl not in visited:
                visited[new], visited[turl]  = 1, 1
                # If URL resolved to a different URL, process it
                if turl != new:
                    urlseg = urlsplit(turl)
                    # If URL is not in root domain, block it
                    if urlseg[1] not in sb:
                        # Log as a redirected internal URL
                        if redir: redirs[(new, turl)] = base
                        continue
                    # Block duplicate root URLs
                    elif not urlseg[2] and urlseg[1] == sb: continue
                # If URL exceeds depth, don't process 
                if len(turl.split('/')) >= depth: continue
                # Otherwise return URL
                else:
                    newurls, rawurls = {}, stash.get((turl, new))
                    if rawurls:
                        for rawurl in rawurls:
                            if rawurl not in visited: newurls[rawurl] = 1
                        cache[turl] = newurls
                    yield turl, base

    def _urlverify(self, url, base, newbase):
        '''Returns a full URL relative to a base URL

        Arguments:
        urls -- list of raw URLs
        base -- referring URL
        newbase -- temporary version of referring URL for joining'''
        # Assignments
        visited, webopen, others = self._visited, self._webopen, self.others
        urlsplit, urldefrag = self._uparse.urlsplit, self._uparse.urldefrag
        sb, depth, urljoin = self._sb[2], self.depth, self._uparse.urljoin        
        outs, redirs, supported = self.outs, self.redirs, self._supported
        redir, other, out = self._redir, self._other, self._out
        if url not in visited:
            # Remove whitespace from URL
            if url.find(' ') != -1:
                visited[url], new = 1, url.replace(' ', '')
                if url in visited: return 0, 0
            # Remove fragments i.e. 'http:foo/bar#frag'
            if url.find('#') != -1:
                visited[url], new = 1, urldefrag(url)[0]
                if url in visited: return 0, 0
            # Process full URLs i.e. 'http://foo/bar
            if url.find(':') != -1:
                urlseg = urlsplit(url)
                # Block non-FTP, HTTP URLs
                if urlseg[0] not in supported:
                    visited[url] = 1
                    if other: others[url] = 1
                    return 0, 0
                # If URL is not in root domain, block it
                elif urlseg[1] not in sb:
                    visited[url] = 1
                    if out: outs[url] = base
                    return 0, 0
                # Block duplicate root URLs
                elif not urlseg[2] and urlseg[1] == sb:
                    visited[url] = 1
                    return 0, 0
                new = url
            # Handle relative URLs i.e. ../foo/bar
            elif url.find(':') == -1:
                # Join root domain and relative URL
                visited[url], new = 1, urljoin(newbase, url)
                if new in visited: return 0, 0
            # Test URL by attempting to open it
            turl, rawurls = webopen(new, base, url)
            if turl and turl not in visited:
                visited[new], visited[turl]  = 1, 1
                # If URL resolved to a different URL, process it
                if turl != new:
                    urlseg = urlsplit(turl)
                    # If URL is not in root domain, block it
                    if urlseg[1] not in sb:
                        # Log as a redirected internal URL
                        if redir: redirs[(new, turl)] = base
                        return 0, 0
                    # Block duplicate root URLs
                    elif not urlseg[2] and urlseg[1] == sb: return 0, 0
                # If URL exceeds depth, don't process 
                if len(turl.split('/')) >= depth: return 0, 0
                # Otherwise return URL
                else:
                    if rawurls: return turl, rawurls
                    else: return turl, []
            else: return 0,0
        else: return 0, 0

    def _genverify(self, urls, base):
        '''Verifies a list of full URL relative to a base URL

        Arguments:
        urls -- list of raw URLs
        base -- referring URL'''
        # Assignments
        cache, visit, urlverify = self._cache, self._visited, self._urlverify
        # Strip file off base URL for joining
        newbase = base.replace(base.split('/')[-1], '') 
        for url in urls:
            # Get resolved url and raw child URLs
            url, rawurls = urlverify(url, base, newbase)
            # Handle any child URLs
            if rawurls:
                newurls = {}
                # Eliminate duplicate URLs
                for rawurl in rawurls:
                    # Eliminate known visited URLs
                    if rawurl not in visit: newurls[rawurl] = 1
                # Put new URLs in cache if present
                if newurls: cache[url] = newurls
            # Yield new URL
            if url: yield url, base

    def _multiverify(self, url, base):
        '''Verifies a full URL relative to a base URL

        Arguments:
        url -- a raw URLs
        base -- referring URL'''
        # Assignments
        cache, visited = self._cache, self._visited
        # Strip file off base URL for joining
        newbase = base.replace(base.split('/')[-1], '')
        # Get resolved url and raw child URLs
        url, rawurls = self._urlverify(url, base, newbase)
        # Handle any child URLs
        if rawurls:
            # Eliminate known visited URLs and duplicates
            for rawurl in rawurls:
                # Put new URLs in cache if present
                if rawurl not in visited: cache[rawurl] = url
        # Put URL in list of good URLs
        if url: self._good[url] = base

    def _onewalk(self):
        '''Yields good URLs from under a base URL'''
        # Assignments
        cache, genverify2 = self._cache, self._genverify2
        # End processing if cache is empty
        while cache:
            # Fetch item from cache
            base, urls = cache.popitem()
            # If item has child URLs, process them and yield good URLs
            if urls: 
                for url in genverify2(urls, base): yield url

    def _multiwalk(self, threads):
        '''Extracts good URLs from under a base URL

        Arguments:
        threads -- number of threads to run'''

        def urlthread(url, base):
            '''Spawns a thread containing a multiverify function

            Arguments:
            url -- URL to verify
            base -- referring URL'''
            # Create instance of Thread
            dthread = Thread(target=multiverify, args=(url, base))
            # Put in pool
            pool.append(dthread)

        # Assignments
        pool, cache, multiverify = [], self._cache, self._multiverify
        Thread, width, good = self._thread, self.width, self._good
        # End processing if cache is empty
        while cache:
            # Process URLs as long as width not exceeded
            if len(good) <= width:
                # Fetch item from cache
                url, base = cache.popitem()
                # Make thread
                if url: urlthread(url, base)
                # Run threads once pool size is reached
                if len(pool) == threads or threads >= len(cache):
                    # Start threads
                    for thread in pool: thread.start()
                    # Empty thread pool as threads complete
                    while pool:
                        for thread in pool:
                            if not thread.isAlive(): pool.remove(thread)
            # End if width reached
            elif len(good) >= width: break

    def weburls(self, base=None, width=200, depth=5, thread=None, *vargs):
        '''Returns a list of web paths.

        Arguments:
        base -- base web URL (default: None)
        width -- amount of resources to crawl (default: 200)
        depth -- depth in hierarchy to crawl (default: 5)
        thread -- number of threads to run (default: None)
        vargs -- information to include or exclude
        To override defaults:
        To include information add 'badhtm', 'redir', 'out', or 'other'
        To exclude information add "badurl"'''
        # Default information gathering
        self._badurl, self._badhtm, self._redir, self._out  = 1, 0, 0, 0
        self.badhtms, self.others, self._other = None, None, 0
        self.badurls, self.redirs, self.outs = [], None, None
        # Override default settings if argument is passed to vargs
        for arg in vargs:
            if arg == 'badurl': self._badurl, self.badurls = 0, None
            elif arg == 'badhtm': self._badhtm, self.badhtms = 1, {}
            elif arg == 'redir': self._redir, self.redirs = 1, {}
            elif arg == 'out': self._out, self.outs = 1, {}
            elif arg == 'other': self._other, self.others = 1, {}
        # Assignments        
        self._visited, self._good, self._cache = {}, {}, {}
        onewalk, self._robot = self._onewalk, self._rparser()
        uparse, robot, multiwalk = self._uparse, self._robot, self._multiwalk
        cache, good = self._cache, self._good
        # Assign width
        if self.width and width == 200: width = self.width
        else: self.width = width
        # sgmlop crashes Python after too many iterations
        #if width > 5000:
        self._defasync()
        self._parserpick(1)        
        #else: self._parserpick() 
        # Use global base if present
        if not base: base = self.base
        # Verify URL and get child URLs
        newbase, rawurls = self._webopen(base, '', base)
        if newbase:
            # Change base URL if different
            base, newurls = newbase, {}
            # Ensure there's a trailing '/' in base URL
            if base[-1] != '/':
                url = list(uparse.urlsplit(base))
                url[1] = ''.join([url[1], '/'])
                base = uparse.urlunsplit(url)
            # Eliminate duplicates and put raw URLs in cache
            for rawurl in rawurls: newurls[rawurl] = 1
            if newurls:
                # Cache URLs individually if threads are desired
                if thread:
                    for new in newurls: cache[new] = base
                # Cache in group if no threads
                else: cache[base] = newurls
            # Make base URL, get split, and put in verified URL list
            self.base, self._sb = base, base.split('/')
            self._visited[base], good[base] = 1, base
        # If URL is bad, abort and raise exception
        else: raise IOError, 'Invalid URL'
        # Adjust dept to length of base URL
        if self.depth and depth == 6: self.depth += len(self._sb)
        else: self.depth = depth + len(self._sb)
        # Get robot limits
        robot.set_url(''.join([base, 'robots.txt']))
        robot.read()
        # Get signature of bad URL
        self._webtest2()
        # Get good URLs as long as total width isn't exceeded
        try:
            # Multiwalk if threaded
            if thread: self._multiwalk(thread)
            # Otherwise, use single thread
            else:
                for item in onewalk():
                    # Don't exceed maximum width
                    if len(good) <= width:
                        good[item[0]] = item[1]
                        print 2, len(good)
                        print 3, len(self._cache)
                    elif len(good) >= width: break
        # If user interrupts crawl, return what's done
        except KeyboardInterrupt: pass
        # Get URLs, sort them, and return list
        self.urls = good.keys()
        self.urls.sort()
        return self.urls

    def webpaths(self, b=None, w=200, d=5, t=None, *vargs):
        '''Returns a list of web paths.
        
        Arguments:
        b -- base web URL (default: None)
        w -- amount of resources to crawl (default: 200)
        d -- depth in hierarchy to crawl (default: 5)
        t -- number of threads (default: None)
        vargs -- information to include or exclude'''

        def pathize():
            '''Strips base URL from full URLs to produce paths'''
            for url in urls:
                # Remove base URL from path list
                url = url.replace(self.base, '')
                # Add default name 'index.html' to root URLs and directories
                if not url: url = 'index.html'
                elif url[-1] == '/': url = ''.join([url, 'index.html'])
                # Verify removal of base URL and remove it if found
                if url.find(':') != -1: url = urlsplit(url)[2:][0]                
                yield url

        # Assignments
        urlsplit = self._uparse.urlsplit
        # Run weburls if base passed as an argument
        if b: self.weburls(b, w, d, t, vargs)
        # Strip off trailing resource or query from base URL
        if self.base[-1] != '/': self.base = '/'.join(self._sb[:-1])
        urls = self.urls
        # Return path list after stripping base URL
        self.paths = list(pathize())
        return self.paths

    def webmirror(self, root=None, t=None, base=None, width=200, depth=5):
        '''Mirrors a website on a local filesystem

        Arguments:
        root -- local filesystem path (default: None)
        t -- number of threads (default: None)
        base -- base web URL (default: None)
        width -- amount of resources to crawl (default: 200)
        depth -- depth in hierarchy to crawl (default: 5)'''
        if base: self.webspider(base, width, depth, t, 'badurl')
        return self._mirror((self.paths, self.urls), root, t)
    
    def webspider(self, b=None, w=200, d=5, t=None, *vargs):
        '''Returns two lists of child URLs and paths
        
        Arguments:
        b -- base web URL (default: None)
        w -- amount of resources to crawl (default: 200)
        d -- depth in hierarchy to crawl (default: 5)
        t -- number of threads (default: None)
        vargs -- information to include or exclude'''
        if b: self.weburls(b, w, d, t)
        return self.webpaths(), self.urls

    def badurlreport(self, f=None, b=None, w=200, d=5, t=None):
        '''Pretties up a list of bad URLs

        Arguments:
        f -- output file for report (default: None)
        b -- base web URL (default: None)
        w -- amount of resources to crawl (default: 200)
        d -- depth in hierarchy to crawl (default: 5)
        t -- number of threads (default: None)'''
        if b: self.weburls(b, w, d, t)
        # Format report if information is available
        if self.badurls:
            # Number of bad URLs
            amount = str(len(self.badurls))
            header = '%s broken URLs under %s on %s:\n'
            # Print referring URL pointing to bad URL
            body = '\n'.join([' -> '.join(i) for i in self.badurls])
            report = self._formatreport(amount, header, body, f)
            # Return if just getting string
            if report: return report

    def badhtmreport(self, f=None, b=None, w=200, d=5, t=None):
        '''Pretties up a list of unparsed HTML URLs

        Arguments:
        f -- output file for report (default: None)
        b -- base web URL (default: None)
        w -- amount of resources to crawl (default: 200)
        d -- depth in hierarchy to crawl (default: 5)
        t -- number of threads (default: None)'''
        if b: self.weburls(b, w, d, t, 'badhtm')
        # Format report if information is available
        if self.badhtms:
            amount = str(len(self.badhtms))
            header = '%s unparsable HTML URLs under %s on %s:\n'
            body = '\n'.join(self.badhtms)
            report = self._formatreport(amount, header, body, f)
            # Return if just getting string
            if report: return report

    def redireport(self, f=None, b=None, w=200, d=5, t=None):
        '''Pretties up a list of URLs redirected to an external URL

        Arguments:
        f -- output file for report (default: None)
        b -- base web URL (default: None)
        w -- amount of resources to crawl (default: 200)
        d -- depth in hierarchy to crawl (default: 5)
        t -- number of threads (default: None)'''
        if b: self.weburls(b, w, d, t, 'redir')
        # Format report if information is available
        if self.redirs:
            amount = str(len(self.redirs))
            header = '%s redirects to external URLs under %s on %s:\n'
            # Print referring URL pointing to new URL
            body = '\n'.join([' -> '.join(i) for i in self.redirs])
            report = self._formatreport(amount, header, body, f)
            # Return if just getting string
            if report: return report

    def outreport(self, f=None, b=None, w=200, d=5, t=None):
        '''Pretties up a list of outside URLs referenced under the base URL

        Arguments:
        f -- output file for report (default: None)
        b -- base web URL (default: None)
        w -- amount of resources to crawl (default: 200)
        d -- depth in hierarchy to crawl (default: 5)
        t -- number of threads (default: None)'''
        if b: self.weburls(b, w, d, t, 'out')
        # Format report if information is available
        if self.outs:
            amount = str(len(self.outs))
            header = '%s links to external URLs under %s on %s:\n'
            body = '\n'.join(self.outs)
            report = self._formatreport(amount, header, body, f)
            # Return if just getting string
            if report: return report            

    def othereport(self, f=None, b=None, w=200, d=5, t=None):
        '''Pretties up a list of non-HTTP/FTP URLs

        Arguments:
        f -- output file for report (default: None)
        b -- base web URL (default: None)
        w -- amount of resources to crawl (default: 200)
        d -- depth in hierarchy to crawl (default: 5)
        t -- number of threads (default: None)'''
        if b: self.weburls(b, w, d, t, 'other')
        # Format report if information is available
        if self.others:
            amount = str(len(self.others))
            header = '%s non-FTP/non-HTTP URLs under %s on %s:\n'
            body = '\n'.join(self.others)
            report = self._formatreport(amount, header, body, f)
            # Return if just getting string
            if report: return report

    def urlreport(self, f=None, b=None, w=200, d=5, t=None):
        '''Pretties up a list of all URLs under a URL

        Arguments:
        f -- output file for report (default: None)
        b -- base web URL (default: None)
        w -- amount of resources to crawl (default: 200)
        d -- depth in hierarchy to crawl (default: 5)
        t -- number of threads (default: None)'''
        if b: self.weburls(b, w, d, t)
        # Format report if information is available
        if self.urls:
            amount = str(len(self.urls))
            header = '%s verified URLs under %s on %s:\n'
            body = '\n'.join(self.urls)
            report = self._formatreport(amount, header, body, f)
            # Return if just getting string
            if report: return report

    def webreport(self, f=None, b=None, w=200, d=5, t=None, *vargs):
        '''Pretties up a list of logged information under a URL

        Arguments:
        f -- output file for report (default: None)
        b -- base web URL (default: None)
        w -- amount of resources to crawl (default: 200)
        d -- depth in hierarchy to crawl (default: 5)
        t -- number of threads (default: None)
        vargs -- report sections to include or exclude
        To override defaults:
        To include a section add 'badhtm', 'redir', 'out', or 'other'
        To exclude a section add 'badurl' or "url"'''
        if b: self.weburls(b, w, d, t)
        # Defaults for report
        badurl, badhtm, redir, url, out, other = 1, 0, 0, 1, 0, 0
        # Create compilation list
        compile = []
        # Override default report settings if argument is passed to vargs
        for arg in vargs:
            if arg == 'badurl': badurl = 0
            elif arg == 'badhtm': badhtm = 1
            elif arg == 'redir': redir = 1
            elif arg == 'url': url = 0
            elif arg == 'out': out = 1
            elif arg == 'other': other = 1
        # Compile report
        if badurl:
            badurls = self.badurlreport()
            if badurls: compile.append(badurls)
        if url:
            urls = self.urlreport()
            if urls: compile.append(urls)
        if out:
            outs = self.outreport()
            if outs: compile.append(outs)
        if redir:
            redirs = self.redireport()
            if redirs: compile.append(redirs)
        if badhtm:
            badhtms = self.badhtmreport()
            if badhtms: compile.append(badhtms)        
        if other:
            others = self.othereport()
            if others: compile.append(others)
        # Make report
        report = '\n\n'.join(compile)
        # Write to file if argument present
        if file: open(f, 'w').write(report)
        # Or return string
        else: return report
        
    def _formatreport(self, amount, header, body, file=None):
        '''Generic prettifier with date/time stamper

        Arguments:
        header -- title of report
        body -- body of report
        file -- output file for report (default: None)'''
        # Get current time
        localtime, strftime = self._localtime, self._formtime
        curtime = strftime('%A, %B %d, %Y at %I:%M %p', localtime())
        # Make section header
        header = header % (amount, self.base, curtime)
        # Add header to body
        report = '\n'.join([header, body])
        # Write to file if argument present
        if file: open(file, 'w').write(report)
        # Or return string
        else: return report

    def _mirror(self, lists, root=None, threads=None):
        '''Mirrors a site on a local filesystem based on lists passed to it

        Argument:
        lists -- lists of URLs and paths
        root -- local filesystem path (default: None)
        threads -- number of threads (default: None)'''

        def download(url, np, op):
            '''Downloads files that need to be mirrored.'''
            # If ftp...
            if url[:3] == 'ftp':
                # Open local file
                local = open(np, 'wb')
                # Download using FTP session
                ftp = ftpopen(base, name, password)
                ftp.retrbinary('RETR %s' % op, local.write)
                ftp.close()
                # Close local file
                local.close()
            # Use normal urlretrieve if no FTP required
            else: ulib.urlretrieve(url, np)

        def dlthread(url, np, op):
            '''Spawns a thread containing the download function'''
            # Create thread
            dthread = Thread(target=download, args=(url, np, op))
            # Add to thread pool
            pool.append(dthread)

        # Extract path and URL lists
        paths, urls = lists
        # Avoid outside namespace lookups
        ulib, makedirs, sep = self._ulib, self._os.makedirs, self._os.sep
        normcase, split = self._path.normcase, self._path.split
        exists, isdir = self._path.exists, self._path.isdir
        ftpopen = self._ftpopen
        # Create local names for thread class and thread pool
        if threads: Thread, pool = self._thread, []
        # Localize name and password if exists
        try: base, name, password = self.base, self._name, self._password
        except AttributeError: pass
        # Change to directory if given...
        if root:
            if exists(root):
                if isdir(root): self._os.chdir(root)
            # Create root if it doesn't exist
            else:
                makedirs(root)
                self._os.chdir(root)
        # Otherwise use current directory
        else: root = self._os.getcwd()
        # Iterate over paths and download files
        for oldpath in paths:
            # Sync with the URL for oldpath
            url = urls[paths.index(oldpath)]
            # Create name of local copy
            newpath = normcase(oldpath).lstrip(sep)
            # Get directory name
            dirname = split(newpath)[0]
            # If the directory exists, download the file directly
            if exists(dirname):
                if isdir(dirname):
                    if threads: dlthread(url, newpath, oldpath)
                    else: download(url, newpath, oldpath)
            # Don't create local directory if path in root of remote URL
            elif not dirname:
                if threads: dlthread(url, newpath, oldpath)
                else: download(url, newpath, oldpath)
            # Make local directory if it doesn't exist, then dowload file
            else:
                makedirs(dirname)
                if threads: dlthread(url, newpath, oldpath)
                else: download(url, newpath, oldpath)
            # Run threads if they've hit the max number of threads allowed
            if threads:
                # Run if max threads or final thread reached
                if len(pool) == threads or paths[-1] == oldpath:
                    # Start all threads
                    for thread in pool: thread.start()
                    # Clear the thread pool as they finish
                    while pool:
                        for thread in pool:
                            if not thread.isAlive(): pool.remove(thread)


# Instance of Spider enables exporting Spider's methods as standalone functions
_inst = Spider()
ftpurls = _inst.ftpurls
weburls = _inst.weburls
ftppaths = _inst.ftppaths
webpaths = _inst.webpaths
ftpmirror = _inst.ftpmirror
ftpspider = _inst.ftpspider
webmirror = _inst.webmirror
webspider = _inst.webspider
webreport = _inst.webreport
urlreport = _inst.urlreport
outreport = _inst.outreport
redireport = _inst.redireport
othereport = _inst.othereport
badurlreport = _inst.badurlreport
badhtmreport = _inst.badhtmreport
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.