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

Commit b307d70

Browse files
committed
Stop rewriting bit.ly with j.mp because this suddenly doesn't seem to work anymore
1 parent a5e8cd1 commit b307d70

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ The following have historically been the rate limits per token:
3333
* Per hour: 1000 (presumably for status 200 or 201)
3434
* Per month: 1000 (presumably for status 201 only)
3535

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

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

4343
### Python
44-
Python 3.7+ is required.
44+
Python 3.7 is required.
4545
Any older version of Python will not work due to the use of
4646
[`ThreadPoolExecutor`](https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor)
4747
with an *initializer*.
@@ -62,19 +62,19 @@ Usage examples:
6262
# Shorten to list
6363
>>> long_urls = ['https://www.amazon.com/gp/product/B07LFJMS2S/', 'https://www.cnn.com/election/2020', 'https://paperswithcode.com/sota']
6464
>>> shortener.shorten_urls(long_urls)
65-
['https://amzn.to/2HcWFgV', 'https://cnn.it/3ofdpVp', 'https://j.mp/2IHwQ8P']
65+
['https://amzn.to/3Inxf9V', 'https://cnn.it/3FKKZd8', 'https://bit.ly/3tLlp5w']
6666

6767
# Shorten to dict
6868
>>> long_urls = ['https://news.google.com', 'https://yahoo.com/']
6969
>>> shortener.shorten_urls_to_dict(long_urls)
70-
{'https://news.google.com': 'https://j.mp/31t9qL2', 'https://yahoo.com/': 'https://yhoo.it/3ondJS2'}
70+
{'https://news.google.com': 'https://bit.ly/3IjSObD', 'https://yahoo.com/': 'https://yhoo.it/2BiHgp8'}
7171

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

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

bitlyshortener/shortener.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def _shorten_url(self, long_url: str) -> str: # pylint: disable=too-many-locals
108108
# Preprocess long URL
109109
long_url = long_url.strip()
110110
if self._is_known_short_url(long_url):
111-
# Note: A preexisting Bitly link can use one of many domains, not just j.mp. It can also be
111+
# Note: A preexisting Bitly link can use one of many domains, not just bit.ly. It can also be
112112
# a custom link or not. Such a link must be validated and normalized.
113113
long_url = self._lengthen_url(long_url)
114114

@@ -180,9 +180,6 @@ def _shorten_url(self, long_url: str) -> str: # pylint: disable=too-many-locals
180180
# Postprocess short URL
181181
if short_url.startswith("http://"): # Example: http://citi.us/2FPqsuZ
182182
short_url = short_url.replace("http://", "https://", 1)
183-
if short_url.startswith("https://bit.ly/"):
184-
url_id = short_url.rpartition("/")[-1]
185-
short_url = f"https://j.mp/{url_id}"
186183

187184
log.debug("Returning short URL %s for long URL %s.", short_url, long_url)
188185
return short_url

scripts/try_shortener.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,9 @@
6464
"https://arxiv.org/abs/1902.00541v1",
6565
]
6666

67-
# URLs = ["https://j.mp/websniffer", "http://j.mp/2Bo2LVf", "http://bit.ly/2BombJQ", "https://cnn.it/2Ggb2ih"]
68-
6967
try:
7068
shortener = Shortener(tokens=TOKENS)
71-
urls = random.sample(URLs, k=min(len(URLs), {"none": 0, "one": 1, "some": 3, "all": len(URLs)}["one"]))
69+
urls = random.sample(URLs, k=min(len(URLs), {"none": 0, "one": 1, "some": 3, "all": len(URLs)}["some"]))
7270
print(shortener.shorten_urls(urls))
7371
print(shortener.shorten_urls(urls)) # Should use cache.
7472

0 commit comments

Comments
 (0)