Arkm20 commited on
Commit
f0c04e9
·
verified ·
1 Parent(s): 749a12a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -4
app.py CHANGED
@@ -42,11 +42,15 @@ class TokenData(BaseModel):
42
  username: Optional[str] = None
43
 
44
  class EpisodeUpdate(BaseModel):
 
45
  show_id: str
46
- show_title: str
47
  season_number: int
48
  episode_number: int
49
 
 
 
 
 
50
  class UserBase(BaseModel):
51
  username: str
52
 
@@ -247,17 +251,63 @@ async def upload_profile_picture(
247
  )
248
 
249
 
250
- @app.post("/users/me/watch-history", status_code=status.HTTP_200_OK, tags=["User"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
  async def update_watch_history(
252
  episode_data: EpisodeUpdate,
253
  current_user: UserInDB = Depends(get_current_user)
254
  ):
255
  """
256
- Adds a new episode to the user's watch history.
 
257
  """
258
  users_db = load_users()
259
  user_data = users_db[current_user.username]
260
 
 
 
 
 
261
  # Create a unique identifier for the episode to prevent duplicates
262
  episode_id = f"{episode_data.show_id}_{episode_data.season_number}_{episode_data.episode_number}"
263
 
@@ -268,7 +318,13 @@ async def update_watch_history(
268
  )
269
 
270
  if not is_already_watched:
271
- user_data["watch_history"].append(episode_data.dict())
 
 
 
 
 
 
272
  save_users(users_db)
273
  return {"message": "Watch history updated."}
274
 
 
42
  username: Optional[str] = None
43
 
44
  class EpisodeUpdate(BaseModel):
45
+ """Data required to add an episode to the watch history."""
46
  show_id: str
 
47
  season_number: int
48
  episode_number: int
49
 
50
+ class WatchedEpisode(EpisodeUpdate):
51
+ """Represents a single episode in the user's watch history, including the timestamp."""
52
+ watch_timestamp: datetime
53
+
54
  class UserBase(BaseModel):
55
  username: str
56
 
 
251
  )
252
 
253
 
254
+ import status
255
+ from fastapi import Depends, APIRouter
256
+ from pydantic import BaseModel
257
+ from datetime import datetime, timezone
258
+ from typing import List
259
+
260
+ # Assume these are defined elsewhere in your application
261
+ # from .dependencies import get_current_user, load_users, save_users
262
+ # from .models import UserInDB
263
+
264
+ # --- Pydantic Models ---
265
+ # It's good practice to define your data models clearly.
266
+
267
+
268
+
269
+
270
+
271
+
272
+ @app.get("/users/me/watch-history", response_model=List[WatchedEpisode])
273
+ async def get_watch_history(
274
+ current_user: UserInDB = Depends(get_current_user)
275
+ ):
276
+ """
277
+ Retrieves the current user's watch history, sorted by most recently watched.
278
+ """
279
+ users_db = load_users()
280
+ user_data = users_db.get(current_user.username, {})
281
+
282
+ # Get the watch history, defaulting to an empty list if it doesn't exist
283
+ watch_history = user_data.get("watch_history", [])
284
+
285
+ # Optional: Sort the history so the most recently watched items are first
286
+ # The `key` lambda function handles cases where an old item might not have a timestamp.
287
+ watch_history.sort(
288
+ key=lambda item: item.get("watch_timestamp", "1970-01-01T00:00:00Z"),
289
+ reverse=True
290
+ )
291
+
292
+ return watch_history
293
+
294
+
295
+ @app.post("/users/me/watch-history", status_code=status.HTTP_200_OK)
296
  async def update_watch_history(
297
  episode_data: EpisodeUpdate,
298
  current_user: UserInDB = Depends(get_current_user)
299
  ):
300
  """
301
+ Adds a new episode with a timestamp to the user's watch history.
302
+ If the episode already exists, its timestamp is not updated.
303
  """
304
  users_db = load_users()
305
  user_data = users_db[current_user.username]
306
 
307
+ # Ensure watch_history list exists
308
+ if "watch_history" not in user_data:
309
+ user_data["watch_history"] = []
310
+
311
  # Create a unique identifier for the episode to prevent duplicates
312
  episode_id = f"{episode_data.show_id}_{episode_data.season_number}_{episode_data.episode_number}"
313
 
 
318
  )
319
 
320
  if not is_already_watched:
321
+ # Convert the Pydantic model to a dictionary to add the new field
322
+ new_episode_record = episode_data.dict()
323
+
324
+ # Add the UTC timestamp in standard ISO format
325
+ new_episode_record["watch_timestamp"] = datetime.now(timezone.utc).isoformat()
326
+
327
+ user_data["watch_history"].append(new_episode_record)
328
  save_users(users_db)
329
  return {"message": "Watch history updated."}
330