Welcome, guest | Sign In | My Account | Store | Cart

I will download all of the recipes from the Python Cookbook Includes a Tk input interface and a new contest (replaces previous recipes: 541083, 542189, and 542196))

Python, 1009 lines
   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
"""I am P1.

The latest public version of me may be found at:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/543267/index_txt

I will download all of the recipes from the Python Cookbook using
the URL http://aspn.activestate.com/ASPN/Cookbook/Python?query_start=????
and store them into the drive and directory you specify.

I extract the file names using the recipe number and the HTML <a></a> string;
replacing special characters with an underscore.  I also clean up the filename.

There are a few commands that I recognize:
   Help or ? - shows the list of commands
   Recipes   - shows lists of recipe numbers as they are downloaded
   Files     - shows lists of file names and their CRC64 hashes
   Stats     - shows processing statistics for this run
   Compile   - compiles everything in the selected download directory
   Time      - shows timing information for this process
   Find      - shows the number of recipes that have recipe numbers
               greater than the recipe number you enter, used for planning,
               this does not download the recipes.
   Print     - route output to the specified networked printer
   You may combine them with interleaving spaces;  Help will stop the process.

You will be asked for information such as:

If you so choose, enter the drive and the full path name, with trailing backslash where
    the downloaded folders and files will end up. -- J:\Python\Doc\Cookbook\ is the default

If you so choose, enter a number of a recipe where I should start downloading
    (if negative, I'll start retreiving from the recipe last used.) -- 0 is the default
    -1 looks for the number in ...\largest_number.txt
    99999999 will skip the Cookbook download and proceed to the other download sections

If you so choose, enter a command (Help, Stats, Time, Recipes, Files, ?, Compile, Find, Print)
    -- 'Stats Time Print' are the defaults

If you have chosen the command 'Print', enter the printer's network path
    (\\\\servername\printername) -- no default

If you so choose, enter the URL (hyperlink,) with trailing backslash, to a website 
    to download all files from that website that match the .suffix you specify in the .py box

If you so choose, click Download all .??? files from the ENTIRE World Wide Web -- .py is the default

If you so choose, click Enter the Download Contest -- no default
    Before doing this you must have modified the P1 source code on only the one line so noted,
    so that the Google search string will find the most .py files anywhere on the World Wide Web.
    Find that line on line #375 of this source code, or search for 'CONTEST CONTEST'.

If you have clicked the 'Enter the Download Contest' button:
    Enter your Name or u$3rN4m3.
    Enter your e-mail address.
    Enter your SMTP server name.
        To find this in MS Outlook 2003:
        1)  On the Menu Bar, click on Tools, then
        2)  click on E-mail Accounts..., then
        3)  select "View or change existing e-mail accounts'; then click next, then
        4)  select "Microsoft Exchange Server"; then click Change...; then
        5)  select the text inside the box labeled "Microsoft Exchange Server"; then
        6)  press ctrl-c (to copy this selected text); then click cancel
        7)  back at 'your.smtp.server.name'; select it and press ctrl-v

Click either Quit or Continue.  Quit will halt the process.  Continue will proceed with the process.

CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST 
Each month the winner of the contest will have their information
logged here, possibly shown in the next version of the printed Cookbook
+-------------------------------------------------------------------------------------------+
|MMM/Year Name             E-mail Address                   Duration       # of Files       |
|Jan/2008 pee one          p1p1p1p1p1p1p1p1p1p1p1@gmail.com 00:40:23       2,047            |
|         searchFor = '.py+intitle:index.of'                                                |
|Feb/2008                                                                                   |
|Mar/2008                                                                                   |
|Apr/2008                                                                                   |
|May/2008                                                                                   |
|Jun/2008                                                                                   |
|Jul/2008                                                                                   |
|Aug/2008                                                                                   |
|Sep/2008                                                                                   |
|Oct/2008                                                                                   |
|Nov/2008                                                                                   |
|Dec/2008                                                                                   |
|Jan/2009                                                                                   |
|Feb/2009                                                                                   |
|Mar/2009                                                                                   |
|Apr/2009                                                                                   |
|May/2009                                                                                   |
|Jun/2009                                                                                   |
|Jul/2009                                                                                   |
|Aug/2009                                                                                   |
|Sep/2009                                                                                   |
|Oct/2009                                                                                   |
|Nov/2009                                                                                   |
|Dec/2009                                                                                   |
+-------------------------------------------------------------------------------------------+
I am P1.
"""
#J:\Python\Doc\Cookbook\
try:
    import re,urllib,urllib2,os,sys,Tkinter,string,readline
except ImportError:
    raise ImportError, "This program requires Python 2.4 or later."

from Tkinter import *

#Initialize counters
fls = rem = skipped = recipe_count = largest_number = nooffolders = nooffiles = time2Quit = skipCookbook = 0

# CRC64 is borrowed from _259177_CRC64_Calculate_the_cyclic_redundancy_check.py
POLY64REVh = 0xd8000000L
CRCTableh = [0] * 256
CRCTablel = [0] * 256
isInitialized = False

