File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
APIScripts/URLShortener API Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
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 ("\n Enter 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 ("\n Short url is : " ,tinyurl ,"\n " )
29
+
30
+ if __name__ == '__main__' :
31
+ main ()
You can’t perform that action at this time.
0 commit comments