Files
zmVault/recipe_markdown.py
T

100 lines
2.9 KiB
Python

"""
This script was originally based on
https://github.com/atiumcache/pure-recipe
but has been modified to remove functionality
that I consider extraneous.
What remains is only a basic wrapper for recipe_scrapers,
but out of an abundance of caution
I'm including pure-recipe's original license below.
---Zane Meyers
***
MIT License
Copyright (c) 2023 Andrew Attilio
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from recipe_scrapers import scrape_me
import argparse
def main():
args = parse_arguments()
try:
scraper = scrape_me(args.url)
md_content = generate_markdown(scraper)
print(md_content)
except Exception as e:
print(f"An error occurred: {str(e)}")
def generate_markdown(scraper) -> str:
"""
Generate markdown content from a recipe scraper object.
Args:
scraper: A recipe_scrapers scraper object
Returns:
str: Markdown formatted recipe content
"""
lines = []
title = scraper.title()
lines.append(f"# {title}")
lines.append(f"\n**Serves:** {scraper.yields()}")
lines.append(f"**Total Time:** {scraper.total_time()} mins")
lines.append(f"\n## Ingredients")
for ingredient in scraper.ingredients():
lines.append(f"- {ingredient}")
lines.append(f"\n## Instructions")
for index, instruction in enumerate(scraper.instructions_list()):
lines.append(f"{index + 1}. {instruction}")
return "\n".join(lines)
def parse_arguments() -> argparse.Namespace:
"""
Parse command-line arguments for the recipe formatter program.
argument:
- `url`: The URL of the recipe to view.
Returns:
Namespace: An argparse.Namespace object containing the parsed arguments.
"""
parser = argparse.ArgumentParser(
prog="Recipe Markdown", description="Format web recipes as Markdown."
)
parser.add_argument("url", help="URL of the recipe to scrape")
return parser.parse_args()
if __name__ == "__main__":
main()