def CRC64(aString):
    global isInitialized
    crcl = 0
    crch = 0
    if (isInitialized is not True):
        isInitialized = True
        for i in xrange(256):
            partl = i
            parth = 0L
            for j in xrange(8):
                rflag = partl & 1L
                partl >>= 1L
                if (parth & 1):
                    partl |= (1L << 31L)
                parth >>= 1L
                if rflag:
                    parth ^= POLY64REVh
            CRCTableh[i] = parth;
            CRCTablel[i] = partl;

    for item in aString:
        shr = 0L
        shr = (crch & 0xFF) << 24
        temp1h = crch >> 8L
        temp1l = (crcl >> 8L) | shr
        tableindex = (crcl ^ ord(item)) & 0xFF

        crch = temp1h ^ CRCTableh[tableindex]
        crcl = temp1l ^ CRCTablel[tableindex]
    return (crch, crcl)

def CRC64digest(aString):
    return "%08X%08X" % (CRC64(aString))

def printP1():
    p = ''.join("""oolcay itay""")
    print p
    if printFound != -1:
        pp.write(p)
        pp.flush()
        pp.close()
    sys.exit()

def help():
    p = __doc__
    print p
    if printFound != -1:
        pp.write(p)
        pp.flush()
        pp.close()
    sys.exit()

