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

Commit

Permalink
Stop rewriting bit.ly with j.mp because this suddenly doesn't seem to…
Browse files Browse the repository at this point in the history
… work anymore
  • Loading branch information
impredicative committed Jan 19, 2022
1 parent a5e8cd1 commit b307d70
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ The following have historically been the rate limits per token:
* Per hour: 1000 (presumably for status 200 or 201)
* Per month: 1000 (presumably for status 201 only)

Bitly sends a monthly email if if 50% of the account's usage limit for new short links is exceeded for the calendar month.
Bitly sends a monthly email if 50% of the account's usage limit for new short links is exceeded for the calendar month.
If this email is received, it is suggested to immediately obtain and add additional tokens to the pool used by this package.
As follows, it is preferable to stay under 50% of the usage limit by having a sufficiently big pool of tokens.
It is possible to monitor the usage via the **`.usage()`** method as shown in the examples.

It is unknown what the per-IP rate limit is, if any.

### Python
Python 3.7+ is required.
Python 3.7 is required.
Any older version of Python will not work due to the use of
[`ThreadPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor)
with an *initializer*.
Expand All @@ -62,19 +62,19 @@ Usage examples:
# 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://amzn.to/2HcWFgV', 'https://cnn.it/3ofdpVp', 'https://j.mp/2IHwQ8P']
['https://amzn.to/3Inxf9V', 'https://cnn.it/3FKKZd8', 'https://bit.ly/3tLlp5w']

# 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/31t9qL2', 'https://yahoo.com/': 'https://yhoo.it/3ondJS2'}
{'https://news.google.com': 'https://bit.ly/3IjSObD', 'https://yahoo.com/': 'https://yhoo.it/2BiHgp8'}

# Normalize diverse preexisting Bitly links
>>> urls = ['http://j.mp/2Bo2LVf', 'http://bit.ly/2BombJQ', 'https://cnn.it/2Ggb2ih', 'https://j.mp/websniffer']
>>> urls = ['http://bit.ly/3Ad49Hw', 'http://j.mp/2Bo2LVf', 'https://cnn.it/3FKKZd8', 'https://j.mp/websniffer']
>>> shortener.shorten_urls(urls)
['https://j.mp/37qFvH0', 'https://j.mp/3obETLt', 'https://cnn.it/2FMI6jc', 'https://j.mp/37FmjFV']
['https://bit.ly/3Ad49Hw', 'https://bit.ly/3KjocZw', 'https://cnn.it/3FKKZd8', 'https://bit.ly/3nINKph']

# Show usage for tokens pool (cached for an hour)
# Show usage for tokens pool (is 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.

Expand Down
5 changes: 1 addition & 4 deletions bitlyshortener/shortener.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _shorten_url(self, long_url: str) -> str: # pylint: disable=too-many-locals
# Preprocess long URL
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
# Note: A preexisting Bitly link can use one of many domains, not just bit.ly. It can also be
# a custom link or not. Such a link must be validated and normalized.
long_url = self._lengthen_url(long_url)

Expand Down Expand Up @@ -180,9 +180,6 @@ 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 short_url.startswith("https://bit.ly/"):
url_id = short_url.rpartition("/")[-1]
short_url = f"https://j.mp/{url_id}"

log.debug("Returning short URL %s for long URL %s.", short_url, long_url)
return short_url
Expand Down
4 changes: 1 addition & 3 deletions scripts/try_shortener.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@
"https://arxiv.org/abs/1902.00541v1",
]

# URLs = ["https://j.mp/websniffer", "http://j.mp/2Bo2LVf", "http://bit.ly/2BombJQ", "https://cnn.it/2Ggb2ih"]

try:
shortener = Shortener(tokens=TOKENS)
urls = random.sample(URLs, k=min(len(URLs), {"none": 0, "one": 1, "some": 3, "all": len(URLs)}["one"]))
urls = random.sample(URLs, k=min(len(URLs), {"none": 0, "one": 1, "some": 3, "all": len(URLs)}["some"]))
print(shortener.shorten_urls(urls))
print(shortener.shorten_urls(urls)) # Should use cache.

Expand Down

0 comments on commit b307d70

Please sign in to comment.