Skip to content

Commit

Permalink
1.1.6
Browse files Browse the repository at this point in the history
Changed the way it checks for previous author (now via ID rather than name). Now converts emojis in embeds. Added 'Timezone' field to top of HTML file, will work at allowing Timezone customisation
  • Loading branch information
mahtoid committed Nov 26, 2020
1 parent c193f1f commit 35d6d54
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
51 changes: 19 additions & 32 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, parse_embed_markdown
from chat_exporter.markdown_convert import parse_markdown, parse_embed_markdown, parse_emoji
from chat_exporter.emoji_convert import convert_emoji
from pytz import timezone
from datetime import timedelta
Expand All @@ -26,6 +26,7 @@ def init_exporter(_bot):


async def export(ctx):
# noinspection PyBroadException
try:
transcript = await produce_transcript(ctx.channel)
except Exception:
Expand Down Expand Up @@ -63,6 +64,7 @@ async def export(ctx):


async def generate_transcript(channel):
# noinspection PyBroadException
try:
transcript = await produce_transcript(channel)
except Exception:
Expand All @@ -75,7 +77,7 @@ async def generate_transcript(channel):
async def produce_transcript(channel):
guild = channel.guild
messages = await channel.history(limit=None, oldest_first=True).flatten()
previous_author = ""
previous_author = 0
previous_timestamp = ""
messages_html = ""
for m in messages:
Expand Down Expand Up @@ -160,12 +162,12 @@ async def produce_transcript(channel):
("EMBED_R", str(r)),
("EMBED_G", str(g)),
("EMBED_B", str(b)),
("EMBED_AUTHOR", author),
("EMBED_TITLE", title),
("EMBED_AUTHOR", author, PARSE_MODE_EMBED_EMOJI),
("EMBED_TITLE", title, PARSE_MODE_EMBED_EMOJI),
("EMBED_IMAGE", embed_image),
("EMBED_DESC", desc, PARSE_MODE_EMBED),
("EMBED_FIELDS", fields, PARSE_MODE_EMBED_VALUE),
("EMBED_FOOTER", footer_fields)
("EMBED_FOOTER", footer_fields, PARSE_MODE_EMBED_EMOJI)

])
embeds += cur_embed
Expand Down Expand Up @@ -254,35 +256,14 @@ async def produce_transcript(channel):
])
emojis += cur_emoji

output = []
for word in m.content.split():
emoji_pattern = re.compile(r"<:.*:.*>")
emoji_animated = re.compile(r"<a:.*:.*>")
if emoji_pattern.search(word):
pattern = r"<:.*:(\d*)>"
emoji_id = re.search(pattern, word).group(1)
new_w = f'<img class="emoji emoji--small" src="https://cdn.discordapp.com/emojis/{str(emoji_id)}.png">'
word = re.sub(emoji_pattern, new_w, word)
output.append(word)
elif emoji_animated.search(word):
pattern = r"&lt;a:.*:(\d*)&gt;"
emoji_id = re.search(pattern, word).group(1)
new_w = f'<img class="emoji emoji--small" src="https://cdn.discordapp.com/emojis/{str(emoji_id)}.gif">'
word = re.sub(emoji_animated, new_w, word)
output.append(word)
else:
word = convert_emoji(word)
output.append(word)
m.content = " ".join(output)
m.content = await parse_emoji(m.content)

cur_msg = ""
try:
author_name = await escape_html(m.author.nick)
except AttributeError:
author_name = await escape_html(m.author.name)

author_name = await escape_html(m.author.display_name)

