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

Commit

Permalink
Always use vanity short URLs
Browse files Browse the repository at this point in the history
Forcing j.mp or bit.ly for vanity short URLs seems to no longer be supported by Bitly
  • Loading branch information
impredicative committed Oct 21, 2020
1 parent 5731d1f commit 3d412fe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,29 @@ import bitlyshortener
# Setup
tokens_pool = ['9fbe2864bb8872f5027c103321ff91be90aea687', '0cbe3864bc8872f5027c103321ff91be30aea787'] # Use your own.
shortener = bitlyshortener.Shortener(tokens=tokens_pool, max_cache_size=256)
vanity_shortener = bitlyshortener.Shortener(tokens=tokens_pool, max_cache_size=256, vanitize=True) # vanity=True keeps custom vanity domains when available.

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

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

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

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

# Show cache info
shortener.cache_info
{'Shortener._shorten_url': CacheInfo(hits=0, misses=9, maxsize=256, currsize=9)}
{'Shortener._shorten_url': CacheInfo(hits=4, misses=10, maxsize=128, currsize=10)}
```

To obtain the fastest response, URLs must be shortened together in a batch as in the examples above.
Expand Down
13 changes: 3 additions & 10 deletions bitlyshortener/shortener.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
class Shortener:
"""Shortener."""

def __init__(self, *, tokens: List[str], max_cache_size: int = config.DEFAULT_CACHE_SIZE, vanitize: bool = False):
def __init__(self, *, tokens: List[str], max_cache_size: int = config.DEFAULT_CACHE_SIZE):
self._tokens = tokens
self._max_cache_size = max_cache_size
self._vanitize = vanitize
self._check_args()
self._tokens = sorted(self._tokens) # Sorted for subsequent reproducible randomization.

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

# Check vanitize
vanity_status = "enabled" if self._vanitize else "disabled"
log.debug("Vanity domains are %s.", vanity_status)
# Ref: https://support.bitly.com/hc/en-us/articles/230558107-What-is-a-Custom-Domain-

@staticmethod
def _check_long_urls(long_urls: List[str]) -> None:
if not (isinstance(long_urls, list) and all(isinstance(long_url, str) for long_url in long_urls)):
Expand Down Expand Up @@ -113,8 +107,7 @@ def _shorten_url(self, long_url: str) -> str: # pylint: disable=too-many-locals
long_url = long_url.strip()
if self._is_known_short_url(long_url):
# Note: A preexisting Bitly link can use one of many domains, not just j.mp. It can also be
# a custom link or not. A custom link cannot be encoded to an integer for caching. Such a link
# must be validated and normalized.
# a custom link or not. Such a link must be validated and normalized.
long_url = self._lengthen_url(long_url)

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

Expand Down

0 comments on commit 3d412fe

Please sign in to comment.