From 42f8738777d019599ac1ecf6064bf4c60721c424 Mon Sep 17 00:00:00 2001 From: Salman Faris Date: Mon, 6 Jul 2026 15:46:57 +0530 Subject: [PATCH 1/2] Fix cross-team asset 401 and stale label associations The QC script was failing with 401 when adding assets to playlists because GET /v4/assets returns cross-team assets that the token's RLS policy blocks on insert (42501 insufficient_privilege). Fixed by fetching the current team_id and filtering assets by team_id in the query. Also fixed delete_playlist() to explicitly remove labels/playlists associations before deleting a playlist, preventing stale rows that caused POST /v4/labels/playlists to fail on subsequent runs. Additional improvements: - Add resolution=ignore-duplicates to POST /v4/labels/playlists as a safety net against any remaining stale associations - Add progress logging in create_qc_playlist() to make it clear which step fails when errors occur - Rename error message from "Unable to create playlist" to "QC playlist setup failed" to better reflect that the failure may occur in asset-adding or label-linking steps, not just creation --- automated_quality_control.py | 53 ++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/automated_quality_control.py b/automated_quality_control.py index ed9de8b..001c7f4 100644 --- a/automated_quality_control.py +++ b/automated_quality_control.py @@ -16,26 +16,36 @@ PLAYLIST_PREFIX = "QC" -def get_ten_random_assets(): +def get_team_id() -> str: """ - Return 10 random assets in the account. + Return the current account's team ID via the built-in all-screens label. """ - response = requests.get( - 'https://api.screenlyapp.com/v4/assets?select=id&type=in.("appweb","audio","edge-app","image","video","web")&status=in.("finished","processing")', + "https://api.screenlyapp.com/v4/labels?type=eq.all-screens", headers=REQUEST_HEADERS, ) response.raise_for_status() + labels = response.json() + if not labels: + raise ValueError("No 'all-screens' label found in the account") + return labels[0]["team_id"] + - asset_count = len(response.json()) +def get_ten_random_assets(team_id: str) -> List[str]: + """ + Return 10 random asset IDs that belong to the current team. + Filtering by team_id ensures the token has permission to add them to playlists. + """ + response = requests.get( + f'https://api.screenlyapp.com/v4/assets?select=id&team_id=eq.{team_id}&type=in.("appweb","audio","edge-app","image","video","web")&status=in.("finished","processing")', + headers=REQUEST_HEADERS, + ) + response.raise_for_status() - # Pick 10 random assets - asset_list = [] - for i in range(10): - random_index = random.randint(0, asset_count - 1) - asset_list.append(response.json()[random_index]["id"]) + assets = response.json() + asset_count = len(assets) - return asset_list + return [assets[random.randint(0, asset_count - 1)]["id"] for _ in range(10)] def get_screens() -> List[Dict[str, Any]]: @@ -102,9 +112,13 @@ def get_qc_playlist_ids(): def delete_playlist(playlist_id): """ - Delete a playlist and its items. In v4, playlist items must be - removed before the playlist itself can be deleted. + Delete a playlist and its items. In v4, label associations and playlist + items must be removed before the playlist itself can be deleted. """ + requests.delete( + f"https://api.screenlyapp.com/v4/labels/playlists?playlist_id=eq.{playlist_id}", + headers=REQUEST_HEADERS, + ) items_response = requests.delete( f"https://api.screenlyapp.com/v4/playlist-items?playlist_id=eq.{playlist_id}", headers=REQUEST_HEADERS, @@ -162,7 +176,7 @@ def assign_playlist_to_all_screens(playlist_id): } response = requests.post( "https://api.screenlyapp.com/v4/labels/playlists", - headers={**REQUEST_HEADERS, "Prefer": "return=representation"}, + headers={**REQUEST_HEADERS, "Prefer": "return=representation, resolution=ignore-duplicates"}, json=payload, ) response.raise_for_status() @@ -192,10 +206,15 @@ def create_qc_playlist(): data = response.json() playlist_id = data[0]["id"] if isinstance(data, list) else data["id"] + print(f"Playlist created: {playlist_id}") + + team_id = get_team_id() - for asset_id in get_ten_random_assets(): + print("Adding assets to playlist...") + for asset_id in get_ten_random_assets(team_id): add_asset_to_playlist(playlist_id, asset_id) + print("Assigning playlist to all screens...") assign_playlist_to_all_screens(playlist_id) @@ -230,10 +249,10 @@ def main(): try: create_qc_playlist() except requests.HTTPError as error: - print(f"Unable to create playlist: {error.response.status_code} {error.response.text}") + print(f"QC playlist setup failed: {error.response.status_code} {error.response.text}") sys.exit(1) except Exception as error: - print(f"Unable to create playlist: {error}") + print(f"QC playlist setup failed: {error}") sys.exit(1) print("Waiting for screens to sync...") From 655f6c8fec05c4bf4a6fac02d082e8ae3ac32c4b Mon Sep 17 00:00:00 2001 From: Salman Faris Date: Wed, 8 Jul 2026 14:25:02 +0530 Subject: [PATCH 2/2] Use /v4.1/teams?is_current=eq.true to get team_id --- automated_quality_control.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/automated_quality_control.py b/automated_quality_control.py index 001c7f4..89453bd 100644 --- a/automated_quality_control.py +++ b/automated_quality_control.py @@ -18,17 +18,17 @@ def get_team_id() -> str: """ - Return the current account's team ID via the built-in all-screens label. + Return the current account's team ID. """ response = requests.get( - "https://api.screenlyapp.com/v4/labels?type=eq.all-screens", + "https://api.screenlyapp.com/v4.1/teams?is_current=eq.true", headers=REQUEST_HEADERS, ) response.raise_for_status() - labels = response.json() - if not labels: - raise ValueError("No 'all-screens' label found in the account") - return labels[0]["team_id"] + teams = response.json() + if not teams: + raise ValueError("No current team found for this token") + return teams[0]["id"] def get_ten_random_assets(team_id: str) -> List[str]: