recipe-manager/scripts/import_copymethat.py

87 lines
3.3 KiB
Python

import os
import glob
from typing import List, Dict
# Parsing logic should be implemented or imported here
def parse_copy_me_that_txt(file_content: str) -> Dict:
"""Stub: parsing logic for CopyMeThat .txt files."""
# TODO: Replace with actual parsing logic
return {}
def parse_copy_me_that_html(file_content: str) -> Dict:
"""Stub: parsing logic for CopyMeThat .html files."""
# TODO: Replace with actual parsing logic
return {}
class RecipeManager:
def __init__(self, store_path):
self.store_path = store_path
if not os.path.exists(self.store_path):
open(self.store_path, 'w').close()
def add_recipe(self, recipe: Dict):
with open(self.store_path, 'a', encoding='utf-8') as f:
f.write(f"{recipe}\n")
class CopyMeThatBulkImporter:
def __init__(self, export_dir: str, store_path: str):
self.export_dir = export_dir
self.manager = RecipeManager(store_path)
self.results = {
"imported": 0,
"errors": []
}
def import_all(self):
file_globs = [
os.path.join(self.export_dir, "*.txt"),
os.path.join(self.export_dir, "*.html")
]
files = []
for g in file_globs:
files.extend(glob.glob(g))
for file_path in files:
try:
with open(file_path, encoding='utf-8') as f:
content = f.read()
if file_path.endswith(".txt"):
recipe = parse_copy_me_that_txt(content)
elif file_path.endswith(".html"):
recipe = parse_copy_me_that_html(content)
else:
raise ValueError("Unsupported file type")
if not recipe or "title" not in recipe:
raise ValueError("Missing critical fields")
self.manager.add_recipe(recipe)
self.results["imported"] += 1
except Exception as e:
self.results["errors"].append({
"file": os.path.basename(file_path),
"error": str(e)
})
return self.results
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--export-dir', required=True, help='Directory with CopyMeThat exports')
parser.add_argument('--output', required=True, help='Recipe manager store path (jsonl)')
parser.add_argument('--report', required=False, help='Output report markdown')
args = parser.parse_args()
importer = CopyMeThatBulkImporter(export_dir=args.export_dir, store_path=args.output)
results = importer.import_all()
print('\nImport Summary:')
print(results)
if args.report:
with open(args.report, 'w', encoding='utf-8') as rf:
rf.write(f"# CopyMeThat Import Report\n\n")
rf.write(f"**Directory Scanned:** {args.export_dir}\n")
rf.write(f"**Target Store:** {args.output}\n\n")
rf.write(f"| Recipes Imported | Errors |\n|-----------------|--------|\n")
rf.write(f"| {results['imported']} | {len(results['errors'])} |\n\n")
if results['errors']:
rf.write("## Errors\n")
for err in results['errors']:
rf.write(f"- `{err['file']}`: {err['error']}\n")
if __name__ == "__main__":
main()