Update routes/recommend.py
Browse files- routes/recommend.py +22 -10
routes/recommend.py
CHANGED
|
@@ -62,21 +62,33 @@ def recommend_tv():
|
|
| 62 |
}), 500
|
| 63 |
result = response.json()
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
print(f"TV microservice response: {result}")
|
| 67 |
-
print(f"Response type: {type(result)}")
|
| 68 |
-
|
| 69 |
-
# Handle different response formats (like book route)
|
| 70 |
if isinstance(result, list):
|
| 71 |
-
#
|
| 72 |
raw_items = result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
else:
|
| 74 |
-
#
|
| 75 |
if result.get("status") != "success":
|
| 76 |
error_msg = result.get("message") or result.get("error") or "Unknown error"
|
| 77 |
-
|
| 78 |
-
print(f"Full error response: {result}")
|
| 79 |
-
return jsonify({"error": error_msg, "full_response": result}), 500
|
| 80 |
raw_items = result.get(RESPONSE_KEYS.get(rec_type, "items"), [])
|
| 81 |
|
| 82 |
# Normalize response format
|
|
|
|
| 62 |
}), 500
|
| 63 |
result = response.json()
|
| 64 |
|
| 65 |
+
# Handle TV microservice's specific response format
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if isinstance(result, list):
|
| 67 |
+
# Direct list response
|
| 68 |
raw_items = result
|
| 69 |
+
elif "recommendations" in result:
|
| 70 |
+
# TV microservice format: {"genre": "Tragedy", "recommendations": [...]}
|
| 71 |
+
recommendations = result.get("recommendations", [])
|
| 72 |
+
# Convert string recommendations to objects
|
| 73 |
+
raw_items = []
|
| 74 |
+
for rec in recommendations:
|
| 75 |
+
if isinstance(rec, str):
|
| 76 |
+
# Convert string to object format
|
| 77 |
+
raw_items.append({
|
| 78 |
+
"name": rec,
|
| 79 |
+
"title": rec,
|
| 80 |
+
"description": "",
|
| 81 |
+
"genre": [result.get("genre", "")],
|
| 82 |
+
"rating": None
|
| 83 |
+
})
|
| 84 |
+
else:
|
| 85 |
+
# Already an object
|
| 86 |
+
raw_items.append(rec)
|
| 87 |
else:
|
| 88 |
+
# Standard format with status
|
| 89 |
if result.get("status") != "success":
|
| 90 |
error_msg = result.get("message") or result.get("error") or "Unknown error"
|
| 91 |
+
return jsonify({"error": error_msg}), 500
|
|
|
|
|
|
|
| 92 |
raw_items = result.get(RESPONSE_KEYS.get(rec_type, "items"), [])
|
| 93 |
|
| 94 |
# Normalize response format
|