-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyurl.py
50 lines (36 loc) · 1.14 KB
/
tinyurl.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
"""A really tiny interface to ``tinyurl.com``."""
import sys
import urllib
import optparse
API_CREATE = "http://tinyurl.com/api-create.php"
DEFAULT_DELIM = "\n"
USAGE = """%prog [options] url [url url ...]
""" + __doc__ + """
Any number of urls may be passed and will be returned
in order with the given delimiter, default=%r
""" % DEFAULT_DELIM
ALL_OPTIONS = (
(('-d', '--delimiter'),
dict(dest='delimiter', default=DEFAULT_DELIM,
help='delimiter for returned results')),
)
def _build_option_parser():
prs = optparse.OptionParser(usage=USAGE)
for args, kwargs in ALL_OPTIONS:
prs.add_option(*args, **kwargs)
return prs
def create_one(url):
url_data = urllib.urlencode(dict(url=url))
ret = urllib.urlopen(API_CREATE, data=url_data).read().strip()
return ret
def create(*urls):
for url in urls:
yield create_one(url)
def main(sysargs=sys.argv[:]):
parser = _build_option_parser()
opts, urls = parser.parse_args(sysargs[1:])
for url in create(*urls):
sys.stdout.write(url + opts.delimiter)
return 0
if __name__ == '__main__':
sys.exit(main())