Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
feat(jsonà: Implement export to JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatelot committed May 3, 2022
1 parent dfecde2 commit cf22d8f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 10 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ Processing [####################################] 100%
</details>
### Export to JSON file
By default, the results are exported to a CSV file. But, you can specify to export the results to a JSON file.
```bash
ecoindex-cli analyze --url http://www.ecoindex.fr --export-format json
```
<details><summary>Result</summary>
```bash
📁️ Urls recorded in file `input/www.ecoindex.fr.csv`
There are 1 url(s), do you want to process? [Y/n]:
1 urls for 1 window size with 2 maximum workers
Processing [####################################] 100%
🙌️ File /tmp/ecoindex-cli/output/www.ecoindex.fr/2022-03-05_215320/results.json written !
```
</details>
### Multiple url analysis
```bash
Expand Down
12 changes: 10 additions & 2 deletions ecoindex_cli/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
get_window_sizes_from_args,
)
from ecoindex_cli.cli.helper import run_page_analysis
from ecoindex_cli.enums import ExportFormat
from ecoindex_cli.files import write_results_to_file, write_urls_to_file
from ecoindex_cli.logger import Logger
from ecoindex_cli.report.report import generate_report
Expand Down Expand Up @@ -57,6 +58,11 @@ def analyze(
default=None,
help="You can define the number of workers to use for the analysis. Default is the number of cpu cores",
),
export_format: Optional[ExportFormat] = Option(
default=ExportFormat.csv,
help="You can export the results in json or csv. Default is csv",
case_sensitive=False,
),
):
"""
Make an ecoindex analysis of given webpages or website. You
Expand Down Expand Up @@ -163,14 +169,16 @@ def analyze(
output_folder = (
f"/tmp/ecoindex-cli/output/{file_prefix}/{time_now.strftime('%Y-%d-%m_%H%M%S')}"
)
output_filename = f"{output_folder}/results.csv"
output_filename = f"{output_folder}/results.{export_format.value}"

if output_file:
output_filename = output_file
output_folder = dirname(output_filename)

Path(output_folder).mkdir(parents=True, exist_ok=True)
write_results_to_file(filename=output_filename, results=results)
write_results_to_file(
filename=output_filename, results=results, export_format=export_format
)
secho(f"🙌️ File {output_filename} written !", fg=colors.GREEN)
if html_report:
generate_report(
Expand Down
6 changes: 6 additions & 0 deletions ecoindex_cli/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from enum import Enum


class ExportFormat(Enum):
csv = "csv"
json = "json"
62 changes: 54 additions & 8 deletions ecoindex_cli/files.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,71 @@
from abc import ABC, abstractmethod
from csv import DictWriter
from json import dump
from os import makedirs
from os.path import exists
from typing import List
from typing import List, Optional

from ecoindex_scraper.models import Result

from ecoindex_cli.enums import ExportFormat


def create_folder(path: str) -> None:
if not exists(path):
makedirs(path)


def write_results_to_file(filename: str, results: List[Result]) -> None:
headers = results[0].__dict__
class File(ABC):
def __init__(
self,
filename: str,
results: List[Result],
export_format: Optional[ExportFormat] = ExportFormat.csv,
):
self.filename = filename
self.results = results
self.export_format = export_format

@abstractmethod
def write(self) -> None:
pass


class CsvFile(File):
def write(self) -> None:
headers = self.results[0].__dict__

with open(self.filename, "w") as fp:
writer = DictWriter(fp, fieldnames=headers)

writer.writeheader()
for ecoindex in self.results:
writer.writerow(ecoindex.__dict__)


class JsonFile(File):
def write(self) -> None:
with open(self.filename, "w") as fp:
dump(
obj=[ecoindex.__dict__ for ecoindex in self.results],
fp=fp,
indent=4,
default=str,
)


with open(filename, "w") as fp:
writer = DictWriter(fp, fieldnames=headers)
def write_results_to_file(
filename: str,
results: List[Result],
export_format: Optional[ExportFormat] = ExportFormat.csv,
) -> None:
print(export_format)
if export_format == ExportFormat.csv:
file = CsvFile(filename=filename, results=results, export_format=export_format)
elif export_format == ExportFormat.json:
file = JsonFile(filename=filename, results=results, export_format=export_format)

writer.writeheader()
for ecoindex in results:
writer.writerow(ecoindex.__dict__)
file.write()


def write_urls_to_file(file_prefix: str, urls: List[str]) -> None:
Expand Down

0 comments on commit cf22d8f

Please sign in to comment.