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