Created
August 24, 2017 15:05
-
-
Save VISWESWARAN1998/24af6f2aa01f43f84c6850718593deb3 to your computer and use it in GitHub Desktop.
Shorten the url using bitly and Python 3.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SWAMI KARUPPASWAMI THUNNAI | |
import urllib.request | |
import json | |
class Shorterner: | |
""" | |
This class is used to shorten the long urls | |
""" | |
__genericAccessToken = None | |
__url = None # This is the actual url supplied by the user | |
__encoded_url = None | |
__shortened_url = None | |
__status_code = None | |
__status_txt = None | |
__bitly_url = None | |
__access_token = None | |
# set the access token via constructor | |
def __init__(self,token,url): | |
self.__genericAccessToken = token | |
self.__url = url | |
self.__access_token = token | |
# now we will construct the url | |
# now we must perform url encoding | |
self.__url = self.__url.replace(" ","%20") | |
self.__url = self.__url.replace("!","%21") | |
self.__url = self.__url.replace("#","%23") | |
self.__url = self.__url.replace("$","%24") | |
self.__url = self.__url.replace("&","%26") | |
self.__url = self.__url.replace("'","%27") | |
self.__url = self.__url.replace("(","%28") | |
self.__url = self.__url.replace(")","%29") | |
self.__url = self.__url.replace("*","%2A") | |
self.__url = self.__url.replace("+","%2B") | |
self.__url = self.__url.replace(",","%2C") | |
self.__url = self.__url.replace("/","%2F") | |
self.__url = self.__url.replace(":","%3A") | |
self.__url = self.__url.replace(";","%3B") | |
self.__url = self.__url.replace("=","%3D") | |
self.__url = self.__url.replace("?","%3F") | |
self.__url = self.__url.replace("@","%40") | |
self.__url = self.__url.replace("[","%5B") | |
self.__url = self.__url.replace("]","%5D") | |
# now assign the encoded url | |
self.__encoded_url = "https://api-ssl.bitly.com/v3/shorten?access_token="+self.__access_token+"&longUrl="+self.__url | |
# This method is used to start shortening the url | |
def start(self): | |
if self.__access_token == None: | |
print("[*] Acceess Token is needed! Please Update the code with a token") | |
response = urllib.request.urlopen(self.__encoded_url) | |
result = response.read() | |
# we will receive the data as bytes and we will decode it to string | |
result = result.decode("utf-8") | |
data = json.loads(result) | |
self.__status_code = data['status_code'] # set the status code | |
self.status_txt = data['status_txt'] # set the status text | |
self.__bitly_url = data['data']['url'] | |
# This method is used to get the shortened url | |
def get_shortened_url(self): | |
return self.__bitly_url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You may ask:
How to use this to shorten the ur???
Just download the code and in your program add these lines
shorten = Shorterner(access_token,final_url) # final_url is your url which is to be shortened
shorten.start()
result = shorten.get_shortened_url() # you will get the shortened url
Offlicial bitly_api client seems to be incompatible for various Python 3.x versions and many users have posted the issue which is still not solved see here: [https://github.com/bitly/bitly-api-python/issues/39] so why I came across with my own solution. Hope it helps some one!
Kind regards,
Visweswaran Nagasivam