| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import print_function |
| 9 |
import sys |
| 10 |
import os |
| 11 |
import re |
| 12 |
|
| 13 |
import mechanize |
| 14 |
from mechanize.polyglot import HTTPError |
| 15 |
|
| 16 |
mech = mechanize.Browser() |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
mech.set_handle_robots(False) |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
try: |
| 25 |
mech.open("http://search.cpan.org") |
| 26 |
except HTTPError as e: |
| 27 |
sys.exit("%d: %s" % (e.code, e.msg)) |
| 28 |
|
| 29 |
|
| 30 |
mech.select_form(nr=0) |
| 31 |
mech["query"] = "Lester" |
| 32 |
mech["mode"] = ["author"] |
| 33 |
try: |
| 34 |
mech.submit() |
| 35 |
except HTTPError as e: |
| 36 |
sys.exit("post failed: %d: %s" % (e.code, e.msg)) |
| 37 |
|
| 38 |
|
| 39 |
try: |
| 40 |
mech.follow_link(text_regex=re.compile("Andy")) |
| 41 |
except HTTPError as e: |
| 42 |
sys.exit("post failed: %d: %s" % (e.code, e.msg)) |
| 43 |
|
| 44 |
|
| 45 |
urls = [link.absolute_url for link in |
| 46 |
mech.links(url_regex=re.compile(r"\.tar\.gz$"))] |
| 47 |
print("Found", len(urls), "tarballs to download") |
| 48 |
|
| 49 |
if "--all" not in sys.argv[1:]: |
| 50 |
urls = urls[:1] |
| 51 |
|
| 52 |
for url in urls: |
| 53 |
filename = os.path.basename(url) |
| 54 |
f = open(filename, "wb") |
| 55 |
print("%s -->" % filename, end=' ') |
| 56 |
r = mech.open(url) |
| 57 |
while 1: |
| 58 |
data = r.read(1024) |
| 59 |
if not data: |
| 60 |
break |
| 61 |
f.write(data) |
| 62 |
f.close() |
| 63 |
print(os.stat(filename).st_size, "bytes") |