Skip to content

Commit

Permalink
feat: facebook url preview
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroday0619 committed Aug 15, 2023
1 parent 1f47aaf commit ac5ea0d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(
super().__init__(message, intents, discord_token, *args, **kwargs)

async def on_ready(self):
await self.load_extensions(["cogs.system", "cogs.tts"])
await self.load_extensions(["cogs.system", "cogs.tts", "cogs.utils"])
try:
while self.is_ws_ratelimited():
self.logger.info("Websocket ratelimited, waiting 10 seconds")
Expand Down
39 changes: 39 additions & 0 deletions app/extension/facebook/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import re
import httpx
from scrapy import Selector


def htmlTagClean(html):
cleaner = re.compile('<.*?>')
return cleaner.sub('', html)


class FacebookParser(object):
"""Facebook public post parser."""
def __init__(self) -> None:
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
'viewport-width': 1466
}

def parse_html(self, html) -> dict[str, str]:
soup = Selector(text=html)
title = soup.xpath('//*[@id="facebook"]/head/title')
meta = soup.xpath('//*[@id="facebook"]/head/meta')
description = ""
for i in meta:
for key in i.attrib.keys():
if key == "name":
if i.attrib[key] == "description":
description = i.attrib["content"]
return {
"author": htmlTagClean(title.extract()[0]).split(" - ")[0],
"title": htmlTagClean(title.extract()[0]).split(" - ")[1],
"description": description
}


async def parse(self, url) -> dict[str, str]:
async with httpx.AsyncClient() as client:
response = await client.get(url, headers=self.headers)
return self.parse_html(html=response.text)
42 changes: 42 additions & 0 deletions cogs/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import discord
from discord.ext import commands
from discord.ext.commands import Context, hybrid_command
from app.services.logger import generate_log
from app.extension.facebook import FacebookParser

class UrlFlags(commands.FlagConverter):
_url: str = commands.flag(description="Facebook public post url")


class Utils(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
self.logger = generate_log()

@hybrid_command(name="facebook", with_app_command=True)
async def facebook(self, ctx: Context, url: UrlFlags):
"""License"""

url = url._url
FP = FacebookParser()
resp = await FP.parse(url=url)

embed = discord.Embed(
title="Facebook Public Post Preview",
)
embed.set_author(
name=resp["author"],
)
embed.add_field(
name="Title",
value=resp["title"],
)
embed.add_field(
name="Description",
value=resp["description"],
)
embed.set_footer(text="Facebook Public Post Parser v1.0.0")
await ctx.send(embed=embed)

async def setup(bot):
await bot.add_cog(Utils(bot))

0 comments on commit ac5ea0d

Please sign in to comment.