class App:
    def __init__(self, master):
        #Define the outer frame
        frame = Frame(master)
        frame.grid()

        #Define a label(0,0) asking for the name of the storage directory
        self.label1 = Label(frame, wraplength=500, justify=LEFT, text="Enter the drive and the full path name, with trailing backslash where the downloaded files will end up. -- J:\Python\Doc\Cookbook\ is the default -->", font=("Verdana", 12))
        self.label1.grid(row=0, column=0, sticky=W)

        #Define an entry(0,1) accepting the name of the storage directory
        self.entry1 = Entry(frame, fg="blue", font=("Verdana", 12), relief=RAISED, width=25)
        self.entry1.grid(row=0, column=1, sticky=W)
        self.entry1.insert(0, 'J:\\Python\\Doc\\Cookbook\\')

        #Define a label(1,0) asking for the starting recipe number
        self.label2 = Label(frame, wraplength=500, justify=LEFT, text="Enter a number of a recipe where I should start downloading (if negative, gets recipe last used.) -- 0 is the default; -1 looks for the number in <specified drive and path>\largest_number.txt-->", font=("Verdana", 12))
        self.label2.grid(row=1, column=0, sticky=W)

        #Define an entry(1,1) accepting the starting recipe number
        self.entry2 = Entry(frame, fg="blue", font=("Verdana", 12), relief=RAISED, width=25)
        self.entry2.grid(row=1, column=1, sticky=W)
        self.entry2.insert(0, '0')

        #Define a label(2,0) asking for a command list
        self.label3 = Label(frame, wraplength=500, justify=LEFT, text="Enter a command (Help, ?, Stats, Time, Recipes, Files, Compile, Find, Print, Multiple commands may be separated by spaces.) 'Stats Time Print' is the default -->", font=("Verdana", 12))
        self.label3.grid(row=2, column=0, sticky=W)

        #Define an entry(2,1) accepting the command list
        self.entry3 = Entry(frame, fg="blue", font=("Verdana", 12), relief=RAISED, width=25)
        self.entry3.grid(row=2, column=1, sticky=W)
        self.entry3.insert(0, 'Stats Time Print')

        #Define a lable(3,0) asking for the printer's network path
        self.label4 = Label(frame, wraplength=500, justify=LEFT, text="Enter the printer's network path (Ignore this if you did not specify 'Print' as a command) (\\\\servername\printername) -- no default -->", font=("Verdana", 12))
        self.label4.grid(row=3, column=0, sticky=W)

        #Define an entry(3,1) accepting the printer's network path
        self.entry4 = Entry(frame, fg="blue", font=("Verdana", 12), relief=RAISED, width=25)
        self.entry4.grid(row=3, column=1, sticky=W)
        self.entry4.insert(0, '\\\\servername\\printername')

        #Define a lable(4,0) asking for the URL to search
        self.label5 = Label(frame, wraplength=500, justify=LEFT, text="Enter the URL of the website to search for files.  (http://www.koders.com/zeitgeist/python/)\n(Specify what kind of filetype in the .py box.)\n-- no default -->", font=("Verdana", 12))
        self.label5.grid(row=4, column=0, sticky=W)

        #Define an entry(4,1) accepting the URL to search
        self.entry5 = Entry(frame, fg="blue", font=("Verdana", 12), relief=RAISED, width=25)
        self.entry5.grid(row=4, column=1, sticky=W)
        self.entry5.insert(0, ' ')


        #Define a lable(5,0) asking to search the WWW
        self.label6 = Label(frame, wraplength=500, justify=LEFT, text="Check if you want to search the entire WWW for <anything><.supplied suffix> files -->", font=("Verdana", 12))
        self.label6.grid(row=5, column=0, sticky=W)

        #Define a checkbutton(5,1) accepting whether to search the WWW
        self.var1 = IntVar()
        self.textvar1 = StringVar()
        self.textvar1.set('Click to Search ENTIRE WWW')
        self.checkbutton1 = Checkbutton(frame, textvariable=self.textvar1, command=self.changeText, font=("Verdana", 12), foreground='blue', selectcolor='red', variable=self.var1, borderwidth=3, indicatoron=FALSE)
        self.checkbutton1.grid(row=5, column=1, sticky=W)

        #Define an entry(5,2) accepting the files' suffix (.py)
        self.entry7 = Entry(frame, fg="blue", font=("Verdana", 12), relief=RAISED, width=5, borderwidth=3)
        self.entry7.grid(row=5, column=2, sticky=W, padx=3)
        self.entry7.insert(0, '.py')


        #Define a label(6,0) asking to enter the contest
        self.label8 = Label(frame, wraplength=500, justify=LEFT, text="Check if you want to enter the contest to find the most .py files on the World Wide Web -->", font=("Verdana", 12))
        self.label8.grid(row=6, column=0, sticky=W)

        #Define a checkbutton(6,1) accepting whether to enter the contest
        self.var2 = IntVar()
        self.textvar2 = StringVar()
        self.textvar2.set('Click to Enter Contest')
        self.checkbutton2 = Checkbutton(frame, textvariable=self.textvar2, command=self.contestText, font=("Verdana", 12), foreground='blue', selectcolor='red', variable=self.var2, borderwidth=3, indicatoron=FALSE)
        self.checkbutton2.grid(row=6, column=1, sticky=W)

        #Define a label(7,0) asking for their name
        self.textvar3 = StringVar()
        self.textvar3.set(' ')
        self.label9 = Label(frame, wraplength=500, justify=LEFT, textvariable=self.textvar3, font=("Verdana", 12))
        self.label9.grid(row=7, column=0, sticky=W)
        
        #Define an entry(7,1) accepting their name
        self.entry9 = Entry(frame, fg="blue", font=("Verdana", 12), relief=RAISED, width=25)
        self.entry9.grid(row=7, column=1, sticky=W)
        self.entry9.insert(0, 'Your name or u$3rN4m3')

        #Define a label(8,0) asking for their email address
        self.textvar4 = StringVar()
        self.textvar4.set(' ')
        self.label10 = Label(frame, wraplength=500, justify=LEFT, textvariable=self.textvar4, font=("Verdana", 12))
        self.label10.grid(row=8, column=0, sticky=W)

        #Define an entry(8,1) accepting their email address
        self.entry10 = Entry(frame, fg="blue", font=("Verdana", 12), relief=RAISED, width=25)
        self.entry10.grid(row=8, column=1, sticky=W)
        self.entry10.insert(0, 'youremailname<at>youremail<dot>com')
        
        #Define a label(9,0) asking for their SMTP server name
        self.textvar5 = StringVar()
        self.textvar5.set(' ')
        self.label11 = Label(frame, wraplength=500, justify=LEFT, textvariable=self.textvar5, font=("Verdana", 12))
        self.label11.grid(row=9, column=0, sticky=W)
        
        #Define an entry(9,1) accepting their SMTP server name
        self.entry11 = Entry(frame, fg="blue", font=("Verdana", 12), relief=RAISED, width=25)
        self.entry11.grid(row=9, column=1, sticky=W)
        self.entry11.insert(0, 'your.smtp.server.name')
                
        #Define an entry(7,1) accepting their name
        self.labelb9b = Entry(frame, fg="blue", font=("Verdana", 12), relief=FLAT, width=25)
        self.labelb9b.grid(row=7, column=1, sticky=W)
        self.labelb9b.insert(0, ' ')

        #Define an entry(8,1) accepting their email address
        self.labelb10b = Entry(frame, fg="blue", font=("Verdana", 12), relief=FLAT, width=25)
        self.labelb10b.grid(row=8, column=1, sticky=W)
        self.labelb10b.insert(0, ' ')

        #Define an entry(9,1) accepting their SMTP server name
        self.labelb11b = Entry(frame, fg="blue", font=("Verdana", 12), relief=FLAT, width=25)
        self.labelb11b.grid(row=9, column=1, sticky=W)
        self.labelb11b.insert(0, ' ')


        import tkFont
        #Define a Verdana font
        Verdana = tkFont.Font(family="Verdana", size=12, weight=tkFont.BOLD)

        #Define a button to continue
        self.button1 = Button(frame, text='CONTINUE', fg='green', font=Verdana, width=20, command=self.say)
        self.button1.grid(row=10, column=0, columnspan=1)

        #Define a button to quit
        self.button2 = Button(frame, text='QUIT', fg='red', font=Verdana, width=20, command=self.quit)
        self.button2.grid(row=10, column=1, columnspan=1)

    def quit(self):
        #Chop down the tree and pull out the roots
        global time2Quit
        time2Quit = 1
        root.destroy()

    def say(self):
        #Define a command list to set the variables from the contents of the entry boxes
        global fileInName, greaterThan, commandPrompt, printPath, searchURL, fileSuffix
        fileInName    = app.entry1.get()
        greaterThan   = app.entry2.get()
        commandPrompt = app.entry3.get()
        printPath     = app.entry4.get()
        searchURL     = app.entry5.get()
        fileSuffix    = app.entry7.get()
        root.destroy()

    def changeText(self):
        #Define a command list to set the searchWWW button text
        global searchWWW
        searchWWW = app.var1.get()
        if app.var1.get() == 0:
            app.textvar1.set('Search Disabled')
        else:
            app.textvar1.set('Search ENTIRE WWW')

    def contestText(self):
        """Define a command list to:
           1) set contestWWW from the contents of the entry box
           2) set the contestWWW button text
           3) turn on and off the labels and entry boxes of the name, email address, and smtp server name """
        global contestWWW, fromname, fromaddr, smtpServer
        contestWWW = app.var2.get()
        fromname   = app.entry9.get()
        fromaddr   = app.entry10.get()
        smtpServer = app.entry11.get()
        if app.var2.get() == 0:
            app.textvar2.set("No, thanks")
            #Hide self.lable7, self.entry7, self.lable8, and self.entry8
            app.textvar3.set(" ")
            app.textvar4.set(" ")
            app.textvar5.set(" ")
            app.entry9.lower()
            app.entry10.lower()
            app.entry11.lower()
            app.labelb9b.lift()
            app.labelb10b.lift()
            app.labelb11b.lift()
        else:
            app.textvar2.set("YES, CONTEST ENTERED!!")
            #Unhide self.lable7, self.entry7, self.lable8, and self.entry8
            app.textvar3.set("Enter your full name or your u$3rN4m3, will be entered in the comments log and possibly in the printed Cookbook) -- no default -->")
            app.textvar4.set("Enter your email address (youremailname<at>youremail<dot>com) -- no default -->")
            app.textvar5.set("Enter your SMTP server name (your.smtp.server.name) -- no default -->")
            app.entry9.lift()
            app.entry10.lift()
            app.entry11.lift()
            app.labelb9b.lower()
            app.labelb10b.lower()
            app.labelb11b.lower()
