From 352bdc8c92bae5e651d68667184a2050c22befc0 Mon Sep 17 00:00:00 2001 From: lakshaysharma12 <91069696+lakshaysharma12@users.noreply.github.com> Date: Sat, 30 Oct 2021 22:15:48 +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..4a5724f --- /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()