user_colour = await member_colour_translator(m.author)
if previous_author == m.author.name and previous_timestamp > time_string:
if previous_author == m.author.id and previous_timestamp > time_string:
cur_msg = await fill_out(channel, continue_message, [
("AVATAR_URL", str(m.author.avatar_url)),
("NAME_TAG", "%s#%s" % (m.author.name, m.author.discriminator)),
Expand All @@ -297,7 +278,7 @@ async def produce_transcript(channel):
("EMOJI", emojis)
])
else:
if previous_author != "" and previous_timestamp != "":
if previous_author != 0 and previous_timestamp != "":
cur_msg = await fill_out(channel, end_message, [])
cur_msg += await fill_out(channel, msg, [
("AVATAR_URL", str(m.author.avatar_url)),
Expand All @@ -313,7 +294,7 @@ async def produce_transcript(channel):
("ATTACHMENTS", attachments, PARSE_MODE_NONE),
("EMOJI", emojis)
])
previous_author = m.author.name
previous_author = m.author.id
previous_timestamp = time_string + timedelta(minutes=4)

messages_html += cur_msg
Expand All @@ -328,6 +309,7 @@ async def produce_transcript(channel):
("CHANNEL_NAME", f"Channel: {channel.name}"),
("MESSAGE_COUNT", str(len(messages))),
("MESSAGES", messages_html, PARSE_MODE_NONE),
("TIMEZONE", str(eastern)),
])

return transcript
Expand All @@ -338,6 +320,7 @@ async def produce_transcript(channel):
PARSE_MODE_MARKDOWN = 2
PARSE_MODE_EMBED = 3
PARSE_MODE_EMBED_VALUE = 4
PARSE_MODE_EMBED_EMOJI = 5


async def fill_out(channel, base, replacements):
Expand All @@ -358,8 +341,12 @@ async def fill_out(channel, base, replacements):
if mode == PARSE_MODE_EMBED:
v = await parse_embed_markdown(v)
v = await parse_markdown(v)
v = await parse_emoji(v)
if mode == PARSE_MODE_EMBED_VALUE:
v = await parse_embed_markdown(v)
v = await parse_emoji(v)
if mode == PARSE_MODE_EMBED_EMOJI:
v = await parse_emoji(v)

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

Expand Down
10 changes: 8 additions & 2 deletions chat_exporter/chat_exporter_html/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@
}

.info__guild-name {
font-size: 1.4em;
font-size: 1.2em;
}

.info__channel-name {
font-size: 1.2em;
font-size: 1em;
}

.info__channel-topic {
Expand All @@ -202,6 +202,11 @@
margin-top: 2px;
}

.info__channel-timezone {
margin-top: 2px;
font-size:0.9em;
}

.info__channel-date-range {
margin-top: 2px;
}
Expand Down Expand Up @@ -608,6 +613,7 @@


<div class="info__channel-message-count">{{MESSAGE_COUNT}} messages</div>
<div class="info__channel-timezone">Timezone: {{TIMEZONE}}</div>

</div>
</div>
Expand Down
25 changes: 25 additions & 0 deletions chat_exporter/markdown_convert.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
import re
from chat_exporter.emoji_convert import convert_emoji


async def parse_emoji(content):
output = []
for word in content.split():
emoji_pattern = re.compile(r"&lt;:.*:.*&gt;")
emoji_animated = re.compile(r"&lt;a:.*:.*&gt;")
if emoji_pattern.search(word):
pattern = r"&lt;:.*:(\d*)&gt;"
emoji_id = re.search(pattern, word).group(1)
new_w = f'<img class="emoji emoji--small" src="https://cdn.discordapp.com/emojis/{str(emoji_id)}.png">'
word = re.sub(emoji_pattern, new_w, word)
output.append(word)
elif emoji_animated.search(word):
pattern = r"&lt;a:.*:(\d*)&gt;"
emoji_id = re.search(pattern, word).group(1)
new_w = f'<img class="emoji emoji--small" src="https://cdn.discordapp.com/emojis/{str(emoji_id)}.gif">'
word = re.sub(emoji_animated, new_w, word)
output.append(word)
else:
word = convert_emoji(word)
output.append(word)
content = " ".join(output)
return content


async def parse_embed_markdown(content):
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.5.5",
version="1.1.6",
author="mahtoid",
description="A simple Discord chat exporter for Python Discord bots.",
long_description=long_description,
Expand Down

0 comments on commit 35d6d54

Please sign in to comment.