aboutsummaryrefslogtreecommitdiff
path: root/modules/link.py
diff options
context:
space:
mode:
Diffstat (limited to 'modules/link.py')
-rw-r--r--modules/link.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/modules/link.py b/modules/link.py
new file mode 100644
index 0000000..68f56e1
--- /dev/null
+++ b/modules/link.py
@@ -0,0 +1,39 @@
+from aiohttp import ClientSession, ClientResponse
+from bs4 import BeautifulSoup
+from discord import Embed
+from discord.ext.commands import Bot, command, Context
+
+
+class Link(object):
+ def __init__(self, bot: Bot):
+ self.bot = bot
+
+ @command(aliases=['unshorten'])
+ async def retrace(self, ctx: Context, *, link: str):
+ found = set()
+ history = []
+ async with ClientSession() as sess:
+ while link not in found:
+ async with sess.get(link, allow_redirects=False) as res:
+ res: ClientResponse
+ found.add(res.url)
+ if res.status // 100 == 3:
+ history.append(('header', res.url))
+ link = res.headers['Location']
+ continue
+ soup = BeautifulSoup((await res.text()), 'html5lib')
+ el = soup.find('meta', attrs={'http-equiv': 'refresh'})
+ if el:
+ history.append(('meta', res.url))
+ link = el['content'].split('=')[1]
+ continue
+ break
+ text = '\n'.join([f'{re[0]} - {re[1]}' for re in history] + [f'Final - {link}'])
+ await ctx.send(
+ embed=Embed(
+ description=text
+ ))
+
+
+def setup(bot: Bot):
+ bot.add_cog(Link(bot))