Skip to content

rss: Replace crashy double-formatting with standard format_token_replace #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 14, 2023

Conversation

progval
Copy link
Contributor

@progval progval commented Jun 13, 2023

No description provided.

@examknow examknow linked an issue Jun 13, 2023 that may be closed by this pull request
modules/rss.py Outdated
# just in case the format starts keyerroring and you're not sure why
self.log.trace("RSS Entry: " + str(entry))
try:
format = channel.get_setting("rss-format", "$longtitle: $title by $author - $link").replace("$longtitle", feed_title_str).replace("$title", title).replace("$link", link).replace("$author", author).format(**entry)
template = channel.get_setting("rss-format", "$longtitle: $title by $author - $link")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
template = channel.get_setting("rss-format", "$longtitle: $title by $author - $link")
template = channel.get_setting("rss-format", "${longtitle}: ${title} by ${author} - ${link}")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's also a breakage for existing configs, isn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think custom formatting really gets used tbh, but some way to detect/migrate it would be good

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could perform a sweep of the rss-format configs at module load time and convert $variable and {variable} to ${variable} and then set a bot config value to note that the migration has happened. I can't think of a nicer way to do this without majorly overcomplicating things.

@progval progval changed the title rss: Simplify entry formatting rss: Replace crashy double-formatting with standard format_token_replace Jun 13, 2023
@examknow
Copy link
Member

examknow commented Jun 14, 2023

The following should be sufficient for migration of old configs. Just place it inside the BaseModule subclass.

import re

def _migrate_formats(self):
    count = 0
    migration_sub = re.compile(r"(?:\$|{)+(?P<variable>[^}\s]+)(?:})?")
    old_formats = self.bot.database.execute_fetchall("""
        SELECT channel_id, value FROM channel_settings
        WHERE setting = 'rss-format'
    """)

    for channel_id, format in old_formats:
        new_format = migration_re.sub(r"${\1}", format)
        self.bot.database.execute("""
            UPDATE channel_settings SET value = ?
            WHERE setting = 'rss-format'
            AND channel_id = ?
        """, [new_format, channel_id])
        count += 1

    self.log.info("Successfully migrated %d rss-format settings" % count)

def on_load(self):
    if self.bot.get_setting("rss-fmt-migration", False):
        self.log.info("Attempting to migrate old rss-format settings")
        self._migrate_formats()
        self.bot.set_setting("rss-fmt-migration", True)

Copy link
Member

@examknow examknow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and working

@examknow examknow merged commit ababe84 into bitbot-irc:develop Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Important] Bot crashes when parsing the URL of certain RSS feeds
2 participants