Skip to content

Commit

Permalink
feat: SLD parser & finder
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNumericite committed Oct 22, 2024
1 parent 1779ec0 commit a995b27
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 0 deletions.
56 changes: 56 additions & 0 deletions scripts/obiz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Rope
.ropeproject

# Django stuff:
*.log
*.pot

# Sphinx documentation
docs/_build/

# idea
.idea/

# Specific
inputs/
134 changes: 134 additions & 0 deletions scripts/obiz/finders/sousgenre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import json
from typing import Dict, List
from colorama import init, Fore, Style

def format_reduction(reduction: float, variable_remise: str, is_variable: str) -> str:
"""
Formate la réduction en fonction des conditions
"""
if is_variable == 'True':
return variable_remise
return f"{reduction:.1f}"


def format_price(price_str: str) -> str:
"""
Formate un prix en string avec 2 décimales et le symbole €
"""
try:
# Remplace la virgule par un point pour la conversion
price_float = float(price_str.replace(',', '.'))
# Formate le prix avec 2 décimales et le symbole €
return f"{price_float:.2f} €"
except (ValueError, AttributeError):
return "Prix non disponible"


def calculate_reduction(prix_public: str, prix_reduc: str) -> float:
"""
Calcule le pourcentage de réduction entre deux prix
"""
try:
prix_public_float = float(prix_public.replace(',', '.'))
prix_reduc_float = float(prix_reduc.replace(',', '.'))

if prix_public_float == 0:
return 0.0

reduction = ((prix_public_float - prix_reduc_float) / prix_public_float) * 100
return reduction
except (ValueError, AttributeError):
return 0.0


def search_sousgenre_articles(file_paths: List[str], sousgenre_name: str) -> Dict[str, List[Dict]]:
"""
Recherche un sous-genre par son nom dans plusieurs fichiers et retourne tous ses articles associés
"""
all_results = {}

for file_path in file_paths:
source_name = file_path.split('/')[-1].split('.')[0]

with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)

results = []

for catalogue in data.get('catalogues', []):
for cat in catalogue.get('catalogue', []):
for genre in cat.get('genres', []):
for gen in genre.get('genre', []):
for sousgenre in gen.get('sousgenres', []):
for sg in sousgenre.get('sousgenre', []):
if sg.get('sousgenres_nom', '').lower() == sousgenre_name.lower():
sousgenre_info = {
'nom': sg.get('sousgenres_nom', ''),
'genre_nom': gen.get('genres_nom', ''),
'url': sg.get('sousgenres_url', ''),
'description': sg.get('sousgenres_descriptif', {}).get('#cdata-section', ''),
'articles': []
}

for articles in sg.get('articles', []):
for article in articles.get('article', []):
if article['articles_actif'] == "True":
article_info = {
'nom': article.get('articles_nom', ''),
'prix_public': article.get('articles_prix_public', ''),
'prix_reduc_ttc': article.get('articles_puttc', ''),
'variable': article.get('articles_valeur_variable', ''),
'variable_remise': article.get('articles_remise_btob', ''),
'code': article.get('articles_code', ''),
'type': article.get('articles_type', ''),
'description': article.get('articles_descriptif', {}).get(
'#cdata-section', '')
}
sousgenre_info['articles'].append(article_info)

results.append(sousgenre_info)

if results:
all_results[source_name] = results

return all_results


def print_sousgenre_details(results: Dict[str, List[Dict]]) -> None:
"""
Affiche les détails d'un sous-genre et ses articles de manière formatée pour chaque source
"""
if not results:
print("Aucun sous-genre trouvé avec ce nom dans aucune source.")
return

for source, source_results in results.items():
print(f"\n{'=' * 20} Source: {source} {'=' * 20}")

for result in source_results:
print(f"\nSous-genre: {result['nom']} (genre - {result['genre_nom']})")
print(f"URL: {result['url']}")
print("\nArticles associés:")
print("-" * 30)

if not result['articles']:
print("Aucun article trouvé pour ce sous-genre.")

for article in result['articles']:
prix_public = article['prix_public']
prix_reduc = article['prix_reduc_ttc']
reduction = calculate_reduction(prix_public, prix_reduc)

formatted_reduction = format_reduction(
reduction,
article['variable_remise'],
article['variable']
)

