view 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
line wrap: on
line source

#!/usr/bin/env python

"""
get the human-form of the name of the final path segment in a url:

xclip -o | sed 's/_//' | sed 's/.html//'
"""

import argparse
import sys

def url2txt(url):
    """gets the text equivalent of a URL"""
    url = url.rstrip('/')
    if '/' in url:
        url = url.rsplit('/')[-1]
    if '.' in url:
        url = url.split('.', 1)[0]
    url = url.replace('_', ' ')
    return url


def main(args=sys.argv[1:]):
    """CLI"""

    # parse command line
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_option('urls', metavar='url', nargs='+',
                      help="URLs to convert")
    options = parser.parse_args(args)

    # convert urls
    for url in options.urls:
        print (url2txt(url))


if __name__ == '__main__':
    main()