-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add asset channel as way to export assets (#91) * Addition of an optional asset channel to save attachments to another channel and preserve them from being not displayed in the final transcript (as long as the asset channel doesn't get deleted) * Fetch attachments properly as Bytes and send them into the attachment channel * propagate errors from sending assets to asset channel to user level * Cleanup code after 1st review Signed-off-by: doluk <69309597+doluk@users.noreply.github.com> * Add new attribute to README.md Signed-off-by: doluk <69309597+doluk@users.noreply.github.com> * cleanup Signed-off-by: doluk <69309597+doluk@users.noreply.github.com> * Fix missing keyword argument --------- Signed-off-by: doluk <69309597+doluk@users.noreply.github.com> * Implement member caching across multiple instances of MemberConstruct (#92) * feat: implement caching members in MessageConstructs * version bump * Asset Handler (#96) - Removal of asset_channel feature - Addition of AssetHandler origin class - Addition of two examples for implementing AssetHandlers in form of LocalFileHostHandler and DiscordChannelHandler (latter one has the same functionality as the asset_channel) - Bugfix: Preventing code blocks from being parsed for other markdown - Bugfix: Fix timestamp markdown - Bugfix: Fix div tags for pinned messages - Bugfix: Fix parsing and css of here and everyone mentions - Bugfix: Attempting to create a transcript of an empty channel would lead to an IndexError - Bugfix: Fixing erroring in some channels but not others #98 - Addition of list markdown - Addition of heading markdown * README update * Explain the concept of AttachmentHandlers (#99) * Explain the AttachmentHandler concept Signed-off-by: doluk <69309597+doluk@users.noreply.github.com> * Deduplicate Signed-off-by: doluk <69309597+doluk@users.noreply.github.com> --------- Signed-off-by: doluk <69309597+doluk@users.noreply.github.com> --------- Signed-off-by: doluk <69309597+doluk@users.noreply.github.com> Co-authored-by: Lukas Dobler <69309597+doluk@users.noreply.github.com> Co-authored-by: Dan <77840397+Void-ux@users.noreply.github.com> Co-authored-by: mahtoid <git@mahto.id>
- Loading branch information
1 parent
51e85d3
commit aa82847
Showing
11 changed files
with
397 additions
and
57 deletions.
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 |
---|---|---|
@@ -1,11 +1,22 @@ | ||
from chat_exporter.chat_exporter import export, raw_export, quick_export, link, quick_link | ||
from chat_exporter.chat_exporter import ( | ||
export, | ||
raw_export, | ||
quick_export, | ||
link, | ||
quick_link, | ||
AttachmentHandler, | ||
AttachmentToLocalFileHostHandler, | ||
AttachmentToDiscordChannelHandler) | ||
|
||
__version__ = "2.6.1" | ||
__version__ = "2.7.0" | ||
|
||
__all__ = ( | ||
export, | ||
raw_export, | ||
quick_export, | ||
link, | ||
quick_link, | ||
AttachmentHandler, | ||
AttachmentToLocalFileHostHandler, | ||
AttachmentToDiscordChannelHandler, | ||
) |
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,68 @@ | ||
import datetime | ||
import io | ||
import pathlib | ||
from typing import Union | ||
|
||
import aiohttp | ||
import discord | ||
|
||
|
||
class AttachmentHandler: | ||
"""Handle the saving of attachments (images, videos, audio, etc.) | ||
Subclass this to implement your own asset handler.""" | ||
|
||
async def process_asset(self, attachment: discord.Attachment) -> discord.Attachment: | ||
"""Implement this to process the asset and return a url to the stored attachment. | ||
:param attachment: discord.Attachment | ||
:return: str | ||
""" | ||
raise NotImplementedError | ||
|
||
class AttachmentToLocalFileHostHandler(AttachmentHandler): | ||
"""Save the assets to a local file host and embed the assets in the transcript from there.""" | ||
|
||
def __init__(self, base_path: Union[str, pathlib.Path], url_base: str): | ||
if isinstance(base_path, str): | ||
base_path = pathlib.Path(base_path) | ||
self.base_path = base_path | ||
self.url_base = url_base | ||
|
||
async def process_asset(self, attachment: discord.Attachment) -> discord.Attachment: | ||
"""Implement this to process the asset and return a url to the stored attachment. | ||
:param attachment: discord.Attachment | ||
:return: str | ||
""" | ||
file_name = f"{int(datetime.datetime.utcnow().timestamp())}_{attachment.filename}".replace(' ', '%20') | ||
asset_path = self.base_path / file_name | ||
await attachment.save(asset_path) | ||
file_url = f"{self.url_base}/{file_name}" | ||
attachment.url = file_url | ||
attachment.proxy_url = file_url | ||
return attachment | ||
|
||
|
||
class AttachmentToDiscordChannelHandler(AttachmentHandler): | ||
"""Save the attachment to a discord channel and embed the assets in the transcript from there.""" | ||
|
||
def __init__(self, channel: discord.TextChannel): | ||
self.channel = channel | ||
|
||
async def process_asset(self, attachment: discord.Attachment) -> discord.Attachment: | ||
"""Implement this to process the asset and return a url to the stored attachment. | ||
:param attachment: discord.Attachment | ||
:return: str | ||
""" | ||
try: | ||
async with aiohttp.ClientSession() as session: | ||
async with session.get(attachment.url) as res: | ||
if res.status != 200: | ||
res.raise_for_status() | ||
data = io.BytesIO(await res.read()) | ||
data.seek(0) | ||
attach = discord.File(data, attachment.filename) | ||
msg: discord.Message = await self.channel.send(file=attach) | ||
return msg.attachments[0] | ||
except discord.errors.HTTPException as e: | ||
# discords http errors, including missing permissions | ||
raise e |
Oops, something went wrong.