Skip to content

Commit 53524d7

Browse files
committed
Python code
1 parent 3e78af5 commit 53524d7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
from __future__ import with_statement
3+
4+
# importing contextlib module as it contains ContextManager class
5+
import contextlib
6+
7+
try:
8+
from urllib.parse import urlencode # For parsing the URLs
9+
except ImportError:
10+
from urllib import urlencode
11+
try:
12+
from urllib.request import urlopen # For opening and closing URLs
13+
except ImportError: # Convert a mapping object to a percent-encoded ASCII text string
14+
from urllib2 import urlopen
15+
import sys
16+
17+
def make_tiny(url): # Converting the given url to short url
18+
request_url = ('http://tinyurl.com/api-create.php?' +
19+
urlencode({'url':url}))
20+
with contextlib.closing(urlopen(request_url)) as response:
21+
return response.read().decode('utf-8')
22+
23+
def main():
24+
s=input("\nEnter the url to convert: ") # Input the url to be converted
25+
l=[]
26+
l.append(s)
27+
for tinyurl in map(make_tiny, l[0:]): # Prints the converted url
28+
print("\nShort url is : ",tinyurl,"\n")
29+
30+
if __name__ == '__main__':
31+
main()

0 commit comments

Comments
 (0)