annotate textshaper/url2txt.py @ 38:56fa70e2e239

STUB: textshaper/url2txt.py
author Jeff Hammel <k0scist@gmail.com>
date Thu, 03 Jul 2014 13:23:19 -0700
parents e6f680d25d63
children 986f8a20c234
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 get the human-form of the name of the final path segment in a url:
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 xclip -o | sed 's/_//' | sed 's/.html//'
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 """
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8
38
56fa70e2e239 STUB: textshaper/url2txt.py
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
9 import argparse
21
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import sys
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 def url2txt(url):
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 """gets the text equivalent of a URL"""
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 url = url.rstrip('/')
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 if '/' in url:
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 url = url.rsplit('/')[-1]
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 if '.' in url:
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 url = url.split('.', 1)[0]
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 url = url.replace('_', ' ')
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 return url
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 def main(args=sys.argv[1:]):
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 """CLI"""
38
56fa70e2e239 STUB: textshaper/url2txt.py
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
25
56fa70e2e239 STUB: textshaper/url2txt.py
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
26 # parse command line
56fa70e2e239 STUB: textshaper/url2txt.py
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
27 parser = argparse.ArgumentParser(description=__doc__)
56fa70e2e239 STUB: textshaper/url2txt.py
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
28 parser.add_option('urls', metavar='url', nargs='+',
56fa70e2e239 STUB: textshaper/url2txt.py
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
29 help="URLs to convert")
56fa70e2e239 STUB: textshaper/url2txt.py
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
30 options = parser.parse_args(args)
56fa70e2e239 STUB: textshaper/url2txt.py
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
31
56fa70e2e239 STUB: textshaper/url2txt.py
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
32 # convert urls
56fa70e2e239 STUB: textshaper/url2txt.py
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
33 for url in options.urls:
56fa70e2e239 STUB: textshaper/url2txt.py
Jeff Hammel <k0scist@gmail.com>
parents: 21
diff changeset
34 print (url2txt(url))
21
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 if __name__ == '__main__':
e6f680d25d63 migrate url2txt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 main()