def clean(config: CleanTootsConfig, delete: bool, headless: bool): """ Delete Toots based on rules in config file. Without the `--delete` flag, toots will only be displayed. """ if not _config_has_sections(config): return h = html2text.HTML2Text() h.ignore_links = True h.ignore_emphasis = True h.ignore_images = True h.ignore_tables = True for section in config.sections(): section = config[section] user_secret_file = config.file(section.get("user_secret_file")) mastodon = Mastodon(access_token=user_secret_file) user = mastodon.me() page = mastodon.account_statuses(user["id"]) would_delete = [] protected = [] while page: for toot in page: protection_reason = _toot_protection_reason(toot, section) if protection_reason: protected.append({"toot": toot, "reason": protection_reason}) else: would_delete.append(toot) page = mastodon.fetch_next(page) _delete_or_log(delete, h, headless, mastodon, protected, would_delete)
def login(config: CleanTootsConfig, only_missing: bool): """Fetch credentials for each app described in config file.""" if not _config_has_sections(config): return prompt = True for section in config.sections(): section = config[section] app_file_exists = config.isfile(section.get("app_secret_file")) user_file_exists = config.isfile(section.get("user_secret_file")) if not (only_missing and app_file_exists): Mastodon.create_app( "cleantoots", api_base_url=section.get("api_base_url"), to_file=config.file(section.get("app_secret_file")), ) mastodon = Mastodon( client_id=config.file(section.get("app_secret_file"))) if not (only_missing and user_file_exists and app_file_exists): _open_url(mastodon.auth_request_url(), echo=prompt) prompt = False code = click.prompt("Enter code for {}".format( section.get("api_base_url"))) mastodon.log_in(code=code, to_file=config.file( section.get("user_secret_file")))
def edit(config: CleanTootsConfig): """Edit config file.""" if not _config_has_sections(config): click.pause() if sys.stdout.isatty() and sys.stdin.isatty(): click.edit(filename=config.main_file) else: click.secho("Not running in a terminal, can't open file.", fg="red")
def list_(config: CleanTootsConfig): """Display parsed config.""" if not _config_has_sections(config): return for section_name in config.sections(): click.secho(section_name, bold=True) section = config[section_name] for key, value in section.items(): click.secho(f"{key} = {value}") click.echo()
def clear_credentials(config: CleanTootsConfig): """Delete all credential files described in config file.""" if not _config_has_sections(config): return for section_name in config.sections(): section = config[section_name] try: os.remove(config.file(section.get("app_secret_file"))) except FileNotFoundError: pass try: os.remove(config.file(section.get("user_secret_file"))) except FileNotFoundError: pass click.secho(f"Removed files for {section_name}", fg="green")