print(f"\nNom: {Style.BRIGHT}{Fore.BLUE}{article['nom']}{Style.RESET_ALL}")
print(f"Réduction: {Style.BRIGHT}{Fore.GREEN}{formatted_reduction}%{Style.RESET_ALL}")
print(f"Valeur variable : {'Oui' if article['variable'] == 'True' else 'Non'}")
print(f"Code: {article['code']}")
print(f"Type: {article['type']}")
print(f"Prix public: {format_price(prix_public)}")
print(f"Prix avec réduc ttc : {format_price(prix_reduc)}")
59 changes: 59 additions & 0 deletions scripts/obiz/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import json
from parsers.articles import get_article_names
from parsers.sousgenres import get_sousgenre_names
from finders.sousgenre import search_sousgenre_articles, print_sousgenre_details

def main():
file_paths = [
'inputs/reduccine.fr-preprod.json',
'inputs/reduckdo.fr-preprod.json',
'inputs/reducparc.fr-preprod.json'
]

try:
while True:
print("\nQue souhaitez-vous faire ?")
print("1. Afficher tous les noms d'articles")
print("2. Afficher tous les noms de sous-genres")
print("3. Rechercher un sous-genre spécifique")
print("4. Quitter")

choice = input("\nVotre choix (1-4): ")

if choice == "1":
all_articles = get_article_names(file_paths)
print("\nListe des noms d'articles par source :")
for source, articles in all_articles.items():
print(f"\n{'-' * 20} {source} {'-' * 20}")
for name in articles:
print(f"- {name}")

elif choice == "2":
all_sousgenres = get_sousgenre_names(file_paths)
print("\nListe des noms de sous-genres par source :")
for source, sousgenres in all_sousgenres.items():
print(f"\n{'-' * 20} {source} {'-' * 20}")
for name in sousgenres:
print(f"- {name}")

elif choice == "3":
search_name = input("\nEntrez le nom du sous-genre à rechercher: ")
results = search_sousgenre_articles(file_paths, search_name)
print_sousgenre_details(results)

elif choice == "4":
print("Au revoir!")
break

else:
print("Choix invalide. Veuillez réessayer.")

except FileNotFoundError as e:
print(f"Le fichier n'a pas été trouvé: {str(e)}")
except json.JSONDecodeError as e:
print(f"Erreur lors de la lecture du fichier JSON: {str(e)}")
except Exception as e:
print(f"Une erreur est survenue : {str(e)}")

if __name__ == "__main__":
main()
32 changes: 32 additions & 0 deletions scripts/obiz/parsers/articles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json
from typing import List


def get_article_names(file_paths: List[str]) -> dict:
"""
Extraire tous les noms d'articles des fichiers JSON spécifiés
"""
all_articles = {}

for file_path in file_paths:
source_name = file_path.split('/')[-1].split('.')[0] # Extraire le nom du fichier

with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)

article_names = []

for catalogue in data.get('catalogues', []):
for cat in catalogue.get('catalogue', []):
for genre in cat.get('genres', []):
for gen in genre.get('genre', []):
for sousgenre in gen.get('sousgenres', []):
for sg in sousgenre.get('sousgenre', []):
for articles in sg.get('articles', []):
for article in articles.get('article', []):
if 'articles_nom' in article:
article_names.append(article['articles_nom'])

all_articles[source_name] = article_names

return all_articles
30 changes: 30 additions & 0 deletions scripts/obiz/parsers/sousgenres.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import json
from typing import List


def get_sousgenre_names(file_paths: List[str]) -> dict:
"""
Extraire tous les noms de sous-genres des fichiers JSON spécifiés
"""
all_sousgenres = {}

for file_path in file_paths:
source_name = file_path.split('/')[-1].split('.')[0]

with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)

sousgenre_names = []

for catalogue in data.get('catalogues', []):
for cat in catalogue.get('catalogue', []):
for genre in cat.get('genres', []):
for gen in genre.get('genre', []):
for sousgenre in gen.get('sousgenres', []):
for sg in sousgenre.get('sousgenre', []):
if 'sousgenres_nom' in sg:
sousgenre_names.append(sg['sousgenres_nom'])

all_sousgenres[source_name] = sousgenre_names

return all_sousgenres
1 change: 1 addition & 0 deletions webapp/src/pages/widget/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Widget CJE

### Générer un token

#### Endpoint
Expand Down

0 comments on commit a995b27

Please sign in to comment.