Skip to content

Commit

Permalink
Update to 1.1.5 (See Release Notes for details)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtoid committed Nov 22, 2020
1 parent 522e117 commit 1b47476
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 18 deletions.
60 changes: 45 additions & 15 deletions chat_exporter/chat_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import traceback
from chat_exporter.misc_tools import escape_html, member_colour_translator
from chat_exporter.mention_convert import parse_mentions, escape_mentions, unescape_mentions
from chat_exporter.markdown_convert import parse_markdown
from chat_exporter.markdown_convert import parse_markdown, parse_embed_markdown
from chat_exporter.emoji_convert import convert_emoji
from pytz import timezone
from datetime import timedelta
Expand All @@ -27,7 +27,7 @@ def init_exporter(_bot):

async def export(ctx):
try:
transcript = await generate_transcript(ctx.channel)
transcript = await produce_transcript(ctx.channel)
except Exception:
transcript = None
print("Error during transcript generation!", file=sys.stderr)
Expand Down Expand Up @@ -63,9 +63,18 @@ async def export(ctx):


async def generate_transcript(channel):
try:
transcript = await produce_transcript(channel)
except Exception:
transcript = None
print(f"Please send a screenshot of the above error to https://www.github.com/mahtoid/DiscordChatExporterPy")

return transcript


async def produce_transcript(channel):
guild = channel.guild
messages = await channel.history(limit=None, oldest_first=True).flatten()

previous_author = ""
previous_timestamp = ""
messages_html = ""
Expand All @@ -88,11 +97,18 @@ async def generate_transcript(channel):
for e in m.embeds:
fields = ""
for f in e.fields:
cur_field = await fill_out(channel, msg_embed_field, [
("EMBED_FIELD_NAME", f.name),
("EMBED_FIELD_VALUE", f.value),
])
fields += cur_field
if f.inline:
cur_field = await fill_out(channel, msg_embed_field_inline, [
("EMBED_FIELD_NAME", f.name),
("EMBED_FIELD_VALUE", f.value),
])
fields += cur_field
else:
cur_field = await fill_out(channel, msg_embed_field, [
("EMBED_FIELD_NAME", f.name),
("EMBED_FIELD_VALUE", f.value),
])
fields += cur_field

# default values for embeds need explicit setting because
# Embed.empty breaks just about everything
Expand All @@ -113,17 +129,22 @@ async def generate_transcript(channel):
else ""
footer_icon = e.footer.icon_url \
if e.footer.icon_url != discord.Embed.Empty \
else ""
else None
url = e.url \
if e.url != discord.Embed.Empty \
else ""
embed_image = ""
footer_fields = ""
if footer != "":
cur_footer = await fill_out(channel, embed_footer, [
("EMBED_FOOTER", footer),
("EMBED_FOOTER_ICON", footer_icon)
])
if footer_icon:
cur_footer = await fill_out(channel, embed_footer_image, [
("EMBED_FOOTER", footer),
("EMBED_FOOTER_ICON", footer_icon)
])
else:
cur_footer = await fill_out(channel, embed_footer, [
("EMBED_FOOTER", footer),
])
footer_fields += cur_footer
if "tenor.com" in str(url):
try:
Expand All @@ -142,8 +163,8 @@ async def generate_transcript(channel):
("EMBED_AUTHOR", author),
("EMBED_TITLE", title),
("EMBED_IMAGE", embed_image),
("EMBED_DESC", desc, PARSE_MODE_MARKDOWN),
("EMBED_FIELDS", fields, []),
("EMBED_DESC", desc, PARSE_MODE_EMBED),
("EMBED_FIELDS", fields, PARSE_MODE_EMBED_VALUE),
("EMBED_FOOTER", footer_fields)

])
Expand Down Expand Up @@ -315,6 +336,8 @@ async def generate_transcript(channel):
PARSE_MODE_NONE = 0
PARSE_MODE_NO_MARKDOWN = 1
PARSE_MODE_MARKDOWN = 2
PARSE_MODE_EMBED = 3
PARSE_MODE_EMBED_VALUE = 4


async def fill_out(channel, base, replacements):
Expand All @@ -332,6 +355,11 @@ async def fill_out(channel, base, replacements):
v = await parse_mentions(v, channel.guild, bot)
if mode == PARSE_MODE_MARKDOWN:
v = await parse_markdown(v)
if mode == PARSE_MODE_EMBED:
v = await parse_embed_markdown(v)
v = await parse_markdown(v)
if mode == PARSE_MODE_EMBED_VALUE:
v = await parse_embed_markdown(v)

base = base.replace("{{" + k + "}}", v)

