vget.py (2120B)
1 #!/usr/bin/python3 2 3 # This script is meant as a client for 'python3 -m http.server' 4 # This script either gets a given resource or returns autocomplete suggestions 5 # (with flag -l) based on the input argument the contents detected on the server 6 # 7 # Setup: To install and set up bash completion, run: 8 # ln -s $PWD/vget.py /usr/bin/vget 9 # and add the following to /usr/share/bash-completion/completions/vget: 10 # _vget() 11 # { 12 # COMPREPLY=($(vget -l ${COMP_WORDS[1]})) 13 # } 14 # complete -o nospace -F _vget vget 15 16 import re 17 import requests 18 import argparse 19 20 # Host aliases go here 21 DEFAULT_HOST='me' 22 HOST_URLS={ 23 'me': 'http://localhost:8000', 24 'vetle': 'http://localhost:8000' 25 } 26 27 28 parser = argparse.ArgumentParser() 29 parser.add_argument('url', type=str, nargs='?') 30 parser.add_argument("-l", dest='list_only', action='store_true', 31 help="Actually download the resource, don't get list") 32 parser.set_defaults(list_only=False) 33 args = parser.parse_args() 34 url = args.url 35 download = not args.list_only 36 37 # TODO: Allow no arguments, and return the entire list of aliases if so 38 39 # Split the URL in host and path 40 # If the URL doesn't contain a path, it should be a host alias only 41 if not url: 42 print(' '.join(HOST_URLS.keys())) 43 exit() 44 if 'http' in url[:4]: 45 pass # in this case the url does not contain an alias 46 if '/' in url: 47 alias = url.split('/')[0] 48 host = HOST_URLS[alias] 49 path = url.replace(alias, '') 50 url = host + path 51 elif url in HOST_URLS: 52 alias = url 53 host = HOST_URLS[url] 54 path = '/' 55 url = host + path 56 else: 57 print(' '.join(alias for alias in HOST_URLS if url in alias)) 58 exit() 59 60 if download: 61 r = requests.get(url) 62 filename = url.split('/')[-1] 63 open(filename, 'wb').write(r.content) 64 print('--> ', filename) 65 exit() 66 67 # Otherwise download the list for autocompletion 68 incomplete = path[path.rindex('/')+1:] 69 path = path[:path.rindex('/')] + '/' 70 url = host + path 71 html = requests.get(url).text 72 resources = re.findall('<a.+>(.+)</a>', html) 73 resources = [alias+path+r for r in resources if incomplete in r] 74 print(' '.join(resources))