shorturl.py: use bitly v4 api (#355)

This commit is contained in:
David Schultz 2023-01-17 13:46:52 -06:00 committed by GitHub
parent 398aca20fa
commit 4e37f7cb35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,7 +4,7 @@
import re
from src import ModuleManager, utils
URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v3/shorten"
URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v4/shorten"
class Module(ModuleManager.BaseModule):
def on_load(self):
@ -66,11 +66,16 @@ class Module(ModuleManager.BaseModule):
access_token = self.bot.config.get("bitly-api-key", None)
if access_token:
page = utils.http.request(URL_BITLYSHORTEN, get_params={
"access_token": access_token, "longUrl": url}).json()
resp = utils.http.request(
URL_BITLYSHORTEN,
method="POST",
post_data={"long_url": url},
json_body=True,
headers={"Authorization": f"Bearer {access_token}"}
)
if page["data"]:
return page["data"]["url"]
if resp.code == 200:
return resp.json()["link"]
return None
def _find_url(self, target, args):
@ -112,4 +117,4 @@ class Module(ModuleManager.BaseModule):
event["stdout"].write("Unshortened: %s" %
response.headers["location"])
else:
event["stderr"].write("Failed to unshorten URL")
event["stderr"].write("Failed to unshorten URL")