Skip to content

Commit 394a537

Browse files
Added url shortener service
1 parent 0cf50ec commit 394a537

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

url-shortener-api/urlshortener.py

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
from pyshorteners import Shortener
2+
import webbrowser
3+
import clipboard
4+
import time
5+
import tinyurl
6+
import urllib2
7+
8+
#------------------------HELPER UTILITIES--------------------
9+
10+
def getOnlyQRCode(shortened_url):
11+
url = "http://chart.apis.google.com/chart?cht=qr&chl={}&chs=120x120".format(shortened_url)
12+
print "\n\tQRCode is on the URL: {}".format(url)
13+
webbrowser.open_new_tab(url)
14+
time.sleep(2)
15+
16+
17+
def printShortener(shortener, url):
18+
#print "\n\tinside print."
19+
try:
20+
getshorted = shortener.short(url)
21+
print "\n\tShortened url is {}".format(getshorted)
22+
clipboard.copy(getshorted)
23+
print "\n\tDone, your shortened url is on clipboard.!"
24+
print "\n\tLaunch your browser and use 'Command-V' OR 'Ctrl + V' to paste the link in your browser."
25+
26+
time.sleep(5)
27+
28+
print "\n\tWant to Fetch QRCode? press 1 else press 0"
29+
30+
choice=int(input("\n\t"))
31+
32+
if choice == 1:
33+
getQRCode(shortener,url)
34+
elif choice == 0:
35+
return
36+
else:
37+
print "Error!"
38+
return
39+
40+
except Exception as e:
41+
print str(e)
42+
43+
44+
def getQRCode(shortener, url):
45+
shortener.short(url)
46+
print "\n\tQRCode is on the URL: {}".format(shortener.qrcode())
47+
try:
48+
webbrowser.open_new_tab(shortener.qrcode())
49+
time.sleep(2)
50+
except Exception as e:
51+
print "\n\tLaunch your browser and use 'Command-V' OR 'Ctrl + V' to paste the link in your browser."
52+
clipboard.copy(shortener.short(url))
53+
time.sleep(2)
54+
55+
#you could also save your qrcode locally by simply calling urllib on the image url
56+
57+
58+
59+
#----------------------- MAIN FUNCTIONS --------------------
60+
61+
def googleShortener(url):
62+
key = 'AIzaSyAkWqqAmotOf98k421TC3PetlPbeZlXoEA'
63+
shortener = Shortener('Google', api_key = key)
64+
printShortener(shortener,url)
65+
66+
def bitlyShortener(url):
67+
access_token = '03cf036ff2a4aa31b93c369af9e33478ddd44f02'
68+
shortener = Shortener('Bitly', bitly_token = access_token)
69+
printShortener(shortener,url)
70+
71+
def tinyurlShortener(url):
72+
shortened = tinyurl.create_one(url)
73+
print "\n\tShortened url is {}".format(shortened)
74+
clipboard.copy(shortened)
75+
print "\n\tDone, your shortened url is on clipboard.!"
76+
print "\n\tLaunch your browser and use 'Command-V' OR 'Ctrl + V' to paste the link in your browser."
77+
time.sleep(5)
78+
print "\n\tWant to Fetch QRCode? press 1 else press 0"
79+
choice=int(input("\n\t"))
80+
if choice == 1:
81+
getOnlyQRCode(shortened)
82+
elif choice == 0:
83+
return
84+
else:
85+
print "Error!"
86+
return
87+
88+
89+
90+
def isgdShortener(tourl):
91+
url = 'https://is.gd/create.php?format=simple&url={}'.format(tourl)
92+
shortened = urllib2.urlopen(url).read()
93+
print "\n\tShortened url is {}".format(shortened)
94+
clipboard.copy(shortened)
95+
print "\n\tDone, your shortened url is on clipboard.!"
96+
print "\n\tLaunch your browser and use 'Command-V' OR 'Ctrl + V' to paste the link in your browser."
97+
time.sleep(5)
98+
print "\n\tWant to Fetch QRCode? press 1 else press 0"
99+
choice=int(input("\n\t"))
100+
if choice == 1:
101+
getOnlyQRCode(shortened)
102+
elif choice == 0:
103+
return
104+
else:
105+
print "Error!"
106+
return
107+
108+
'''
109+
def adflyShortener(tourl):
110+
UID = 18844965
111+
API_KEY = 'd8a2283a6bbafbe31b442776fdc108ab'
112+
url = 'http://api.adf.ly/api.php?key={}&uid={}&advert_type=int&domain=adf.ly&url={}'.format(API_KEY,UID,tourl)
113+
r = urllib2.urlopen(url).read()
114+
print r
115+
'''
116+
117+
def main():
118+
119+
print "\n\tList of URL Shortener Services:\n\t\t1. Google\n\t\t2. Bit.ly\n\t\t3. TinyURL\n\t\t4. IS.GD\n\t\t"
120+
try:
121+
choice = int(raw_input("\n\tChoose URL SHORTENER service: "))
122+
print "\n\tTo enter url, you can type manually in your console or else you can copy the url using 'Command-V' or 'Ctrl + V'\n\tfrom browser."
123+
print "\n\t1. Manually in console\n\t2. Copy from browser\t"
124+
urlchoice = int(raw_input("\n\tEnter choice: "))
125+
126+
if urlchoice == 1:
127+
print "\n\tEnter url to be shortened: ",
128+
129+
url = str(raw_input(""))
130+
elif urlchoice == 2:
131+
print "\tYou have five seconds..copy the url from address bar you wish to shorten!"
132+
time.sleep(5)
133+
url = clipboard.paste()
134+
else:
135+
print "\n\tInvalid Option.! Quitting.."
136+
time.sleep(1)
137+
sys.exit(0)
138+
139+
if choice == 1:
140+
googleShortener(url)
141+
142+
elif choice == 2:
143+
bitlyShortener(url)
144+
145+
elif choice == 3:
146+
tinyurlShortener(url)
147+
148+
elif choice == 4:
149+
isgdShortener(url)
150+
151+
else:
152+
print "Invalid Service."
153+
154+
except Exception as e:
155+
print str(e)
156+
157+
158+
if __name__ == '__main__':
159+
main()

0 commit comments

Comments
 (0)