Skip to content
This repository was archived by the owner on Dec 17, 2022. It is now read-only.

Commit 3d412fe

Browse files
committed
Always use vanity short URLs
Forcing j.mp or bit.ly for vanity short URLs seems to no longer be supported by Bitly
1 parent 5731d1f commit 3d412fe

File tree

2 files changed

+7
-19
lines changed

2 files changed

+7
-19
lines changed

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,29 @@ import bitlyshortener
5656
# Setup
5757
tokens_pool = ['9fbe2864bb8872f5027c103321ff91be90aea687', '0cbe3864bc8872f5027c103321ff91be30aea787'] # Use your own.
5858
shortener = bitlyshortener.Shortener(tokens=tokens_pool, max_cache_size=256)
59-
vanity_shortener = bitlyshortener.Shortener(tokens=tokens_pool, max_cache_size=256, vanitize=True) # vanity=True keeps custom vanity domains when available.
6059

6160
# Shorten to list
6261
long_urls = ['https://www.amazon.com/gp/product/B07LFJMS2S/', 'https://www.cnn.com/election/2020', 'https://paperswithcode.com/sota']
6362
shortener.shorten_urls(long_urls)
64-
['https://j.mp/3jx2gft', 'https://j.mp/31IQt7D', 'https://j.mp/2YNKi01']
65-
vanity_shortener.shorten_urls(long_urls)
66-
['https://amzn.to/3jx2gft', 'https://cnn.it/31IQt7D', 'https://j.mp/2YNKi01']
63+
['https://amzn.to/2HcWFgV', 'https://cnn.it/3ofdpVp', 'https://j.mp/2IHwQ8P']
6764

6865
# Shorten to dict
6966
long_urls = ['https://news.google.com', 'https://yahoo.com/']
7067
shortener.shorten_urls_to_dict(long_urls)
71-
{'https://news.google.com': 'https://j.mp/2TzvYnq', 'https://yahoo.com/': 'https://j.mp/2TCihE4'}
68+
{'https://news.google.com': 'https://j.mp/31t9qL2', 'https://yahoo.com/': 'https://yhoo.it/3ondJS2'}
7269

7370
# Normalize diverse preexisting Bitly links
7471
urls = ['http://j.mp/2Bo2LVf', 'http://bit.ly/2BombJQ', 'https://cnn.it/2Ggb2ih', 'https://j.mp/websniffer']
7572
shortener.shorten_urls(urls)
76-
['https://j.mp/2BtckCt', 'https://j.mp/2BlS1qw', 'https://j.mp/2TEVtUt', 'https://j.mp/2BmjqbZ']
77-
vanity_shortener.shorten_urls(urls)
78-
['https://j.mp/3b9UzJ3', 'https://j.mp/31H7GhP', 'https://cnn.it/2YPARxb', 'https://j.mp/2EKXwDU']
73+
['https://j.mp/37qFvH0', 'https://j.mp/3obETLt', 'https://cnn.it/2FMI6jc', 'https://j.mp/37FmjFV']
7974

8075
# Show usage for tokens pool (cached for an hour)
8176
shortener.usage()
8277
0.4604 # Means that an average of 46% of the current calendar month's URL shortening quota has been used across all tokens.
8378

8479
# Show cache info
8580
shortener.cache_info
86-
{'Shortener._shorten_url': CacheInfo(hits=0, misses=9, maxsize=256, currsize=9)}
81+
{'Shortener._shorten_url': CacheInfo(hits=4, misses=10, maxsize=128, currsize=10)}
8782
```
8883

8984
To obtain the fastest response, URLs must be shortened together in a batch as in the examples above.

bitlyshortener/shortener.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
class Shortener:
2020
"""Shortener."""
2121

22-
def __init__(self, *, tokens: List[str], max_cache_size: int = config.DEFAULT_CACHE_SIZE, vanitize: bool = False):
22+
def __init__(self, *, tokens: List[str], max_cache_size: int = config.DEFAULT_CACHE_SIZE):
2323
self._tokens = tokens
2424
self._max_cache_size = max_cache_size
25-
self._vanitize = vanitize
2625
self._check_args()
2726
self._tokens = sorted(self._tokens) # Sorted for subsequent reproducible randomization.
2827

@@ -53,11 +52,6 @@ def _check_args(self) -> None:
5352
raise exc.ArgsError(f"Max cache size must be an integer ≥0, but it is {max_cache_size}.")
5453
log.debug("Max cache size is %s.", max_cache_size)
5554

56-
# Check vanitize
57-
vanity_status = "enabled" if self._vanitize else "disabled"
58-
log.debug("Vanity domains are %s.", vanity_status)
59-
# Ref: https://support.bitly.com/hc/en-us/articles/230558107-What-is-a-Custom-Domain-
60-
6155
@staticmethod
6256
def _check_long_urls(long_urls: List[str]) -> None:
6357
if not (isinstance(long_urls, list) and all(isinstance(long_url, str) for long_url in long_urls)):
@@ -113,8 +107,7 @@ def _shorten_url(self, long_url: str) -> str: # pylint: disable=too-many-locals
113107
long_url = long_url.strip()
114108
if self._is_known_short_url(long_url):
115109
# Note: A preexisting Bitly link can use one of many domains, not just j.mp. It can also be
116-
# a custom link or not. A custom link cannot be encoded to an integer for caching. Such a link
117-
# must be validated and normalized.
110+
# a custom link or not. Such a link must be validated and normalized.
118111
long_url = self._lengthen_url(long_url)
119112

120113
# Provision attempts
@@ -185,7 +178,7 @@ def _shorten_url(self, long_url: str) -> str: # pylint: disable=too-many-locals
185178
# Postprocess short URL
186179
if short_url.startswith("http://"): # Example: http://citi.us/2FPqsuZ
187180
short_url = short_url.replace("http://", "https://", 1)
188-
if (not self._vanitize) or (self._vanitize and short_url.startswith("https://bit.ly/")):
181+
if short_url.startswith("https://bit.ly/"):
189182
url_id = short_url.rpartition("/")[-1]
190183
short_url = f"https://j.mp/{url_id}"
191184

0 commit comments

Comments
 (0)