Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Custom Bit.ly urls for Bitly Brand Tools customers #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions bitly_api/bitly_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ def search(self, query, offset=None, cities=None, domain=None, fields=None,
data = self._call_oauth2_metrics("v3/search", params, limit=limit)
return data['results']

def custom_keyword(self, keyword_link, target_link, overwrite=False):
""" Save a Custom Bitlink for a custom short domain.
@parameter keyword_link: the Custom Bitlink (short domain and keyword combination) to set
@parameter target_link: the Bitlink the specified keyword will map to (as returned from /v3/shorten)
@parameter overwrite: Optional. true|false. Overwrite existing entry if one exists. Default: false.
"""
params = dict(keyword_link=keyword_link, target_link=target_link, overwrite=overwrite)
data = self._call(self.host, 'v3/user/save_custom_domain_keyword', params, self.secret)
return data['data']

@classmethod
def _generateSignature(self, params, secret):
if not params or not secret:
Expand Down
15 changes: 15 additions & 0 deletions test/test_bitly_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"""
import os
import sys
import random
import string
sys.path.append('../')
import bitly_api

Expand Down Expand Up @@ -69,3 +71,16 @@ def testUserInfo():
data = bitly.user_info()
assert data is not None
assert 'login' in data

def testCustomKeyword():
bitly = get_connection()
data = bitly.shorten('http://google.com/')
short_url = data.get("url")

random_string = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(6))
custom_url = 'http://bit.ly/Custom%s' % random_string

new_data = bitly.custom_keyword(custom_url, short_url)
assert new_data is not None
assert new_data['target_link'] == 'http://google.com/'
assert new_data['keyword_link'] == custom_url