Spaces:
Running
Running
feat: add duplicate comment prevention and video comment status check endpoints
Browse files- app/routes/commend/router.py +59 -1
app/routes/commend/router.py
CHANGED
|
@@ -23,6 +23,12 @@ from .youtube_service import (
|
|
| 23 |
is_oauth_configured
|
| 24 |
)
|
| 25 |
from .gemini_service import generate_comment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
from ...services.logging_config import get_logger
|
| 27 |
|
| 28 |
logger = get_logger(__name__)
|
|
@@ -95,6 +101,7 @@ class PostCommentResponse(BaseModel):
|
|
| 95 |
message: str
|
| 96 |
commentId: Optional[str] = None
|
| 97 |
postedAt: Optional[str] = None
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
class CommendHealthResponse(BaseModel):
|
|
@@ -217,9 +224,24 @@ async def post_comment_to_youtube(request: PostCommentRequest):
|
|
| 217 |
|
| 218 |
Note: This requires valid OAuth credentials with YouTube posting permissions.
|
| 219 |
Comments include an AI disclaimer as required by platform policies.
|
|
|
|
| 220 |
"""
|
| 221 |
video_id = extract_video_id(request.videoUrl)
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
# Post to YouTube
|
| 224 |
result, error = post_youtube_comment(video_id, request.commentText)
|
| 225 |
|
|
@@ -230,14 +252,50 @@ async def post_comment_to_youtube(request: PostCommentRequest):
|
|
| 230 |
detail=error
|
| 231 |
)
|
| 232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
return PostCommentResponse(
|
| 234 |
status="success",
|
| 235 |
message="Comment posted successfully",
|
| 236 |
commentId=result.get('commentId'),
|
| 237 |
-
postedAt=result.get('postedAt')
|
|
|
|
| 238 |
)
|
| 239 |
|
| 240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
@router.get("/styles")
|
| 242 |
async def get_comment_styles():
|
| 243 |
"""Get available comment styles with descriptions."""
|
|
|
|
| 23 |
is_oauth_configured
|
| 24 |
)
|
| 25 |
from .gemini_service import generate_comment
|
| 26 |
+
from .comment_tracker import (
|
| 27 |
+
is_video_commented,
|
| 28 |
+
mark_video_commented,
|
| 29 |
+
get_video_comment_info,
|
| 30 |
+
get_comment_stats
|
| 31 |
+
)
|
| 32 |
from ...services.logging_config import get_logger
|
| 33 |
|
| 34 |
logger = get_logger(__name__)
|
|
|
|
| 101 |
message: str
|
| 102 |
commentId: Optional[str] = None
|
| 103 |
postedAt: Optional[str] = None
|
| 104 |
+
alreadyCommented: bool = False
|
| 105 |
|
| 106 |
|
| 107 |
class CommendHealthResponse(BaseModel):
|
|
|
|
| 224 |
|
| 225 |
Note: This requires valid OAuth credentials with YouTube posting permissions.
|
| 226 |
Comments include an AI disclaimer as required by platform policies.
|
| 227 |
+
Each video can only receive ONE comment to prevent spam/bot detection.
|
| 228 |
"""
|
| 229 |
video_id = extract_video_id(request.videoUrl)
|
| 230 |
|
| 231 |
+
# Check if already commented on this video
|
| 232 |
+
if is_video_commented(video_id):
|
| 233 |
+
comment_info = get_video_comment_info(video_id)
|
| 234 |
+
logger.warning(f"Duplicate comment attempt for video: {video_id}")
|
| 235 |
+
raise HTTPException(
|
| 236 |
+
status_code=status.HTTP_409_CONFLICT,
|
| 237 |
+
detail={
|
| 238 |
+
"message": "Bu videoya zaten yorum yapılmış / Already commented on this video",
|
| 239 |
+
"alreadyCommented": True,
|
| 240 |
+
"previousCommentId": comment_info.get('comment_id') if comment_info else None,
|
| 241 |
+
"postedAt": comment_info.get('posted_at') if comment_info else None
|
| 242 |
+
}
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
# Post to YouTube
|
| 246 |
result, error = post_youtube_comment(video_id, request.commentText)
|
| 247 |
|
|
|
|
| 252 |
detail=error
|
| 253 |
)
|
| 254 |
|
| 255 |
+
# Mark video as commented (for duplicate prevention)
|
| 256 |
+
video_details, _ = get_video_details(request.videoUrl)
|
| 257 |
+
video_title = video_details.get('title') if video_details else None
|
| 258 |
+
mark_video_commented(
|
| 259 |
+
video_id=video_id,
|
| 260 |
+
comment_id=result.get('commentId'),
|
| 261 |
+
video_title=video_title
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
return PostCommentResponse(
|
| 265 |
status="success",
|
| 266 |
message="Comment posted successfully",
|
| 267 |
commentId=result.get('commentId'),
|
| 268 |
+
postedAt=result.get('postedAt'),
|
| 269 |
+
alreadyCommented=False
|
| 270 |
)
|
| 271 |
|
| 272 |
|
| 273 |
+
@router.get("/check/{video_id}")
|
| 274 |
+
async def check_video_comment_status(video_id: str):
|
| 275 |
+
"""
|
| 276 |
+
Check if a video has already been commented on.
|
| 277 |
+
Frontend uses this to disable the post button if already commented.
|
| 278 |
+
"""
|
| 279 |
+
is_commented = is_video_commented(video_id)
|
| 280 |
+
comment_info = get_video_comment_info(video_id) if is_commented else None
|
| 281 |
+
|
| 282 |
+
return {
|
| 283 |
+
"videoId": video_id,
|
| 284 |
+
"alreadyCommented": is_commented,
|
| 285 |
+
"commentInfo": comment_info
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
@router.get("/stats")
|
| 290 |
+
async def get_commend_stats():
|
| 291 |
+
"""Get overall comment statistics."""
|
| 292 |
+
stats = get_comment_stats()
|
| 293 |
+
return {
|
| 294 |
+
"totalComments": stats.get("total_comments", 0),
|
| 295 |
+
"lastUpdated": stats.get("last_updated")
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
|
| 299 |
@router.get("/styles")
|
| 300 |
async def get_comment_styles():
|
| 301 |
"""Get available comment styles with descriptions."""
|