aboutsummaryrefslogtreecommitdiff
path: root/modules/link.py
blob: 68f56e191848578a8828f22bbc0d729ffcb07e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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))