Expand All @@ -350,10 +378,12 @@ def read_file(filename):
bot_tag = read_file(dir_path + "/chat_exporter_html/bot-tag.html")
msg_embed = read_file(dir_path + "/chat_exporter_html/message-embed.html")
msg_embed_field = read_file(dir_path + "/chat_exporter_html/message-embed-field.html")
msg_embed_field_inline = read_file(dir_path + "/chat_exporter_html/message-embed-field-inline.html")
img_attachment = read_file(dir_path + "/chat_exporter_html/image-attachment.html")
msg_attachment = read_file(dir_path + "/chat_exporter_html/message_attachment.html")
emoji = read_file(dir_path + "/chat_exporter_html/emoji_attachment.html")
custom_emoji = read_file(dir_path + "/chat_exporter_html/custom_emoji_attachment.html")
continue_message = read_file(dir_path + "/chat_exporter_html/continue_message.html")
end_message = read_file(dir_path + "/chat_exporter_html/end_message.html")
embed_footer = read_file(dir_path + "/chat_exporter_html/embed_footer.html")
embed_footer_image = read_file(dir_path + "/chat_exporter_html/embed_footer_image.html")
1 change: 0 additions & 1 deletion chat_exporter/chat_exporter_html/embed_footer.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<div class="chatlog__embed-footer">
<img class="chatlog__embed-footer-icon" src="{{EMBED_FOOTER_ICON}}">
<span class="chatlog__embed-footer-text">{{EMBED_FOOTER}}</span>
</div>
4 changes: 4 additions & 0 deletions chat_exporter/chat_exporter_html/embed_footer_image.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="chatlog__embed-footer">
<img class="chatlog__embed-footer-icon" src="{{EMBED_FOOTER_ICON}}">
<span class="chatlog__embed-footer-text">{{EMBED_FOOTER}}</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="chatlog__embed-field chatlog__embed-field--inline ">
<div class="chatlog__embed-field-name"><span class="markdown">{{EMBED_FIELD_NAME}}</span></div>
<div class="chatlog__embed-field-value"><span class="markdown">{{EMBED_FIELD_VALUE}}</span></div>
</div>
2 changes: 1 addition & 1 deletion chat_exporter/chat_exporter_html/message-embed-field.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="chatlog__embed-field chatlog__embed-field--inline ">
<div class="chatlog__embed-field chatlog__embed-field">
<div class="chatlog__embed-field-name"><span class="markdown">{{EMBED_FIELD_NAME}}</span></div>
<div class="chatlog__embed-field-value"><span class="markdown">{{EMBED_FIELD_VALUE}}</span></div>
</div>
43 changes: 43 additions & 0 deletions chat_exporter/markdown_convert.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
import re


async def parse_embed_markdown(content):
# [Message](Link)

pattern = re.compile(r'<div class="chatlog__embed-field-value"><span class="markdown">(.+)?</span></div>')
match = re.search(pattern, content)
if match is not None:
field_value = match.group(1)
pattern = re.compile(r"\[(.+)?]\((.+)?\)")
match = re.search(pattern, field_value)
change = False
while match is not None:
change = True
affected_text = match.group(1)
affected_url = match.group(2)
field_value = field_value.replace(field_value[match.start():match.end()],
'<a href="%s">%s</a>' % (affected_url, affected_text))
match = re.search(pattern, field_value)

if change:
pattern = re.compile(r'<div class="chatlog__embed-field-value"><span class="markdown">(.+)?</span></div>')
match = re.search(pattern, content)
content = content.replace(content[match.start():match.end()],
'<div class="chatlog__embed-field-value"><span class="markdown">%s</span></div>'
% field_value)

if match is None:
pattern = re.compile(r"\[(.+)?]\((.+)?\)")
match = re.search(pattern, content)
while match is not None:
affected_text = match.group(1)
affected_url = match.group(2)
content = content.replace(content[match.start():match.end()],
'<a href="%s">%s</a>' % (affected_url, affected_text))
match = re.search(pattern, content)

return content


async def parse_markdown(content):
# **bold**
pattern = re.compile(r"\*\*(.*?)\*\*")
Expand Down Expand Up @@ -84,6 +122,11 @@ async def parse_markdown(content):
match = re.search(pattern, content)
while match is not None:
affected_text = match.group(1)
if affected_text.lower().startswith(("asciidoc", "autohotkey", "bash", "coffeescript", "cpp", "cs", "css",
"diff", "fix", "glsl", "ini", "json", "md", "ml", "prolog", "py",
"tex", "xl", "xml")):
affected_text = affected_text.replace("<br>", " <br>")
affected_text = ' '.join(affected_text.split()[1:])
affected_text = re.sub(r"^<br>", "", affected_text)
affected_text = re.sub(r"<br>$", "", affected_text)
affected_text = await return_to_markdown(affected_text)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="chat_exporter",
version="1.1.4",
version="1.1.5",
author="mahtoid",
description="A simple Discord chat exporter for Python Discord bots.",
long_description=long_description,
Expand Down

0 comments on commit 1b47476

Please sign in to comment.