21
|
1 #!/usr/bin/env python
|
|
2
|
|
3 """
|
|
4 get the human-form of the name of the final path segment in a url:
|
|
5
|
|
6 xclip -o | sed 's/_//' | sed 's/.html//'
|
|
7 """
|
|
8
|
38
|
9 import argparse
|
21
|
10 import sys
|
39
|
11 import urlparse
|
21
|
12
|
39
|
13 def url2txt(url, strip_extension=True, replacements=(('_', ' '),)):
|
21
|
14 """gets the text equivalent of a URL"""
|
39
|
15
|
|
16 # parse the url
|
|
17 parsed = urlparse.urlparse(url)
|
|
18
|
|
19 # process the path, if available
|
|
20 path = parsed.path.rstrip('/')
|
|
21 if path:
|
|
22 text = path.split('/')[-1]
|
|
23 if strip_extension:
|
|
24 # strip the extension, if desired
|
|
25 text = text.split('.', 1)[0]
|
|
26 else:
|
|
27 # otherwise go with the hostname
|
|
28 text = parsed.hostname
|
|
29
|
|
30 # replace desired items
|
|
31 for item, replacement in replacements:
|
|
32 text = text.replace(item, replacement)
|
|
33
|
|
34 return text
|
21
|
35
|
|
36
|
|
37 def main(args=sys.argv[1:]):
|
|
38 """CLI"""
|
38
|
39
|
|
40 # parse command line
|
|
41 parser = argparse.ArgumentParser(description=__doc__)
|
39
|
42 parser.add_argument('urls', metavar='url', nargs='+',
|
|
43 help="URLs to convert")
|
38
|
44 options = parser.parse_args(args)
|
|
45
|
|
46 # convert urls
|
|
47 for url in options.urls:
|
|
48 print (url2txt(url))
|
21
|
49
|
|
50
|
|
51 if __name__ == '__main__':
|
|
52 main()
|