URL shortening with Python

I tried shortening the URL with Python.

First, you need to use the API to generate a shortened URL Get the API key. Please create a Token on the site of the following URL. https://bitly.com/a/oauth_apps

If you get it there, you can put it in the variable access_token in the main function (```if name == "main" `` `) of the code below. After that, specify the URL to be shortened with the command line argument, and you're done.

Execution example

von:~ von$ python /Users/von/Desktop/URL_Shortening.py https://www.youtube.com/

Output example

http://bit.ly/24eSWTH

URL_Shortening.py


def GET_Url(access_token, longUrl):
	import urllib, urllib2, json
	url = 'https://api-ssl.bitly.com/v3/shorten'
	values = {
			"access_token": access_token,
			"longUrl": longUrl
			}
	req = urllib2.Request(url, urllib.urlencode(values))
	result = json.loads(urllib2.urlopen(req).read())
	return result["data"]["url"]

if __name__ == "__main__":
	import sys
	access_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
	print GET_Url(access_token, sys.argv[1])

Recommended Posts

URL shortening with Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Integrate with Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Serial communication with Python
Zip, unzip with python
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learn Python with ChemTHEATER
Run prepDE.py with python3
Collecting tweets with Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python
Solve Sudoku with Python
Heatmap with Python + matplotlib
Multi-process asynchronously with python
Python programming with Atom
Learning Python with ChemTHEATER 02
Use Python 3.8 with Anaconda
Competitive programming with python
Handle rabbimq with python
GRPC starting with Python
Install Voluptuous with Python 2.5
ScreenShot with Selenium (Python)