Skip to content

Commit

Permalink
Add a Python program to Shorten URL
Browse files Browse the repository at this point in the history
 * Using Tinyurl to shorten an url
  • Loading branch information
Amitava123 authored Sep 30, 2020
1 parent ef8d290 commit 830edd1
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Language/Python/urlShortener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# A python program to use TinyURL to shorten an given URL
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

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():
url = input()
print(make_tiny(url))

if __name__ == '__main__':
main()

0 comments on commit 830edd1

Please sign in to comment.