#Start the Google search
def getGoogle():
    global searchFor
    re_links = re.compile(r'<h2 class=r><a href="(.+?)"',re.IGNORECASE|re.DOTALL)
    #CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST
    #If you are entering the contest this is the one line of code you are allowed to modify (change only what is between the single quotes):
    searchFor = 'py+intitle:index.of'
    #original line of code:  searchFor = 'py+intitle:index.of'
    #CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST CONTEST 
    p = 'Google: Searching on google...'
    print p
    if printFound != -1:
        pp.write(p)
    links = {}
    currentPage = 0
    while True:
        p = "Google: Querying page %d (%d links found so far)" % (currentPage/100+1, len(links))
        print p
        if printFound != -1:
            pp.write(p)
        address = "http://www.google.com/search?q=%s&num=100&hl=en&start=%d" % (urllib.quote_plus(searchFor),currentPage)
        p = address
        print p
        if printFound != -1:
            pp.write(p)
        request = urllib2.Request(address, None, {'User-Agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'} )
        urlfile = urllib2.urlopen(request)
        #page = urlfile.read(200000)
        page = urlfile.read()
        p = 'page:', page
        print p
        if printFound != -1:
            pp.write(p)
        urlfile.close()
        for url in re_links.findall(page):
            links[url] = 0
        if "</div>Next</a></table></div></div><center>" in page: # Is there a "Next" link for next page of results ?
            currentPage += 100  # Yes, go to next page of results.
        else:
            break   # No, break out of the while True loop.
    p = "Google: Found %d links." % len(links)
    print p
    if printFound != -1:
        pp.write(p)
    return sorted(links.keys())

#Get files found on the returned Google page
def getFile(path, site, dir):
    global s, files, nooffiles, folder
    url = site + dir
    try:
        f = urllib.urlopen(url)
    except:
        p = 'Bad URL open: ' + url
        print p
        if printFound != -1:
            pp.write(p)
        return
    s = f.read()
    f.close()
    regexPy = re.compile('(?<=href=")[A-Za-z0-9_-]*'+fileSuffix)
    matches = regexPy.findall(s)
    for x in range(len(matches)):
        url = site + dir + matches[x]
        try:
            f = urllib.urlopen(url)
        except:
            p = 'Can not open URL: ' + url
            print p
            if printFound != -1:
                pp.write(p)
            continue
        s = f.read()
        f.close()
        if folder[-4:-1] == fileSuffix:
            try:
                os.chdir(path + folder[7:] + dir)
            except:
                p = 'Can not chdir to folder name: ' + folder
                print p
                if printFound != -1:
                    pp.write(p)
                continue
        else:
            try:
                os.chdir(path + folder[7:-1] + dir)
            except:
                p = 'Can not chdir to folder name: ' + folder
                print p
                if printFound != -1:
                    pp.write(p)
                continue   
        try:
            m = open(matches[x], 'w')
            m.write(s)
            m.close()
            p = 'Loading file: ' + matches[x]
            print p
            if printFound != -1:
                pp.write(p)  
            files += 1
            nooffiles += 1
        except:
            p = matches[x] + ' IOError: [Errno 9] Bad file descriptor'
            print p
            if printFound != -1:
                pp.write(p)

