Skip to content

Commit

Permalink
feature: add subcomand to export Mastodon data
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Feb 14, 2024
1 parent 83d44c1 commit 5b3ded9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
21 changes: 21 additions & 0 deletions geotribu_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
parser_comments_read,
parser_images_optimizer,
parser_latest_content,
parser_mastodon_export,
parser_new_article,
parser_open_result,
parser_search_content,
Expand Down Expand Up @@ -321,6 +322,26 @@ def main(args: list[str] = None):
add_common_arguments(subcmd_search_image)
parser_search_image(subcmd_search_image)

# -- NESTED SUBPARSER : COMMENTS ---------------------------------------------------
subcmd_social = subparsers.add_parser(
"social",
aliases=["rezosocio", "social"],
help="Commandes liées aux réseaux sociaux.",
formatter_class=main_parser.formatter_class,
prog="social",
)
social_subparsers = subcmd_social.add_subparsers(title="Social", dest="cmd_social")

# Mastodon - Export
subcmd_social_matsodon_export = social_subparsers.add_parser(
"mastodon-export",
help="Exporter les données du compte Mastodon (listes, comptes suivis...).",
formatter_class=main_parser.formatter_class,
prog="mastodon-export",
)
add_common_arguments(subcmd_social_matsodon_export)
parser_mastodon_export(subcmd_social_matsodon_export)

# -- PARSE ARGS --------------------------------------------------------------------
set_default_subparser(
parser_to_update=main_parser,
Expand Down
93 changes: 93 additions & 0 deletions geotribu_cli/social/cmd_mastodon_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#! python3 # noqa: E265

# ############################################################################
# ########## IMPORTS #############
# ################################

# standard library
import argparse
import logging
from os import getenv
from pathlib import Path

# package
from geotribu_cli.constants import GeotribuDefaults
from geotribu_cli.social.mastodon_client import ExtendedMastodonClient

# 3rd party


# ############################################################################
# ########## GLOBALS #############
# ################################

logger = logging.getLogger(__name__)
defaults_settings = GeotribuDefaults()


# ############################################################################
# ########## CLI #################
# ################################


def parser_mastodon_export(
subparser: argparse.ArgumentParser,
) -> argparse.ArgumentParser:
"""Set the argument parser for subcommand.
Args:
subparser (argparse.ArgumentParser): parser to set up
Returns:
argparse.ArgumentParser: parser ready to use
"""

subparser.add_argument(
"-w",
"--where",
help="Dossier dans lequel exporter les fichiers.",
default=getenv(
"GEOTRIBU_MASTODON_EXPORT_DEST_FOLDER",
defaults_settings.geotribu_working_folder.joinpath("mastodon/"),
),
type=Path,
dest="dest_export_folder",
)

subparser.set_defaults(func=run)

return subparser


# ############################################################################
# ########## MAIN ################
# ################################


def run(args: argparse.Namespace):
"""Run the sub command logic.
Open result of a previous command.
Args:
args (argparse.Namespace): arguments passed to the subcommand
"""
logger.debug(f"Running {args.command} with {args}")

mastodon_client = ExtendedMastodonClient()
mastodon_client.export_data(
dest_path_following_accounts=Path(args.dest_export_folder).joinpath(
"mastodon_comptes_suivis_geotribu.csv"
),
dest_path_lists=Path(args.dest_export_folder).joinpath(
"mastodon_listes_geotribu.csv"
),
dest_path_lists_only_accounts=Path(args.dest_export_folder).joinpath(
"mastodon_comptes_des_listes_geotribu.csv"
),
)


# -- Stand alone execution
if __name__ == "__main__":
pass
1 change: 1 addition & 0 deletions geotribu_cli/subcommands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from geotribu_cli.rss.rss_reader import parser_latest_content # noqa: F401
from geotribu_cli.search.search_content import parser_search_content # noqa: F401
from geotribu_cli.search.search_image import parser_search_image # noqa: F401
from geotribu_cli.social.cmd_mastodon_export import parser_mastodon_export # noqa: F401

from .open_result import parser_open_result # noqa: F401
from .upgrade import parser_upgrade # noqa: F401

0 comments on commit 5b3ded9

Please sign in to comment.