Skip to content

Commit

Permalink
fixed urls being formatted wrongly on different platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Deutscher775 committed Dec 8, 2024
1 parent a1e3f51 commit 7d63c13
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ assets/517057624072519680.png
src/astroidapi/test.py
/src/astroidapi/TMP_attachments
/src/healtcheck_summaries
src/astroidapi/test.py
15 changes: 7 additions & 8 deletions src/astroidapi/endpoint_update_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import astroidapi.sending_handler as sending_handler
import astroidapi.surrealdb_handler as surrealdb_handler
import astroidapi.queue_processor as queue_processor
import astroidapi.formatter as formatter
from Bot import config
import astroidapi.health_check as health_check

Expand Down Expand Up @@ -212,12 +213,7 @@ async def update_endpoint(

if message_content:
if sender == "discord":
if "http" in message_content or "https" in message_content:
urls = re.findall(r'(https?://\S+)', message_content)
for url in urls:
image_markdown = f"![{url}]({url})"
message_content = message_content.replace(url, image_markdown) + message_content.split(url)[1]
endpoint_data["meta"]["message"]["content"] = message_content
endpoint_data["meta"]["message"]["content"] = formatter.Format.format_message(message_content)
else:
endpoint_data["meta"]["message"]["content"] = message_content

Expand All @@ -233,8 +229,11 @@ async def update_endpoint(
endpoint_data["meta"]["message"]["attachments"] = [message_attachments]

if message_embed:
embed_object = json.loads(message_embed.replace("'", '"'))
endpoint_data["meta"]["message"]["embed"] = embed_object
try:
embed_object = json.loads(message_embed.replace("'", '"'))
endpoint_data["meta"]["message"]["embed"] = embed_object
except Exception as e:
print("[EmbedError] An error occurred while parsing the embed object: ", e)

if len(endpoint_data["config"]["channels"]["discord"]) == 0:
endpoint_data["meta"]["read"]["discord"] = True
Expand Down
16 changes: 16 additions & 0 deletions src/astroidapi/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

class Format:

@classmethod
def format_message(cls, message: str):
message = cls.removemarkdownlinks(message)
return message

@classmethod
def removemarkdownlinks(cls, message: str):
message = re.sub(r"\[(.*?)\]\((.*?)\)", r"\2", message)
return message

@classmethod
def format_username(cls, username: str):
username = re.sub(r"[^\w\s]", "", username)
Expand All @@ -15,3 +25,9 @@ def format_urlsafe(cls, message: str):
@classmethod
def unformat_urlsafe(cls, message: str):
message = message.replace("%3F", "?").replace("%26", "&")

@classmethod
def format_links_guilded_safe(cls, message: str):
message = re.sub(r"\[(.*?)\]\((.*?)\)", r"\1", message)
message = re.sub(r"(https?://[^\s]+)", r"[\1](\1)", message)
return message
2 changes: 1 addition & 1 deletion src/astroidapi/queue_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ async def sendMessage(cls, endpoint):
async def handleUpdatedEndpointData(cls, endpoint, updated_json):
await cls.appendMessage(endpoint, updated_json)
await cls.sendMessage(endpoint)

return True
4 changes: 4 additions & 0 deletions src/astroidapi/sending_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ async def send_to_guilded(cls, updated_json, endpoint, attachments: list = None)
webhook_obj = guilded.Webhook.from_url(webhook, session=session)
try:
message_content = updated_json["meta"]["message"]["content"]
try:
message_content = formatter.Format.format_links_guilded_safe(message_content)
except Exception as e:
print("[SendToGuilded] Failed to format links: ", e)
if message_content is None or message_content == "" or message_content == " ":
if updated_json["meta"]["message"]["attachments"] is not None:
message_content = "‎ "
Expand Down

0 comments on commit 7d63c13

Please sign in to comment.