#Begin processing; request information
root = Tk()
root.tk_bisque()
root.title('P-1')
app = App(root)
root.mainloop()

#If the Quit button was pressed stop the presses (pun intended)
if time2Quit == 1:
    time2Quit = 0
    print 'Quit button pressed -- process halted'
    sys.exit()

#If help was asked for, help them
helpFound = string.find(str(commandPrompt),'Help')
if helpFound != -1:
    help()

#Shhh
POneFound = string.find(str(commandPrompt),'P-1')
if POneFound != -1:
    printP1()

#Shhh
requestFound = string.find(str(commandPrompt),'rodtsasdt llllllreport')
if requestFound != -1:
    import time
    now = time.time()
    cpus = 20476
    CP = [[1175414400000,2835],[1192176000000,2912],[1193122800000,4321],[1199433600000,6283],[2147328000000,43008],[46893711600000,2147483647]]
    for i in range(5):
        if now < CP[i][0]:break
    ts = CP[i - 1][0];
    bs = CP[i - 1][1];
    megabytes = (((ts-now) / (CP[i][0]+ts) * (CP[i][1]-bs)) + bs);
    cpus = cpus + int(largest_number % int(10000))
    p = ''.join('P-1 CUR ALLOC ' + str(cpus) + ' .... ' + str(megabytes) + ' megabytes\nHELLO GREGORY')
    print p
    if printFound != -1:
        pp.write(p)
        pp.flush()
        pp.close()
    sys.exit()

#Shhh
POneFound = string.find(str(commandPrompt),'P1')
if POneFound != -1:
    printP1()

#If help was asked for, help them
helpFound = string.find(str(commandPrompt),'?')
if helpFound != -1:
    help()

#Open the print path if necessary
printFound = string.find(str(commandPrompt),'Print')
if printFound != -1:
    try:
        #printPath = raw_input("Enter the printer's network path (\\\\servername\printername)-->")
        pp = open(printPath, 'w')
    except:
        print printPath, 'does not exist -- system shutting down\n'
        sys.exit()
        
#Format and Print the start time
timeFound = string.find(str(commandPrompt),'Time')
if timeFound != -1:
    import time
    start = time.localtime(time.time())
    year, month, day, hour, minute, second, weekday, yearday, daylight = start
    p = ''.join("Process Started " + "%02d:%02d:%02d" % (hour, minute, second) + '\n')
    print p
    if printFound != -1:
        pp.write(p)
else:
    p = ''.join("Process Started\n")
    print p
    if printFound != -1:
        pp.write(p)

#If largest_number.txt is not found create it
largestFound = 0
try:
    for root, dirs, files in os.walk(fileInName):
        for _name in files:
            if _name == 'largest_number.txt':
                #print 'largest_number.txt found in',_name
                largestFound += 1
except:
    try:
        root.destroy()
    except:
        pass
    print '\n for root, dirs, files in os.walk(fileInName): line #560 failed\n'
    sys.exit()

#Keep track of the largest Recipe number found so far
if largestFound == 0:
    largest_number = 0
    filegTName = fileInName + 'largest_number.txt'
    try:
        gT = open(filegTName, 'w')
        gT.write(str(largest_number))
        gT.flush()
        gT.close()
    except:
        print 'Open failed on file:', filegTName
        print 'If you entered the correct directory name, then make sure it has been built.'
        sys.exit()
        

#fileInName = raw_input('Enter the drive and the full path name, with trailing backslash where the downloaded files will end up-->')
if fileInName == '':
    fileInName = 'J:\\Python\\Doc\\Cookbook\\'

#greaterThan = input('Enter a number of a recipe where I should start downloading (if negative, gets number last used)-->')
if greaterThan == '':
    greaterThan = 0
if greaterThan == None:
    greaterThan = 0
greaterThan = int(greaterThan)
if greaterThan < 0:
    filegTName = fileInName + 'largest_number.txt'
    gT = open(filegTName, 'r')
    largest_number = gT.readlines()
    greaterThan = int(largest_number[0])
    largest_number = int(largest_number[0])
    gT.flush()
    gT.close()
if greaterThan == 99999999:
    skipCookbook = 1
    
