from fastapi import FastAPI, HTTPException from datetime import datetime, date import dateutil.parser app = FastAPI() ben_franklin_virtues = { "Temperance": "Practice moderation in all things, especially eating and drinking. Don't overindulge.", "Silence": "Speak only when it contributes to the well-being of yourself or others. Avoid unnecessary chatter.", "Order": "Keep your personal and professional life organized. A tidy environment can lead to a tidy mind.", "Resolution": "Be determined in your actions. If you decide to do something, follow through until it's done.", "Frugality": "Be mindful of your spending. Save and invest wisely, and avoid unnecessary expenses.", "Industry": "Make the most of your time. Be productive and avoid idle activities.", "Sincerity": "Be genuine and honest in your interactions with others. Avoid deceit.", "Justice": "Treat others fairly and justly. Don't harm others by your actions or neglect.", "Moderation": "Avoid extremes. Be moderate in your actions and avoid holding extreme opinions.", "Cleanliness": "Keep your body, clothes, and living spaces clean. Good hygiene and cleanliness can contribute to good health.", "Tranquility": "Don't let minor issues upset you. Stay calm and composed, even in difficult situations.", "Chastity": "Respect your body and the bodies of others. Engage in sexual activities responsibly and consensually.", "Humility": "Be humble. Don't think too highly of yourself, and appreciate the value in others." } @app.get("/") async def virtue(day: str = None): """ Get the virtue of the week """ if day is None: current_date = date.today() else: try: current_date = dateutil.parser.parse(day).date() except: raise HTTPException(status_code=400, detail="Invalid date format, please provide a date in the month/day/year format.") current_week = current_date.isocalendar()[1] virtues = list(ben_franklin_virtues.keys()) virtue_of_the_week = virtues[(current_week - 1) % 13] return { "virtue": virtue_of_the_week, "content": ben_franklin_virtues[virtue_of_the_week] }