Skip to content

Commit

Permalink
Retiring OutputAction for arbitrary additional CSV report in favor of…
Browse files Browse the repository at this point in the history
… CSVFileType

Fixing mc medias command
Fixing ct summary command
  • Loading branch information
Yomguithereal committed Nov 27, 2023
1 parent 9616e02 commit b8815c4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
19 changes: 19 additions & 0 deletions minet/cli/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,25 @@ def __call__(self, string):
return d


class CSVFileType:
def __init__(self, mode="r"):
self.mode = mode

def __call__(self, string):
if string == "-":
return acquire_cross_platform_stdout()

try:
# As per #254: newline='' is necessary for CSV output on windows to avoid
# outputting extra lines because of a '\r\r\n' end of line...
return open(string, self.mode, encoding="utf-8", newline="")
except OSError as e:
raise ArgumentTypeError(
"can't open '%(filename)s': %(error)s"
% {"filename": string, "error": e}
)


class SplitterType:
def __init__(self, splitchar=","):
self.splitchar = splitchar
Expand Down
4 changes: 2 additions & 2 deletions minet/cli/crowdtangle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
from casanova import RowCountResumer, LastCellResumer

from minet.cli.argparse import BooleanAction, SplitterType, OutputAction
from minet.cli.argparse import BooleanAction, SplitterType, CSVFileType

# TODO: lazyloading issue
from minet.crowdtangle.constants import (
Expand Down Expand Up @@ -282,7 +282,7 @@
{
"name": "--posts",
"help": "Path to a file containing the retrieved posts.",
"action": OutputAction,
"type": CSVFileType("w"),
},
{
"flag": "--sort-by",
Expand Down
4 changes: 2 additions & 2 deletions minet/cli/mediacloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from minet.cli.argparse import (
command,
ConfigAction,
OutputAction,
CSVFileType,
SplitterType,
)

Expand All @@ -23,7 +23,7 @@
{
"flag": "--feeds",
"help": "If given, path of the CSV file listing media RSS feeds.",
"action": OutputAction,
"type": CSVFileType("w"),
}
],
)
Expand Down
35 changes: 21 additions & 14 deletions minet/cli/mediacloud/medias.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,42 @@
)


def get_headers(cli_args):
headers = MEDIACLOUD_MEDIA_CSV_HEADER[1:]

if cli_args.feeds is not None:
headers.append("feeds")

return headers


@with_mediacloud_fatal_errors
@with_enricher_and_loading_bar(
headers=MEDIACLOUD_MEDIA_CSV_HEADER[1:], title="Fetching medias", unit="medias"
headers=get_headers, title="Fetching medias", unit="medias"
)
def action(cli_args, enricher, loading_bar):
added_headers = MEDIACLOUD_MEDIA_CSV_HEADER[1:]

feeds_writer = None

if cli_args.feeds:
added_headers.append("feeds")
feeds_writer = casanova.writer(
cli_args.feeds, fieldnames=MEDIACLOUD_FEED_CSV_HEADER
)

client = MediacloudAPIClient(cli_args.token)

for row, media_id in enricher.cells(cli_args.column, with_rows=True):
result = client.media(media_id)
result = result.as_csv_row()[1:]
with loading_bar.step():
result = client.media(media_id)
result = result.as_csv_row()[1:]

if cli_args.feeds:
feeds = client.feeds(media_id)
if cli_args.feeds:
assert feeds_writer is not None

enricher.writerow(row, result + [len(feeds)])
feeds = client.feeds(media_id)

for feed in feeds:
feeds_writer.writerow(feed.as_csv_row())
else:
enricher.writerow(row, result)
enricher.writerow(row, result + [len(feeds)])

loading_bar.advance()
for feed in feeds:
feeds_writer.writerow(feed.as_csv_row())
else:
enricher.writerow(row, result)

0 comments on commit b8815c4

Please sign in to comment.