带内存缓存的大容量Bitly V4 URL Shortener
bitlyshortener的Python项目详细描述
双倍缩短器
bitly shortener是一个基于python 3.7的大容量bitly v4 url shortener,具有内存缓存。 它需要并使用Bitly提供的一个或多个通用访问令牌,Bitly半随机地使用这些令牌。 然而,它受到每IP速率限制的限制。 作为免责声明,这是一个非正式的包,它与Bitly没有关联。
其他bitly操作不在此包的范围内。
以下是每个令牌的已知速率限制:
- 每分钟:100(可能是状态200或201)[ref]
- 每小时:1000(可能是状态200或201)[ref]
- 每月:1000(可能仅适用于状态201)[ref] (requires login)
链接
用法
此包不包含任何访问令牌。要获得一个:
- 注册一个新的bitly帐户。
像
YourGmailUsername+RandomSuffix@gmail.com
这样的电子邮件地址应该可以工作。 - 通过单击确认电子邮件中的链接来验证电子邮件地址。 有时需要重新发送确认电子邮件。
- 在帐户配置文件中,导航到通用访问令牌。
- 输入密码并单击生成令牌。
python 3.7+是必需的。
由于使用了
^{
要安装软件包,请运行:
pip install bitlyshortener
用法示例:
frombitlyshortenerimportShortenertokens_pool=['9fbe2864bb8872f5027c103321ff91be90aea687','0cbe3864bc8872f5027c103321ff91be30aea787']# Use your own.shortener=Shortener(tokens=tokens_pool,max_cache_size=8192)# Shorten to listurls=['https://paperswithcode.com/sota','https://arxiv.org/','https://arxiv.org/list/cs.LG/recent']shortener.shorten_urls(urls)['https://j.mp/2TuIwfz','https://j.mp/2t8R7cu','https://j.mp/2GohbIt']# Shorten to dicturls=['https://news.google.com','https://yahoo.com/']shortener.shorten_urls_to_dict(urls){'https://news.google.com':'https://j.mp/2TzvYnq','https://yahoo.com/':'https://j.mp/2TCihE4'}# Shorten diverse preexisting Bitly links to listurls=['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']# Show cache infoshortener.cache_info{'Shortener._long_url_to_int_id':CacheInfo(hits=0,misses=9,maxsize=2048,currsize=9)}
要获得最快的响应,URL必须像上面的示例一样在一个批处理中一起缩短。
最多可以使用32个并发请求者的线程池,但每个随机令牌最多不超过5个。
例如,如果提供了两个令牌,则最多使用2*5=10个并发工作线程。
如果提供了8个令牌,则不使用8*5=40,但最多使用32个并发工作线程。
如果确实需要,可以在初始化shortener之前通过设置config.MAX_WORKERS
来增加最大限制。
返回的短链接使用带https的j.mp
域。
任何先前存在的短链接也被归一化以使用该域。