From abd03d9154c7579ea0b6e94526eaa18157f8bc15 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Tue, 13 Dec 2022 13:39:48 +0100 Subject: [PATCH] escape user input and rename class --- .../conversation/conversation.py | 17 +++++++++-------- .../conversation/update.py | 1 - src/mastodon_toot_follower/main.py | 8 +++++--- .../{app.py => mastodon_factory.py} | 2 +- .../server/flask_server.py | 15 ++++++++++----- 5 files changed, 25 insertions(+), 18 deletions(-) rename src/mastodon_toot_follower/{app.py => mastodon_factory.py} (98%) diff --git a/src/mastodon_toot_follower/conversation/conversation.py b/src/mastodon_toot_follower/conversation/conversation.py index 321fd45..b708a10 100644 --- a/src/mastodon_toot_follower/conversation/conversation.py +++ b/src/mastodon_toot_follower/conversation/conversation.py @@ -2,22 +2,23 @@ from feedgen.feed import FeedGenerator from flask import url_for from mastodon_toot_follower import path_util -from mastodon_toot_follower.app import App +from mastodon_toot_follower.mastodon_factory import MastodonFactory from mastodon_toot_follower.conversation.update import Update as ConversationUpdate class Conversation: - def __init__(self, app: App, mastodon_instance: str, toot_id: str, seed: str = ''): - self.app = app - self.mastodon = self.app.get_mastodon(mastodon_instance) + def __init__(self, mastodon_factory: MastodonFactory, mastodon_instance: str, toot_id: str, seed: str = ''): + self.mastodon = mastodon_factory.get_mastodon(mastodon_instance) self.toot_id = toot_id self.toot = self.mastodon.status(id=self.toot_id) + escaped_uri = path_util.escape(self.toot["uri"]) if len(seed) > 0: - self.file = self.app.toot_cache_dir().joinpath(f'{path_util.escape(self.toot["uri"])}_{seed}.json') + escaped_seed = path_util.escape(seed) + self.file = mastodon_factory.toot_cache_dir().joinpath(f'{escaped_uri}_{escaped_seed}.json') else: - self.file = self.app.toot_cache_dir().joinpath(f'{path_util.escape(self.toot["uri"])}.json') + self.file = mastodon_factory.toot_cache_dir().joinpath(f'{escaped_uri}.json') # List of replies to initial toot. self.replies = self.mastodon.status_context(id=self.toot_id)['descendants'] @@ -31,8 +32,8 @@ class Conversation: """ Returns a FeedGenerator object "fg". - atomfeed = fg.atom_str(pretty=True) # Get the ATOM feed as string - rssfeed = fg.rss_str(pretty=True) # Get the RSS feed as string + atom_feed = fg.atom_str(pretty=True) # Get the ATOM feed as string + rss_feed = fg.rss_str(pretty=True) # Get the RSS feed as string """ fg = FeedGenerator() diff --git a/src/mastodon_toot_follower/conversation/update.py b/src/mastodon_toot_follower/conversation/update.py index e785dfa..0dbeb23 100644 --- a/src/mastodon_toot_follower/conversation/update.py +++ b/src/mastodon_toot_follower/conversation/update.py @@ -81,7 +81,6 @@ class Update: return f'Edit by {acct}' def get_date(self): - acct = self.dict['acct'] if self.dict['reason'] == 'new': return self.dict['created_at'] else: diff --git a/src/mastodon_toot_follower/main.py b/src/mastodon_toot_follower/main.py index 7f51777..89ce043 100644 --- a/src/mastodon_toot_follower/main.py +++ b/src/mastodon_toot_follower/main.py @@ -3,7 +3,7 @@ import sys from mastodon_toot_follower import mastodon_util -from mastodon_toot_follower.app import App +from mastodon_toot_follower.mastodon_factory import MastodonFactory from mastodon_toot_follower.conversation.conversation import Conversation @@ -18,8 +18,10 @@ def main(): print(e, file=sys.stderr) usage() - app = App() - conversation = Conversation(app=app, + instance_url, username, toot_id = None, None, None # Fix IDE Warnings + + mastodon_factory = MastodonFactory() + conversation = Conversation(mastodon_factory=mastodon_factory, mastodon_instance=instance_url, toot_id=toot_id) conversation.save_changes() diff --git a/src/mastodon_toot_follower/app.py b/src/mastodon_toot_follower/mastodon_factory.py similarity index 98% rename from src/mastodon_toot_follower/app.py rename to src/mastodon_toot_follower/mastodon_factory.py index e1f6129..e6ef055 100644 --- a/src/mastodon_toot_follower/app.py +++ b/src/mastodon_toot_follower/mastodon_factory.py @@ -4,7 +4,7 @@ from mastodon import Mastodon from mastodon_toot_follower import path_util -class App: +class MastodonFactory: def __init__(self, name: str = 'MastodonTootFollower', url: str = 'https://git.privacy1st.de/langfingaz/MastodonTootFollower', diff --git a/src/mastodon_toot_follower/server/flask_server.py b/src/mastodon_toot_follower/server/flask_server.py index ac60135..dee0446 100644 --- a/src/mastodon_toot_follower/server/flask_server.py +++ b/src/mastodon_toot_follower/server/flask_server.py @@ -6,7 +6,7 @@ from flask import Flask, Response, render_template from flask import request from mastodon_toot_follower import mastodon_util, path_util -from mastodon_toot_follower.app import App as MastodonApp +from mastodon_toot_follower.mastodon_factory import MastodonFactory as MastodonFactory from mastodon_toot_follower.conversation.conversation import Conversation # Create Flask's `app` object @@ -17,7 +17,7 @@ app = Flask( static_folder=str(path_util.get_static_dir()), ) -mastodon_app = MastodonApp() +mastodon_factory = MastodonFactory() class Templates(Enum): @@ -35,7 +35,7 @@ def rss(): url = request.args.get('url', 'None') instance_url, username, toot_id = mastodon_util.parse_toot_url(url=url) - conversation = Conversation(app=mastodon_app, mastodon_instance=instance_url, toot_id=toot_id) + conversation = Conversation(mastodon_factory=mastodon_factory, mastodon_instance=instance_url, toot_id=toot_id) conversation.save_changes() fg = conversation.as_feed(request.url) @@ -47,7 +47,9 @@ def html(seed: str): url = request.args.get('url', 'None') instance_url, username, toot_id = mastodon_util.parse_toot_url(url=url) - conversation = Conversation(app=mastodon_app, mastodon_instance=instance_url, toot_id=toot_id, seed=seed) + conversation = Conversation(mastodon_factory=mastodon_factory, + mastodon_instance=instance_url, + toot_id=toot_id, seed=seed) conversation.save_changes() return render_template(Templates.updates.value, updates=conversation.new_updates) @@ -58,7 +60,10 @@ def json(seed: str): url = request.args.get('url', 'None') instance_url, username, toot_id = mastodon_util.parse_toot_url(url=url) - conversation = Conversation(app=mastodon_app, mastodon_instance=instance_url, toot_id=toot_id, seed=seed) + conversation = Conversation(mastodon_factory=mastodon_factory, + mastodon_instance=instance_url, + toot_id=toot_id, + seed=seed) conversation.save_changes() # If you return a dict or list from a view, it will be converted to a JSON response.