diff --git a/weather_mood_journel-Pranav-GURUNG/README.md b/weather_mood_journel-Pranav-GURUNG/README.md new file mode 100644 index 0000000..0136f42 --- /dev/null +++ b/weather_mood_journel-Pranav-GURUNG/README.md @@ -0,0 +1,44 @@ +This PR introduces a Flask backend for the Weather Mood Journal, allowing users to log moods along with weather context and manage their entries. + +Features Implemented: + +Log Mood (POST /log) + +Create a new mood entry with date and weather. + +Optional mood; if not provided, a random mood is suggested based on the weather. + +Retrieve Entries (GET /entries) + +Returns all logged entries in JSON format. + +Get Suggested Moods (GET /moods?weather=) + +Returns a list of suggested moods corresponding to a specific weather type. + +Update Entry (PUT /update/) + +Update an existing entry’s date, weather, or mood using its timestamp. + +Delete Entry (DELETE /delete/) + +Delete an entry by timestamp. +CSV Storage & Privacy + +Entries are stored in mood_journal.csv. + +CSV is not committed to the repo to protect user data. + +.gitignore added to prevent committing sensitive logs. + +CSV is initialized with headers if it doesn’t exist. + +CSV Storage + +Entries are stored in mood_journal.csv. + +CSV is initialized with headers if it doesn’t exist. + +Mood Map Dictionary (mood_map.py) + +Stores moods associated with each weather type for automatic suggestions. \ No newline at end of file diff --git a/weather_mood_journel-Pranav-GURUNG/app.py b/weather_mood_journel-Pranav-GURUNG/app.py new file mode 100644 index 0000000..19e8c80 --- /dev/null +++ b/weather_mood_journel-Pranav-GURUNG/app.py @@ -0,0 +1,165 @@ +from flask import Flask, request, jsonify +import csv +from datetime import datetime +import os +import random + +from db import MOOD_MAP + +app = Flask(__name__) + +CSV_FILE = "mood_journal.csv" + +# Initialize CSV with headers if not present +if not os.path.exists(CSV_FILE): + with open(CSV_FILE, "w", newline="") as f: + writer = csv.writer(f) + writer.writerow(["timestamp", "date", "weather", "mood"]) + + +@app.route("/log", methods=["POST"]) +def log_mood(): + + data = request.get_json() + if not data: + return jsonify({"error": "Missing JSON body"}), 400 + + date = data.get("date") + weather = data.get("weather") + mood = data.get("mood") + + if not date or not weather: + return jsonify({"error": "Fields 'date' and 'weather' are required"}), 400 + + + if not mood: + mood_options = MOOD_MAP.get(weather, MOOD_MAP["Undefined"]) + mood = random.choice(mood_options) + + + with open(CSV_FILE, "a", newline="") as f: + writer = csv.writer(f) + writer.writerow([datetime.now().isoformat(), date, weather, mood]) + + return jsonify({ + "message": "Mood entry logged successfully", + "weather": weather, + "mood": mood + }), 201 + + +@app.route("/entries", methods=["GET"]) +def get_entries(): + + entries = [] + with open(CSV_FILE, "r") as f: + reader = csv.DictReader(f) + for row in reader: + entries.append(row) + return jsonify(entries), 200 + + +@app.route("/analytics", methods=["GET"]) +def analytics(): + + weather_count = {} + mood_count = {} + + with open(CSV_FILE, "r") as f: + reader = csv.DictReader(f) + for row in reader: + weather = row["weather"] + mood = row["mood"] + weather_count[weather] = weather_count.get(weather, 0) + 1 + mood_count[mood] = mood_count.get(mood, 0) + 1 + + return jsonify({ + "weather_summary": weather_count, + "mood_summary": mood_count + }), 200 + + +@app.route("/moods", methods=["GET"]) +def get_weather_moods(): + + weather = request.args.get("weather") + if not weather: + return jsonify({"error": "Missing 'weather' query parameter"}), 400 + + moods = MOOD_MAP.get(weather, MOOD_MAP["Undefined"]) + return jsonify({"weather": weather, "suggested_moods": moods}), 200 + +@app.route("/update/", methods=["PUT"]) +def update_entry(timestamp): + """ + Update an entry by its timestamp. + JSON body can include any of: date, weather, mood, notes + """ + data = request.get_json() + if not data: + return jsonify({"error": "Missing JSON body"}), 400 + + updated = False + all_entries = [] + + with open(CSV_FILE, "r") as f: + reader = csv.DictReader(f) + for row in reader: + if row["timestamp"] == timestamp: + # Update fields + row["date"] = data.get("date", row["date"]) + row["weather"] = data.get("weather", row["weather"]) + row["mood"] = data.get("mood", row["mood"]) + row["notes"] = data.get("notes", row["notes"]) + # Recalculate sentiment if notes changed + row["sentiment"] = analyze_sentiment(row["notes"]) + updated = True + all_entries.append(row) + + if not updated: + return jsonify({"error": "Entry not found"}), 404 + + # Rewrite CSV + with open(CSV_FILE, "w", newline="") as f: + writer = csv.DictWriter(f, fieldnames=all_entries[0].keys()) + writer.writeheader() + writer.writerows(all_entries) + + return jsonify({"message": "Entry updated successfully"}), 200 + + +@app.route("/delete/", methods=["DELETE"]) +def delete_entry(timestamp): + """ + Delete an entry by its timestamp + """ + deleted = False + all_entries = [] + + with open(CSV_FILE, "r") as f: + reader = csv.DictReader(f) + for row in reader: + if row["timestamp"] == timestamp: + deleted = True + continue + all_entries.append(row) + + if not deleted: + return jsonify({"error": "Entry not found"}), 404 + + # Rewrite CSV + if all_entries: + with open(CSV_FILE, "w", newline="") as f: + writer = csv.DictWriter(f, fieldnames=all_entries[0].keys()) + writer.writeheader() + writer.writerows(all_entries) + else: + # If no entries left, recreate header + with open(CSV_FILE, "w", newline="") as f: + writer = csv.writer(f) + writer.writerow(["timestamp", "date", "weather", "mood", "notes", "sentiment"]) + + return jsonify({"message": "Entry deleted successfully"}), 200 + +if __name__ == "__main__": + app.run(debug=True) diff --git a/weather_mood_journel-Pranav-GURUNG/db.py b/weather_mood_journel-Pranav-GURUNG/db.py new file mode 100644 index 0000000..23031ce --- /dev/null +++ b/weather_mood_journel-Pranav-GURUNG/db.py @@ -0,0 +1,21 @@ +# mood_map.py + +# You can expand this dictionary over time to refine analytics +MOOD_MAP = { + "Clear": ["Cheerful", "Energetic", "Creative"], + "Sunny": ["Motivated", "Joyful", "Productive"], + "Cloudy": ["Calm", "Thoughtful", "Balanced"], + "Overcast": ["Lazy", "Pensive", "Cozy"], + "Rainy": ["Peaceful", "Introspective", "Melancholic"], + "Drizzle": ["Dreamy", "Relaxed"], + "Thunderstorm": ["Anxious", "Restless", "Alert"], + "Snow": ["Peaceful", "Playful", "Reflective"], + "Windy": ["Excited", "Adventurous", "Restless"], + "Humid": ["Irritated", "Sluggish"], + "Foggy": ["Uncertain", "Sleepy", "Dreamy"], + "Hail": ["Surprised", "Agitated"], + "Breezy": ["Playful", "Curious", "Light-hearted"], + "Hot": ["Tired", "Slow", "Lazy"], + "Cold": ["Focused", "Reserved", "Chilled"], + "Undefined": ["Observe emotions mindfully"] +} diff --git a/weather_mood_journel-Pranav-GURUNG/update_csv.py b/weather_mood_journel-Pranav-GURUNG/update_csv.py new file mode 100644 index 0000000..22a89ba --- /dev/null +++ b/weather_mood_journel-Pranav-GURUNG/update_csv.py @@ -0,0 +1,20 @@ +import csv +import os + +old_file = "mood_journal.csv" +new_file = "mood_journal_new.csv" + +with open(old_file, "r", newline="") as infile, open(new_file, "w", newline="") as outfile: + reader = csv.DictReader(infile) + + fieldnames = ["timestamp", "user", "date", "weather", "mood", "notes", "sentiment"] + writer = csv.DictWriter(outfile, fieldnames=fieldnames) + writer.writeheader() + + for row in reader: + row["user"] = "default_user" + writer.writerow(row) + +os.replace(new_file, old_file) + +print("CSV updated successfully with 'user' column!")