Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -527,7 +527,7 @@ def generate_image_edit():
|
|
| 527 |
except Exception as e:
|
| 528 |
return jsonify({'error': str(e)}), 500
|
| 529 |
|
| 530 |
-
#----------View
|
| 531 |
@app.route('/api/view/projects', methods=['GET'])
|
| 532 |
def view_projects():
|
| 533 |
try:
|
|
@@ -555,7 +555,8 @@ def view_projects():
|
|
| 555 |
"generation_times": story_record.get("generation_times", {}),
|
| 556 |
"created_at": story_record.get("created_at", ""),
|
| 557 |
"input_type": story_record.get("input_type", ""),
|
| 558 |
-
"story_type": story_record.get("story_type", "")
|
|
|
|
| 559 |
}
|
| 560 |
|
| 561 |
# Optionally, sort the projects by creation date (newest first)
|
|
@@ -567,6 +568,48 @@ def view_projects():
|
|
| 567 |
except Exception as e:
|
| 568 |
return jsonify({'error': str(e)}), 500
|
| 569 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 570 |
# ---------- Credit Request Endpoints ----------
|
| 571 |
|
| 572 |
@app.route('/api/user/request-credits', methods=['POST'])
|
|
|
|
| 527 |
except Exception as e:
|
| 528 |
return jsonify({'error': str(e)}), 500
|
| 529 |
|
| 530 |
+
#----------View projects and videos Endpoints ----------
|
| 531 |
@app.route('/api/view/projects', methods=['GET'])
|
| 532 |
def view_projects():
|
| 533 |
try:
|
|
|
|
| 555 |
"generation_times": story_record.get("generation_times", {}),
|
| 556 |
"created_at": story_record.get("created_at", ""),
|
| 557 |
"input_type": story_record.get("input_type", ""),
|
| 558 |
+
"story_type": story_record.get("story_type", ""),
|
| 559 |
+
"video_url": story_record.get("video_url", "") # Include video URL if present
|
| 560 |
}
|
| 561 |
|
| 562 |
# Optionally, sort the projects by creation date (newest first)
|
|
|
|
| 568 |
except Exception as e:
|
| 569 |
return jsonify({'error': str(e)}), 500
|
| 570 |
|
| 571 |
+
|
| 572 |
+
@app.route('/api/view/videos', methods=['GET'])
|
| 573 |
+
def view_videos():
|
| 574 |
+
"""
|
| 575 |
+
Returns only the stories that have a 'video_url' field,
|
| 576 |
+
meaning the video has been generated.
|
| 577 |
+
"""
|
| 578 |
+
try:
|
| 579 |
+
# --- Authentication ---
|
| 580 |
+
auth_header = request.headers.get('Authorization', '')
|
| 581 |
+
if not auth_header.startswith('Bearer '):
|
| 582 |
+
return jsonify({'error': 'Missing or invalid token'}), 401
|
| 583 |
+
token = auth_header.split(' ')[1]
|
| 584 |
+
uid = verify_token(token)
|
| 585 |
+
if not uid:
|
| 586 |
+
return jsonify({'error': 'Invalid or expired token'}), 401
|
| 587 |
+
|
| 588 |
+
# --- Query Stories for the Authenticated User ---
|
| 589 |
+
stories_ref = db.reference('stories')
|
| 590 |
+
all_stories = stories_ref.get() or {}
|
| 591 |
+
user_videos = {}
|
| 592 |
+
|
| 593 |
+
for story_id, story_record in all_stories.items():
|
| 594 |
+
# Must belong to current user and have a video_url
|
| 595 |
+
if story_record.get('uid') == uid and story_record.get('video_url'):
|
| 596 |
+
user_videos[story_id] = {
|
| 597 |
+
"story_id": story_id,
|
| 598 |
+
"full_story": story_record.get("full_story", ""),
|
| 599 |
+
"sections": story_record.get("sections", []),
|
| 600 |
+
"video_url": story_record.get("video_url", ""),
|
| 601 |
+
"created_at": story_record.get("created_at", "")
|
| 602 |
+
}
|
| 603 |
+
|
| 604 |
+
# Sort by creation date (newest first), if needed
|
| 605 |
+
sorted_videos = dict(
|
| 606 |
+
sorted(user_videos.items(), key=lambda item: item[1]["created_at"], reverse=True)
|
| 607 |
+
)
|
| 608 |
+
|
| 609 |
+
return jsonify({"videos": sorted_videos})
|
| 610 |
+
except Exception as e:
|
| 611 |
+
return jsonify({'error': str(e)}), 500
|
| 612 |
+
|
| 613 |
# ---------- Credit Request Endpoints ----------
|
| 614 |
|
| 615 |
@app.route('/api/user/request-credits', methods=['POST'])
|