-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitly_py2.py
executable file
·52 lines (43 loc) · 1.45 KB
/
bitly_py2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# access_token can be used alone, while apiKey needs the login argument.
# The user/info endpoint for example doesn’t work with apiKey at all.
import sys
from json import loads
from pprint import pprint
from urllib import urlencode, urlopen
# Set a minimum of API variables
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
API_KEY = "YOUR_API_KEY"
LOGIN = "YOUR_LOGIN"
LONGURL = "URL"
USE_PPRINT = False
# Check if the API variables are provided or not
if ACCESS_TOKEN == "" or API_KEY == "" or LOGIN == "" or LONGURL == "":
print "Please fill out all parameters."
sys.exit(1)
else:
pass
query_params = ({"access_token": ACCESS_TOKEN,
"apiKey": API_KEY,
"login": LOGIN,
"longUrl": LONGURL})
query_params = urlencode(query_params)
url = "https://api-ssl.bitly.com/v3/"
endpoint = raw_input("Please enter a bitly endpoint, e.g. ‘shorten’: ")
url = url + endpoint
response = urlopen('%s?%s' % (url, query_params)).read()
jsoncontent = loads(response)
# Decide whether to use print or pprint
if url == "https://api-ssl.bitly.com/v3/shorten":
if USE_PPRINT == False:
print "The shortened link of " + LONGURL + " is:"
print jsoncontent["data"]["url"]
else:
print "The shortened link of " + LONGURL + " is:"
pprint(jsoncontent["data"]["url"])
else:
if USE_PPRINT == False:
print jsoncontent
else:
pprint(jsoncontent)