Skip to content

Commit

Permalink
Fonctionnalité : export des comptes et listes du compte Mastodon (#172)
Browse files Browse the repository at this point in the history
Et utilise le package Mastodon.py pour communiquer avec l'API Mastodon.

Voir halcy/Mastodon.py#369
  • Loading branch information
Guts authored Feb 14, 2024
2 parents fc769ea + db00166 commit d928bcd
Show file tree
Hide file tree
Showing 19 changed files with 731 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
run: python -m pip install -e .[all]

- name: Unit tests
env:
GEOTRIBU_MASTODON_API_ACCESS_TOKEN: ${{ secrets.GEOTRIBU_MASTODON_API_ACCESS_TOKEN }}
run: pytest

- name: Upload coverage to Codecov
Expand Down
18 changes: 18 additions & 0 deletions docs/usage/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,21 @@ A partir d'un dossier local :
```sh
geotribu images optimize ~/Images/Geotribu/images/
```

----

## Réseaux sociaux

### Exporter les données du compte Mastodon

Utile pour le partage des comptes suivis et listes (voir [cet article](https://geotribu.fr/articles/2024/2024-02-16_de-twitter-a-mastodon-guide-geo-import-liste-comptes/))

```sh
geotribu social mastodon-export
```

Préciser le dossier de sortie :

```sh
geotribu social mastodon-export -w ./export-mastodon
```
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
5 changes: 0 additions & 5 deletions geotribu_cli/comments/comments_broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,3 @@ def run(args: argparse.Namespace):
# open a result
if args.opt_auto_open_disabled:
open_uri(in_filepath=online_post.get("url"))


# -- Stand alone execution
if __name__ == "__main__":
pass
5 changes: 0 additions & 5 deletions geotribu_cli/comments/comments_latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,3 @@ def run(args: argparse.Namespace):
else:
print(":person_shrugging: Aucun commentaire trouvé")
sys.exit(0)


# -- Stand alone execution
if __name__ == "__main__":
pass
5 changes: 0 additions & 5 deletions geotribu_cli/comments/comments_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,3 @@ def run(args: argparse.Namespace):
)
else:
open_uri(in_filepath=comment_obj.url_to_comment)


# -- Stand alone execution
if __name__ == "__main__":
pass
5 changes: 0 additions & 5 deletions geotribu_cli/content/new_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,3 @@ def run(args: argparse.Namespace):
# open a result
if args.opt_auto_open_disabled:
open_uri(in_filepath=out_filepath)


# -- Stand alone execution
if __name__ == "__main__":
pass
5 changes: 0 additions & 5 deletions geotribu_cli/images/images_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,3 @@ def run(args: argparse.Namespace):
"images/optim"
)
)


# -- Stand alone execution
if __name__ == "__main__":
pass
5 changes: 0 additions & 5 deletions geotribu_cli/rss/rss_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,3 @@ def run(args: argparse.Namespace):
content_uri=feed_items[int(result_to_open)].url,
application=getenv("GEOTRIBU_OPEN_WITH", "shell"),
)


# -- Stand alone execution
if __name__ == "__main__":
pass
11 changes: 3 additions & 8 deletions geotribu_cli/search/search_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ def run(args: argparse.Namespace):

# crée un résultat de sortie
out_result = {
"type": "Article"
if result.get("ref").startswith("articles/")
else "GeoRDP",
"type": (
"Article" if result.get("ref").startswith("articles/") else "GeoRDP"
),
"date": rezult_date,
"score": f"{result.get('score'):.3}",
"url": f"{defaults_settings.site_base_url}{result.get('ref')}",
Expand Down Expand Up @@ -460,8 +460,3 @@ def run(args: argparse.Namespace):
content_uri=final_results[int(result_to_open)].get("url"),
application=getenv("GEOTRIBU_OPEN_WITH", "shell"),
)


# -- Stand alone execution
if __name__ == "__main__":
pass
5 changes: 0 additions & 5 deletions geotribu_cli/search/search_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,3 @@ def run(args: argparse.Namespace):
content_uri=final_results[int(result_to_open)].get("url"),
application=getenv("GEOTRIBU_OPEN_WITH", "shell"),
)


# -- Stand alone execution
if __name__ == "__main__":
pass
88 changes: 88 additions & 0 deletions geotribu_cli/social/cmd_mastodon_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#! 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"
),
)
Loading

0 comments on commit d928bcd

Please sign in to comment.