#commandPrompt = raw_input('Enter a command (Help,Stats,Time, Recipes,Files,?, Compile,Find,Print)-->')
if commandPrompt == '':
    commandPrompt = 'Stats Time Print'

#Check to see if we are going to search one website
if searchURL == '' or searchURL == None or searchURL == ' ':
    pass
else:
    p = 'We are going to search for ' + searchURL + fileSuffix + ' files'
    print p
    if printFound != -1:
        pp.write(p)
    p = 'Good Connection: ' + searchURL
    print p
    if printFound != -1:
        pp.write(p)
    path = fileInName
    site = searchURL + fileSuffix
    dir  = ''
    folder = str(re.sub("[\[\`\!\%\^\&\*\(\)\+\-\=\{\}\:\;\<\>\,\?\|\'\"\]]",'_',site))
    #Build a directory(folder) for each returned "site"
    try:
        if folder[-4:-1] == fileSuffix:
            os.makedirs(path + folder[8:])
            nooffolders += 1
        else:
            os.makedirs(path + folder[8:-1])
            nooffolders += 1
    except WindowsError, e:
        if e.errno == 17:
            pass
        else:
            raise
    files = 0
    getFile(path, site, dir)
    if files == 0:
        try:
            if folder[-4:-1] == fileSuffix:
                os.removedirs(path + folder[7:])
                nooffolders -= 1
            else:
                os.removedirs(path + folder[7:-1])
                nooffolders -= 1
        except:
            p = 'Directory remove failed -- not empty ' + path+folder[7:-1]
            print p
            if printFound != -1:
                pp.write(p)    
    #For each directory found iterate and get more files from that directory
    regexDir = re.compile('(?<=href=)"[A-Za-z0-9_-]*/"')
    matches = regexDir.findall(s)
    for x in range(len(matches)):
        try:
            if folder[-4:-1] == fileSuffix:
                os.mkdir(path + folder[8:] + matches[x][1:-1])
                nooffolders += 1
            else:
                os.mkdir(path + folder[8:-1] + matches[x][1:-1])
                nooffolders += 1
        except WindowsError, e:
            if e.errno == 17:
                pass
            else:
                raise
        files = 0
        getFile(path, site, matches[x][1:-1])
        if files == 0:
            if folder[-4:-1] == fileSuffix:
                os.rmdir(path + folder[8:] + matches[x][1:-1])
                nooffolders -= 1
            else:
                os.rmdir(path + folder[8:-1] + matches[x][1:-1])
                nooffolders -= 1

#Check to see if we are going to search the ENTIRE World Wide Web for files
if app.var1.get() == 1:
    #Check to see if we are going to compete in the CONTEST
    if app.var2.get() == 1:
        fileSuffix = '.py'
    p = 'We are going to search the ENTIRE World Wide Web for ' + fileSuffix + ' files'
    print p
    if printFound != -1:
        pp.write(p)
    #Process 100 pages per search, for a total limit of 1000 URLs placed in the "sites" list
    pages = getGoogle()
    for x in range(len(pages)):
        p = 'Good Connection: ' + pages[x]
        print p
        if printFound != -1:
            pp.write(p)
        path = fileInName
        site = pages[x]
        dir  = ''
        folder = str(re.sub("[\[\`\!\%\^\&\*\(\)\+\-\=\{\}\:\;\<\>\,\?\|\'\"\]]",'_',site))
        #Build a directory(folder) for each returned "site"
        try:
            if folder[-4:-1] == fileSuffix:
                os.makedirs(path + folder[7:])
                nooffolders += 1
            else:
                os.makedirs(path + folder[7:-1])
                nooffolders += 1
        except WindowsError, e:
            if e.errno == 17:
                pass
            else:
                raise
        files = 0
        getFile(path, site, dir)
        if files == 0:
            try:
                if folder[-4:-1] == fileSuffix:
                    os.removedirs(path + folder[7:])
                    nooffolders -= 1
                else:
                    os.removedirs(path + folder[7:-1])
                    nooffolders -= 1
            except:
                p = 'Directory remove failed -- not empty ' + path+folder[7:-1]
                print p
                if printFound != -1:
                    pp.write(p)    
    #For each directory found iterate and get more files from that directory
    regexDir = re.compile('(?<=href=)"[A-Za-z0-9_-]*/"')
    matches = regexDir.findall(s)
    for x in range(len(matches)):
        try:
            if folder[-4:-1] == fileSuffix:
                os.mkdir(path + folder[7:] + matches[x][1:-1])
                nooffolders += 1
            else:
                os.mkdir(path + folder[7:-1] + matches[x][1:-1])
                nooffolders += 1
        except WindowsError, e:
            if e.errno == 17:
                pass
            else:
                raise
        files = 0
        getFile(path, site, matches[x][1:-1])
        if files == 0:
            if folder[-4:-1] == fileSuffix:
                os.rmdir(path + folder[7:] + matches[x][1:-1])
                nooffolders -= 1
            else:
                os.rmdir(path + folder[7:-1] + matches[x][1:-1])
                nooffolders -= 1
                
