Compare commits
2 Commits
887169e7d2
...
1b8b32027c
| Author | SHA1 | Date | |
|---|---|---|---|
| 1b8b32027c | |||
| a6190aeb84 |
@@ -132,6 +132,54 @@ run_bookmarks = _create_operation_handler(
|
||||
)
|
||||
|
||||
|
||||
def _get_operation_config(cmd: str) -> Optional[Dict[str, Any]]:
|
||||
configs = {
|
||||
"posts": {
|
||||
"operation_name": "Deleting posts",
|
||||
"strategy_type": "feed",
|
||||
"collection": None,
|
||||
"filter_fn": None
|
||||
},
|
||||
"medias": {
|
||||
"operation_name": "Deleting posts with media",
|
||||
"strategy_type": "feed",
|
||||
"collection": None,
|
||||
"filter_fn": lambda post: PostAnalyzer.has_media(post.post)
|
||||
},
|
||||
"likes": {
|
||||
"operation_name": "Undoing likes",
|
||||
"strategy_type": "record",
|
||||
"collection": "app.bsky.feed.like",
|
||||
"filter_fn": None
|
||||
},
|
||||
"reposts": {
|
||||
"operation_name": "Undoing reposts",
|
||||
"strategy_type": "record",
|
||||
"collection": "app.bsky.feed.repost",
|
||||
"filter_fn": None
|
||||
},
|
||||
"quotes": {
|
||||
"operation_name": "Deleting quote posts",
|
||||
"strategy_type": "feed",
|
||||
"collection": None,
|
||||
"filter_fn": lambda post: PostAnalyzer.has_quote(post.post)
|
||||
},
|
||||
"follows": {
|
||||
"operation_name": "Unfollowing accounts",
|
||||
"strategy_type": "record",
|
||||
"collection": "app.bsky.graph.follow",
|
||||
"filter_fn": None
|
||||
},
|
||||
"bookmarks": {
|
||||
"operation_name": "Deleting bookmarks",
|
||||
"strategy_type": "bookmark",
|
||||
"collection": None,
|
||||
"filter_fn": None
|
||||
}
|
||||
}
|
||||
return configs.get(cmd)
|
||||
|
||||
|
||||
def run_all(skip_confirmation: bool = False):
|
||||
logger = get_logger()
|
||||
|
||||
@@ -144,10 +192,32 @@ def run_all(skip_confirmation: bool = False):
|
||||
f"run all cleanup commands ({commands_str})", skip_confirmation)
|
||||
|
||||
logger.info("Running all cleanup commands...")
|
||||
|
||||
from .operations import OperationContext
|
||||
try:
|
||||
context = OperationContext()
|
||||
shared_client = context.client
|
||||
shared_config_data = context.config_data
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Failed to initialize shared context: {e}", exc_info=True)
|
||||
raise
|
||||
|
||||
for cmd in commands:
|
||||
try:
|
||||
logger.info(f"Starting command: {cmd}")
|
||||
registry.execute(cmd, skip_confirmation=True)
|
||||
operation_config = _get_operation_config(cmd)
|
||||
if operation_config:
|
||||
Operation(
|
||||
operation_config["operation_name"],
|
||||
strategy_type=operation_config["strategy_type"],
|
||||
collection=operation_config["collection"],
|
||||
filter_fn=operation_config["filter_fn"],
|
||||
client=shared_client,
|
||||
config_data=shared_config_data
|
||||
).run()
|
||||
else:
|
||||
registry.execute(cmd, skip_confirmation=True)
|
||||
logger.info(f"Completed command: {cmd}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error running '{cmd}': {e}", exc_info=True)
|
||||
|
||||
@@ -10,25 +10,43 @@ from .logger import get_logger, ProgressTracker
|
||||
|
||||
|
||||
class OperationContext:
|
||||
def __init__(self):
|
||||
def __init__(self, client=None, config_data=None):
|
||||
self.logger = get_logger()
|
||||
try:
|
||||
self.auth = Auth()
|
||||
self.client = self.auth.login()
|
||||
self.config = Configuration()
|
||||
self.config_data = self.config.load()
|
||||
except (ValueError, FileNotFoundError) as e:
|
||||
self.logger.error(f"Configuration error: {e}")
|
||||
raise
|
||||
except Exception as e:
|
||||
self.logger.error(
|
||||
f"Unexpected error during initialization: {e}", exc_info=True)
|
||||
raise ValueError(
|
||||
f"Failed to initialize operation context: {e}") from e
|
||||
|
||||
if client is not None:
|
||||
self.client = client
|
||||
self.did = client.me.did
|
||||
else:
|
||||
try:
|
||||
self.auth = Auth()
|
||||
self.client = self.auth.login()
|
||||
self.did = self.client.me.did
|
||||
except (ValueError, FileNotFoundError) as e:
|
||||
self.logger.error(f"Configuration error: {e}")
|
||||
raise
|
||||
except Exception as e:
|
||||
self.logger.error(
|
||||
f"Unexpected error during initialization: {e}", exc_info=True)
|
||||
raise ValueError(
|
||||
f"Failed to initialize operation context: {e}") from e
|
||||
|
||||
if config_data is not None:
|
||||
self.config_data = config_data
|
||||
else:
|
||||
try:
|
||||
self.config = Configuration()
|
||||
self.config_data = self.config.load()
|
||||
except (ValueError, FileNotFoundError) as e:
|
||||
self.logger.error(f"Configuration error: {e}")
|
||||
raise
|
||||
except Exception as e:
|
||||
self.logger.error(
|
||||
f"Unexpected error loading configuration: {e}", exc_info=True)
|
||||
raise ValueError(
|
||||
f"Failed to load configuration: {e}") from e
|
||||
|
||||
self.batch_size = self.config_data.get("batch_size", 10)
|
||||
self.delay = self.config_data.get("delay", 1)
|
||||
self.did = self.client.me.did
|
||||
|
||||
|
||||
class BaseStrategy:
|
||||
@@ -120,10 +138,14 @@ class Operation:
|
||||
operation_name: str,
|
||||
strategy_type: str = "feed",
|
||||
collection: Optional[str] = None,
|
||||
filter_fn: Optional[Callable[[Any], bool]] = None
|
||||
filter_fn: Optional[Callable[[Any], bool]] = None,
|
||||
client=None,
|
||||
config_data=None
|
||||
):
|
||||
self.operation_name = operation_name
|
||||
self.filter_fn = filter_fn
|
||||
self._client = client
|
||||
self._config_data = config_data
|
||||
|
||||
if strategy_type == "record":
|
||||
if not collection:
|
||||
@@ -135,7 +157,8 @@ class Operation:
|
||||
self.strategy = FeedStrategy()
|
||||
|
||||
def run(self) -> int:
|
||||
context = OperationContext()
|
||||
context = OperationContext(
|
||||
client=self._client, config_data=self._config_data)
|
||||
progress = ProgressTracker(operation=self.operation_name)
|
||||
|
||||
context.logger.info(
|
||||
|
||||
Reference in New Issue
Block a user