File: pypi.py

package info (click to toggle)
python-mechanize 1%3A0.4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,224 kB
  • sloc: python: 16,718; makefile: 23
file content (32 lines) | stat: -rw-r--r-- 928 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

# Search PyPI, the Python Package Index, and retrieve latest mechanize tarball.

# This is just to demonstrate mechanize: You should use easy_install to do
# this, not this silly script.

import sys
import os

import mechanize
from mechanize.polyglot import urlsplit


def download_mechanize():
    browser = mechanize.Browser(factory=mechanize.RobustFactory())
    browser.set_handle_robots(False)

    browser.open("http://pypi.python.org/pypi")
    browser.select_form(name="searchform")
    browser.form["term"] = "mechanize"
    browser.submit()
    browser.follow_link(text_regex="mechanize-?(.*)")
    link = browser.find_link(text_regex=r"\.tar\.gz")
    filename = os.path.basename(urlsplit(link.url)[2])
    if os.path.exists(filename):
        sys.exit("%s already exists, not grabbing" % filename)
    browser.retrieve(link.url, filename)


if __name__ == "__main__":
    download_mechanize()