if skipCookbook == 0:
    #Read in a batch of recipes
    for x in range(1,2120,20):
        url = 'http://aspn.activestate.com/ASPN/Cookbook/Python?query_start=' + str(x)
        f = urllib.urlopen(url)
        s = f.read()
        f.close()
        matches = re.findall("/ASPN/Cookbook/Python/Recipe/(\d*)",s)
        # if nothing was found try again
        if matches == []:
            f = urllib.urlopen(url)
            s = f.read()
            f.close()
            matches = re.findall("/ASPN/Cookbook/Python/Recipe/(\d*)",s)
        #If command = Recipes, list each recipe
        recipesFound = string.find(str(commandPrompt),'Recipes')
        if recipesFound != -1:
            p = ''.join(str(x) + ' ' + str(greaterThan) + ' ' + str(matches) + '\n')
            print p
            if printFound != -1:
                pp.write(p)
        #Loop through batches of recipes finding matches
        xcape = 'n'
        for y in range(len(matches)):
            try:
                intMatches = int(matches[y])
            except:
                intMatches = int(0)
            try:
                if intMatches < greaterThan:
                    xcape = 'y'
                else:
                    xcape = 'n'
                    break
            except:
                xcape = 'n'
                break
        if xcape == 'y':
            skipped += 1
            continue
        #Find the recipe numbers
        pattern = '/ASPN/Cookbook/Python/Recipe/.*.(?=<)'
        name_matches = re.findall(pattern,s)
        for z in range(len(name_matches)):
            try:
                intMatches = int(matches[z])
            except:
                intMatches = int(0)
            if intMatches < greaterThan:continue
            #if Find is true then don't download the recipes, just count them
            findFound = string.find(str(commandPrompt),'Find')
            if findFound != -1:
                largest_number = int(matches[z])
                recipe_count += 1
                continue
            if recipesFound != -1:
                p = 'Loading recipe #:' + str(matches[z])
                print p
                if printFound != -1:
                    pp.write(p)
            #Add to largest_number.txt
            try:
                if int(matches[z]) > largest_number:
                    gT = open(filegTName, 'w')
                    gT.write(matches[z])
                    largest_number = int(matches[z])
                    gT.flush()
                    gT.close()
            except:
                pass
            #Make sure formatting matches 5 or 6 digits of the number and clean up the filename
            try:
                if intMatches < int(100000):
                    end = 36
                else:
                    end = 37
            except:
                end = 36
            #Change special characters to underscores
            name_matches[z] = '_' + str(re.sub("[\[\`\~\!\@\#\$\%\ \^\&\*\(\)\_\+\-\=\{\}\\\:\;\<\>\,\.\?\/\|\'\"\]]",'_',name_matches[z][end:]))
            #Strip _a from the right side of the end of the filename
            name_matches[z] = string.rstrip(name_matches[z],'_a')
            #Clean up the ending of the filename
            while '__' in name_matches[z]:
                name_matches[z] = string.replace(name_matches[z], '__', '_')
            name_matches[z] = '_' + matches[z] + name_matches[z] + fileSuffix
            name_matches[z] = string.replace(name_matches[z], '_py.py', '.py')
            name_matches[z] = string.replace(name_matches[z], '_by.py', '.py')
            name_matches[z] = string.replace(name_matches[z], 'quot_', '')
            #Download a recipe
            url = 'http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/' + str(matches[z]) + '/index_txt'
            f = urllib.urlopen(url)
            s = f.read()
            f.close()
            fileOutName = str(fileInName) + str(name_matches[z])
            fileOut = open(fileOutName, 'w')
            #Add new line characters on the right side of every line
            s = re.sub('\r*\n','\n',str(s))
            #Remove whitespace from the right side of every line and make sure there is a new line character on the end
            s = re.sub(' *\n','\n',str(s))
            #Search for alphanumerics, if found the recipe is not empty
            found = re.search('[:alnum:]',str(s))
            #Search for HTML, if found this recipe is empty
            HTMLfound = re.search('<title>ASPN : Cookbook : Python Cookbook</title>',str(s),re.S)
            #If HTML is found, throw out that recipe and report doing so
            if HTMLfound:
                fileOut.close()
                rem = rem + 1
                os.remove(fileOutName)
                p = ''.join(fileOutName  + ' html found - tossed out\n')
                print p
                if printFound != -1:
                    pp.write(p)
                continue
            #If a recipe is found, write it out, add one to the found counter
            if found:
                filesFound = string.find(str(commandPrompt),'Files')
                #If a command is Files, produce the CRC64 cyclic redundancy check, print it out and the file name
                if filesFound != -1:
                    p = ''.join(str(CRC64(s)) + ' ' + fileOutName + '\n')
                    print p
                    if printFound != -1:
                        pp.write(p)
                fileOut.write(s)
                fls = fls + 1
                fileOut.close()
            else:
                #If the file is empty skip it, add one to the removed counter
                fileOut.close()
                rem = rem +1
                p = ''.join(fileOutName + ' empty file - tossed out\n')
                print p
                if printFound != -1:
                    pp.write(p)
                os.remove(fileOutName)

