aboutsummaryrefslogtreecommitdiff
path: root/modules/link.py
diff options
context:
space:
mode:
authorromangraef <romangraef@loves.dicksinhisan.us>2018-10-14 09:38:05 +0200
committerromangraef <romangraef@loves.dicksinhisan.us>2018-10-14 09:38:05 +0200
commit2e09fe0b572d36367f448b008d6121e6a6436329 (patch)
tree90de2085eb9dc24d7f542c2356603cd4a13fce04 /modules/link.py
parent8282428a268f87a0a4d68790521e57904826309e (diff)
downloadnotaselfbotv2finalforsure-2e09fe0b572d36367f448b008d6121e6a6436329.tar.gz
notaselfbotv2finalforsure-2e09fe0b572d36367f448b008d6121e6a6436329.tar.bz2
notaselfbotv2finalforsure-2e09fe0b572d36367f448b008d6121e6a6436329.zip
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))