-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f47aaf
commit ac5ea0d
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |