diff --git a/README.md b/README.md index 2fb32ea..08c5f71 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,17 @@ python client.py python server.py ``` +* ### URL Shortener +This is python script that shortens any URL provided to it. + +```bash +# Takes multiple inputs and returns shortened URL for both +python shortener.py url1 url2 + +#Stores shortened URLs in a file +python shortener.py url1 url2 > file.txt +``` + * ### Video-downloader v1.1 ### About diff --git a/bin/shortener.py b/bin/shortener.py new file mode 100644 index 0000000..ad55e51 --- /dev/null +++ b/bin/shortener.py @@ -0,0 +1,24 @@ +from __future__ import with_statement +import contextlib +try: + from urllib.parse import urlencode +except ImportError: + from urllib import urlencode +try: + from urllib.request import urlopen +except ImportError: + from urllib2 import urlopen +import sys + +def make_tiny(url): + request_url = ('http://tinyurl.com/api-create.php?' + + urlencode({'url':url})) + with contextlib.closing(urlopen(request_url)) as response: + return response.read().decode('utf-8') + +def main(): + for tinyurl in map(make_tiny, sys.argv[1:]): + print(tinyurl) + +if __name__ == '__main__': + main()