Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions automated_quality_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
PLAYLIST_PREFIX = "QC"


@retry(requests.HTTPError, tries=3, delay=5)
def get_ten_random_assets():
"""
Return 10 random assets in the account.
Expand Down Expand Up @@ -84,6 +85,7 @@ def wait_for_screens_to_sync():
raise AssertionError("Not all online screens synchronized")


@retry(requests.HTTPError, tries=3, delay=5)
def get_qc_playlist_ids():
"""
Get all playlists starting with 'PLAYLIST_PREFIX'.
Expand All @@ -100,6 +102,7 @@ def get_qc_playlist_ids():
return qc_playlists


@retry(requests.HTTPError, tries=3, delay=5)
def delete_playlist(playlist_id):
"""
Delete a playlist and its items. In v4, playlist items must be
Expand All @@ -109,15 +112,16 @@ def delete_playlist(playlist_id):
f"https://api.screenlyapp.com/v4/playlist-items?playlist_id=eq.{playlist_id}",
headers=REQUEST_HEADERS,
)
if not items_response.ok:
return False
items_response.raise_for_status()
response = requests.delete(
f"https://api.screenlyapp.com/v4/playlists?id=eq.{playlist_id}",
headers=REQUEST_HEADERS,
)
return response.ok
response.raise_for_status()
return True


@retry(requests.HTTPError, tries=3, delay=5)
def get_all_screens_label_id():
"""
Return the ID of the built-in 'all-screens' label.
Expand Down Expand Up @@ -168,10 +172,12 @@ def assign_playlist_to_all_screens(playlist_id):
response.raise_for_status()


@retry(requests.HTTPError, tries=3, delay=5)
def create_qc_playlist():
"""
Create a new QC playlist, populate it with random assets,
and assign it to all screens.
and assign it to all screens. Retries up to 3 times on transient
API errors (e.g. intermittent 401 from the PostgREST auth layer).
Comment on lines +175 to +180
"""

current_date = datetime.now(timezone.utc)
Expand Down Expand Up @@ -223,8 +229,10 @@ def main():
if len(qc_playlists) > 0:
print("Found a QC playlist. Deleting it...")
for playlist in qc_playlists:
if not delete_playlist(playlist):
print(f"Warning: failed to delete playlist {playlist}")
try:
delete_playlist(playlist)
except requests.HTTPError as error:
print(f"Warning: failed to delete playlist {playlist}: {error.response.status_code} {error.response.text}")

print("Creating new QC playlist...")
try:
Expand Down
Loading