From f6380b85d8f296b0c16ffa12073bce86e296dc56 Mon Sep 17 00:00:00 2001 From: sarthaks-ss <58469335+sarthaks-ss@users.noreply.github.com> Date: Sat, 30 Oct 2021 00:05:37 +0530 Subject: [PATCH] Create url_shortner.py --- url_shortner.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 url_shortner.py diff --git a/url_shortner.py b/url_shortner.py new file mode 100644 index 0000000..a00bdb6 --- /dev/null +++ b/url_shortner.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(): + urls = input("Enter a URL: ") + print(make_tiny(urls)) + +if __name__ == '__main__': + main()