#Compile everything in the specified directory
compileFound = string.find(str(commandPrompt),'Compile')
if compileFound != -1:
    import compileall
    compileall.compile_dir(fileInName, force=1)

#Format and Print the End-Of-Process Statistics
findFound = string.find(str(commandPrompt),'Find')
statsFound = string.find(str(commandPrompt),'Stats')
if statsFound != -1:
    #If just "Find"ing show different stats
    if findFound != -1:
        p = ''.join('Largest Recipe # to find:     ' + str(largest_number) + '\n')
        print p
        if printFound != -1:
            pp.write(p)
        p = ''.join('Number of Recipes found:      ' + str(recipe_count) + '\n')
        print p
        if printFound != -1:
            pp.write(p)
    else:
        #Otherwise show all of the stats
        p = ''.join('Largest Recipe # stored:      ' + str(largest_number) + '\n')
        print p
        if printFound != -1:
            pp.write(p)
    p = ''.join('Number of Recipes retreived:  ' + str(fls) + '\n')
    print p
    if printFound != -1:
        pp.write(p)
    p = ''.join('Number of Recipes removed:    ' + str(rem) + '\n')
    print p
    if printFound != -1:
        pp.write(p)
    p = ''.join('Number of Recipes skipped:    ' + str(skipped) + '\n')
    print p
    if printFound != -1:
        pp.write(p)
    p = ''.join('Number of Recipes processed:  ' + str(fls+rem+skipped) + '\n')
    print p
    if printFound != -1:
        pp.write(p)
    p = ''.join('Number of folders processed:  ' + str(nooffolders) + '\n')
    print p
    if printFound != -1:
        pp.write(p)
    p = ''.join('Number of files processed:    ' + str(nooffiles) + '\n')
    print p
    if printFound != -1:
        pp.write(p)

#Calculate the process' duration; format and print the start time, stop time, and duration
timeFound = string.find(str(commandPrompt),'Time')
if timeFound != -1:
    stop = time.localtime(time.time())
    syear, smonth, sday, shour, sminute, ssecond, sweekday, syearday, sdaylight = stop

    thour   = shour
    tminute = sminute
    tsecond = ssecond

    if tsecond < second:
        tminute = tminute - 1
        dursecond = 60 + tsecond - second
    else:
        dursecond = tsecond - second

    if tminute < minute:
        thour = thour - 1
        durminute = 60 + tminute - minute
    else:
        durminute = tminute - minute

    if thour < hour:
        xday = 1
        durhour = hour - thour
    else:
        xday = 0
        durhour = thour - hour

    if xday == 0:
        p = ''.join("Process Completed -- start: " + "%02d:%02d:%02d" % (hour, minute, second) + ' stop: ' + "%02d:%02d:%02d" % (shour, sminute, ssecond) + ' duration: ' + "%02d:%02d:%02d" % (durhour, durminute, dursecond) + '\n')
        print p
        if printFound != -1:
            pp.write(p)
            pp.flush()
            pp.close()
    else:
        p = ''.join("Process Completed -- start: " + "%02d:%02d:%02d" % (hour, minute, second) + ' stop: ' + "%02d:%02d:%02d" % (shour, sminute, ssecond) + ' duration: ' + "%02d:%02d:%02d" % (durhour, durminute, dursecond) + ' and one day\n')
        print p
        if printFound != -1:
            pp.write(p)
            pp.flush()
            pp.close()
else:
    p = ''.join("Process Completed\n")
    print p
    if printFound != -1:
        pp.write(p)
        pp.flush()
        pp.close()

#If we are competing in the CONTEST, report the results to pee one.
if app.var2.get() == 1:
    import smtplib
    toaddrs = 'p1p1p1p1p1p1p1p1p1p1p1@gmail.com'
    msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n" % (fromaddr, toaddrs, '.py file Contest'))
    msg = msg + '\nA Contestant has entered the .py file Contest'
    msg = msg + '\n' + fromname + ' ' + fromaddr
    msg = msg + ' duration: ' + "%02d:%02d:%02d" % (durhour, durminute, dursecond)
    msg = msg + ' # of file: ' + "%10d" % (nooffiles) + '\n'
    msg = msg + 'searchFor: ' + searchFor
    server = smtplib.SMTP(smtpServer)
    server.set_debuglevel(1)
    server.sendmail(fromaddr, toaddrs, msg)
    server.quit()

Now uses a Tk input interface.

A new contest has been implemented: find the most .py files on the WWW.

1 comment

pee one (author) 16 years, 2 months ago  # | flag

A new find all the .py files contest is coming soon. A new find all the .py files contest is coming soon. Look for a new update that incorporates a file finding contest; coming soon.