| from flask import Flask, jsonify, request |
| from pymongo import MongoClient |
| import random, re |
|
|
| app = Flask(__name__) |
|
|
| |
| MONGO_URI = "mongodb+srv://veratheai24:veratheai24@cluster0.b15rk44.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0" |
| client = MongoClient(MONGO_URI) |
| db = client["blogs"] |
| satsang = client["Satsang"] |
|
|
| def get_collection_for_type(s_type): |
| return satsang["satsangs_long"] if s_type == "long" else satsang["satsangs_short"] |
|
|
| |
|
|
| |
| def find_by_video_id(video_id): |
| col_long = satsang["satsangs_long"] |
| col_short = satsang["satsangs_short"] |
| q = {"youtube": {"$regex": f"{re.escape(video_id)}$"}} |
| doc = col_long.find_one(q, {"_id":0}) or col_short.find_one(q, {"_id":0}) |
| return doc |
|
|
| |
| |
| @app.route("/satsangs", methods=["GET"]) |
| def get_all_satsangs(): |
| satsang_type = request.args.get("type", "all") |
| docs = [] |
| if satsang_type in ("all", "", None): |
| docs += list(satsang["satsangs_long"].find({}, {"_id":0})) |
| docs += list(satsang["satsangs_short"].find({}, {"_id":0})) |
| else: |
| docs = list(get_collection_for_type(satsang_type).find({}, {"_id":0})) |
| return jsonify({"satsangs": docs}) |
|
|
|
|
| |
| @app.route("/satsangs/random", methods=["GET"]) |
| def get_random_satsang(): |
| s_type = request.args.get("type", "long") |
| tag = request.args.get("tag", None) |
| col = get_collection_for_type(s_type) |
| query = {} |
| if tag: |
| query["tags"] = {"$in": [tag.lower()]} |
| docs = list(col.find(query, {"_id":0})) |
| if not docs: |
| return jsonify({"error":"No satsangs found"}), 404 |
| return jsonify(random.choice(docs)) |
|
|
|
|
| |
| @app.route("/satsangs/search", methods=["GET"]) |
| def satsang_search(): |
| terms = request.args.get("q","").strip() |
| s_type = request.args.get("type","all") |
| if not terms: |
| return jsonify({"results": []}) |
| tokens = [t.lower() for t in terms.split() if t.strip()] |
| |
| cands = [] |
| if s_type in ("all",""): |
| cands += list(satsang["satsangs_long"].find({}, {"_id":0})) |
| cands += list(satsang["satsangs_short"].find({}, {"_id":0})) |
| else: |
| cands = list(get_collection_for_type(s_type).find({}, {"_id":0})) |
| |
| scored = [] |
| for d in cands: |
| title = (d.get("title") or "").lower() |
| desc = (d.get("description") or "").lower() |
| tags = [t.lower() for t in d.get("tags") or []] |
| score = 0 |
| for tk in tokens: |
| if tk in title: score += 5 |
| if any(tk in tg for tg in tags): score += 3 |
| if tk in desc: score += 1 |
| if score>0: |
| scored.append((score, d)) |
| scored.sort(key=lambda x: x[0], reverse=True) |
| return jsonify({"results":[d for _,d in scored[:25]]}) |
|
|
|
|
| |
| @app.route("/user/set_last_watched", methods=["POST"]) |
| def set_last_watched(): |
| data = request.get_json(force=True) |
| email = data.get("email") |
| video_id = data.get("video_id") |
| if not email or not video_id: |
| return jsonify({"error":"Missing email or video_id"}), 400 |
|
|
| satsang.users.update_one( |
| {"email": email}, |
| {"$set": {"last_video_id": video_id}}, |
| upsert=True |
| ) |
| |
| satsang.users.update_one( |
| {"email": email}, |
| {"$addToSet": {"history_ids": video_id}}, |
| upsert=True |
| ) |
| return jsonify({"status":"ok"}) |
|
|
|
|
| |
| @app.route("/user/last_watched", methods=["GET"]) |
| def get_last_watched(): |
| email = request.args.get("email") |
| if not email: |
| return jsonify({"error":"Missing email"}), 400 |
| user = satsang.users.find_one({"email": email}, {"_id":0, "last_video_id":1}) |
| return jsonify({"last_video_id": (user or {}).get("last_video_id")}) |
|
|
|
|
| |
| @app.route("/update_progress", methods=["POST"]) |
| def update_progress(): |
| data = request.get_json() |
| email = data.get("email") |
| video_id = data.get("video_id") |
| |
| if not email or not video_id: |
| return jsonify({"error":"Missing email or video_id"}), 400 |
| |
| satsang.users.update_one( |
| {"email": email}, |
| {"$addToSet": {"history_ids": video_id}}, |
| upsert=True |
| ) |
| |
| satsang.users.update_one( |
| {"email": email}, |
| {"$set": {"last_video_id": video_id}}, |
| upsert=True |
| ) |
| return jsonify({"status":"ok"}) |
|
|
|
|
| |
| @app.route("/mark_favorite", methods=["POST"]) |
| def mark_favorite(): |
| data = request.get_json() |
| email = data.get("email") |
| video_id = data.get("video_id") |
| if not email or not video_id: |
| return jsonify({"error":"Missing email or video_id"}), 400 |
| satsang.users.update_one({"email": email},{"$addToSet":{"favorites": video_id}}, upsert=True) |
| return jsonify({"status":"ok"}) |
| |
|
|
| |
| @app.route("/add_to_playlist", methods=["POST"]) |
| def add_to_playlist(): |
| data = request.get_json() |
| email = data.get("email"); playlist = data.get("playlist_name"); video_id = data.get("video_id") |
| if not email or not playlist or not video_id: |
| return jsonify({"error":"Missing fields"}), 400 |
| satsang.users.update_one({"email": email}, {"$addToSet": {f"playlists.{playlist}": video_id}}, upsert=True) |
| return jsonify({"status":"ok"}) |
|
|
|
|
| @app.route("/remove_from_playlist", methods=["POST"]) |
| def remove_from_playlist(): |
| data = request.get_json() |
| email = data.get("email"); playlist = data.get("playlist_name"); video_id = data.get("video_id") |
| if not email or not playlist or not video_id: |
| return jsonify({"error":"Missing fields"}), 400 |
| satsang.users.update_one({"email": email}, {"$pull": {f"playlists.{playlist}": video_id}}) |
| return jsonify({"status":"ok"}) |
|
|
|
|
| @app.route("/rename_playlist", methods=["POST"]) |
| def rename_playlist(): |
| data = request.get_json() |
| email = data.get("email") |
| old = data.get("old_name") |
| new = data.get("new_name") |
| if not email or not old or not new: |
| return jsonify({"error":"Missing fields"}), 400 |
|
|
| |
| user = satsang.users.find_one({"email": email}, {"_id":0, f"playlists.{old}":1}) |
| if not user or "playlists" not in user or old not in user["playlists"]: |
| return jsonify({"error":"Playlist not found"}), 404 |
|
|
| |
| satsang.users.update_one( |
| {"email": email}, |
| {"$rename": {f"playlists.{old}": f"playlists.{new}"}} |
| ) |
| return jsonify({"status":"ok"}) |
|
|
| |
|
|
| |
| @app.route("/get_user_saved", methods=["GET"]) |
| def get_user_saved(): |
| email = request.args.get("email") |
| if not email: |
| return jsonify({"error":"Missing email"}), 400 |
| user = satsang.users.find_one({"email": email}, {"_id":0}) |
| if not user: |
| return jsonify({"favorites": [], "playlists": {}, "history_ids": [], "last_video_id": None}) |
| resp = { |
| "favorites": user.get("favorites", []), |
| "playlists": user.get("playlists", {}), |
| "history_ids": user.get("history_ids", []), |
| "last_video_id": user.get("last_video_id") |
| } |
| return jsonify(resp) |
|
|
|
|
|
|
| |
|
|
| @app.route("/categories", methods=["GET"]) |
| def get_categories(): |
| collections = db.list_collection_names() |
| return jsonify({"categories": collections}) |
|
|
| @app.route("/blogs/<category>", methods=["GET"]) |
| def get_blogs(category): |
| collection = db[category] |
| blogs = list(collection.find({}, {"_id":0, "blogTitle":1, "image":1, "date":1, "category":1})) |
| return jsonify({"blogs": blogs}) |
|
|
| @app.route("/blog/details", methods=["GET"]) |
| def get_blog_details(): |
| title = request.args.get("title") |
| if not title: |
| return jsonify({"error":"Title parameter is required"}), 400 |
| for category in db.list_collection_names(): |
| collection = db[category] |
| blog = collection.find_one({"blogTitle": title}, {"_id":0}) |
| if blog: |
| return jsonify(blog) |
| return jsonify({"error":"Blog not found"}), 404 |
|
|
| if __name__ == "__main__": |
| app.run(host="0.0.0.0", port=7860, debug=True) |
|
|