Skip to content

Commit

Permalink
cli added
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothée Demares committed Oct 7, 2024
1 parent d566f51 commit 4e89552
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 3 deletions.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,100 @@ create_excel_from_template(TEMPLATE_PATH, output_path, urls)
# A file will be create here : app/usecase/excel_completion/data/output
```

### Entrypoints

#### CLI

##### Insight

- Commande

```sh
# Général (strategy : desktop or mobile)
python .\app\entrypoint\cli\main.py insight [URL] [strategy]
# Exemple
python .\app\entrypoint\cli\main.py insight https://www.alextraveylan.fr/fr desktop
```
- Output

```sh
[INFO|setup_logging|L39] 2024-10-07T09:36:48+0200: Logger initialized.
{
'performance': 99,
'accessibility': 100,
'best_practices': 100,
'seo': 100,
'first_contentful_paint': 218,
'largest_contentful_paint': 401,
'total_blocking_time': 87,
'cumulative_layout_shift': 0.0,
'speed_index': 1091
}
```

##### Eco-design

- Commande

```sh
# Général
python .\app\entrypoint\cli\main.py eco-design [URL]
# Exemple
python .\app\entrypoint\cli\main.py eco-design https://www.alextraveylan.fr/fr
```

- Output

```sh
[INFO|setup_logging|L39] 2024-10-07T09:37:18+0200: Logger initialized.
{
'url': 'https://www.alextraveylan.fr/fr',
'size': 297.026,
'nodes': 386,
'requests': 29,
'grade': <Grade.B: 'B'>,
'score': 76.0,
'ges': 1.48,
'water': 2.22,
'date': datetime.datetime(2024, 10, 7, 9, 37, 21, 605695),
'page_type': None
}
```

##### Network

- Commande

```sh
# Général
python .\app\entrypoint\cli\main.py network [URL]
# Exemple
python .\app\entrypoint\cli\main.py network https://www.alextraveylan.fr/fr
```

- Output

```sh
[INFO|setup_logging|L39] 2024-10-07T09:37:18+0200: Logger initialized.
{'total': 31, 'js': 19, 'css': 1}
```

##### Complete-excel

- Commande

```sh
# Général
python .\app\entrypoint\cli\main.py complete-excel [URLS...]
# Exemple
python .\app\entrypoint\cli\main.py complete-excel https://www.alextraveylan.fr/fr https://it-wars.com
```

- Output

Should open the created excel file.


### Tests

Execute tests:
Expand Down
Empty file added app/entrypoint/__init__.py
Empty file.
Empty file added app/entrypoint/cli/__init__.py
Empty file.
86 changes: 86 additions & 0 deletions app/entrypoint/cli/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import asyncio

import rich
import typer
from rich.progress import Progress, SpinnerColumn, TextColumn

from app.core.eco_index.scraper import EcoindexScraper
from app.core.insight.google_insight import DestopInsight, MobileInsight
from app.core.insight.schemas import InsightContent
from app.core.inspect_network.count_requests import InspectNetWork
from app.usecase.excel_completion.actions import (
create_excel_from_template,
open_excel_file,
)
from app.usecase.excel_completion.files_infos import (
TEMPLATE_PATH,
get_output_path,
)

app = typer.Typer()


@app.command()
def insight(url: str, strategy: str):
if strategy == "desktop":
insight_class = DestopInsight(url)

elif strategy == "mobile":
insight_class = MobileInsight(url)

else:
print("Stategy must be desktop or mobile")
raise typer.Exit()

with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress:
progress.add_task(description="Fetching data ...", total=None)
result: InsightContent = insight_class.get_result()

rich.print(result.model_dump())


@app.command()
def eco_index(url: str):
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress:
progress.add_task(description="Fetching data ...", total=None)
eco_index = asyncio.run(EcoindexScraper(url=url).get_page_analysis())

rich.print(eco_index.model_dump())


@app.command()
def network(url: str):
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress:
progress.add_task(description="Fetching data ...", total=None)
inspect = InspectNetWork(url=url).get_result()

rich.print(inspect.model_dump())


@app.command()
def complete_excel(urls: list[str]):
output_path = get_output_path()
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress:
progress.add_task(description="Fetching data ...", total=None)
create_excel_from_template(TEMPLATE_PATH, output_path, urls)
open_excel_file(output_path)


if __name__ == "__main__":
app()
4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def main():
eco_index = asyncio.run(EcoindexScraper(url=url).get_page_analysis())
print("\nEcoindex:\n", eco_index)

inpect = InspectNetWork(url=url)
print("\nNetwork requests:\n", inpect.get_result(), "\n")
inspect = InspectNetWork(url=url)
print("\nNetwork requests:\n", inspect.get_result(), "\n")


@register
Expand Down
16 changes: 16 additions & 0 deletions app/usecase/excel_completion/actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import logging
import os
import sys
from datetime import datetime
from pathlib import Path
from typing import List
Expand All @@ -8,6 +10,7 @@
from openpyxl.workbook.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet

from app.adapter.exception.app_exception import AppError
from app.core.constants import LOGGER_NAME
from app.core.eco_index.scraper import EcoindexScraper
from app.core.insight.google_insight import MobileInsight
Expand All @@ -22,6 +25,19 @@
logger = logging.getLogger(LOGGER_NAME)


def open_excel_file(fichier: Path | str) -> None:
fichier = str(fichier)
try:
if sys.platform == "win32":
os.startfile(fichier)
elif sys.platform == "darwin":
os.system(f"open {fichier}")
else:
os.system(f"xdg-open {fichier}")
except Exception as e:
raise AppError("Cannot open excel file") from e


def copy_sheet(
source_sheet: Worksheet, target_workbook: Workbook, new_title: str
) -> None:
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ playwright
pillow
openpyxl
setuptools
playwright-stealth
playwright-stealth
typer

0 comments on commit 4e89552

Please sign in to comment.