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
65 changes: 44 additions & 21 deletions automated_quality_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ def get_ten_random_assets():
"""

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.1/assets?select=id&type=in.("appweb","audio","edge-app","image","video","web")&status=in.("finished","processing")',
headers=REQUEST_HEADERS,
Comment thread
nicomiguelino marked this conversation as resolved.
)
response.raise_for_status()

asset_count = len(response.json())
assets = response.json()
asset_count = len(assets)

# 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"])
asset_list.append(assets[random_index]["id"])

return asset_list

Expand All @@ -43,7 +44,7 @@ def get_screens() -> List[Dict[str, Any]]:
Return a list of screens in the account.
"""

response = requests.get('https://api.screenlyapp.com/v4/screens?select=id,name,hostname,status,in_sync&type=eq.hardware&is_enabled=eq.true', headers=REQUEST_HEADERS)
response = requests.get('https://api.screenlyapp.com/v4.1/screens?select=id,name,hostname,screen_statuses(status,in_sync)&type=eq.hardware&is_enabled=eq.true', headers=REQUEST_HEADERS)
response.raise_for_status()
return response.json()

Expand All @@ -64,10 +65,19 @@ def wait_for_screens_to_sync():
print(f"Unable to fetch screens: {error}")
sys.exit(1)

screens_not_in_sync = [screen for screen in screens if not screen['in_sync']]
screens_not_in_sync = [
screen for screen in screens
if not screen.get('screen_statuses', {}).get('in_sync', False)
]

offline_screens = [s for s in screens_not_in_sync if s['status'].lower() == 'offline']
out_of_sync_screens = [s for s in screens_not_in_sync if s['status'].lower() != 'offline']
offline_screens = []
out_of_sync_screens = []
for screen in screens_not_in_sync:
screen_statuses = screen.get('screen_statuses', {})
if screen_statuses.get('status', '').lower() == 'offline':
offline_screens.append(screen)
else:
out_of_sync_screens.append(screen)

if offline_screens:
print(f"Skipping {len(offline_screens)} offline screen(s) (cannot sync while unreachable):")
Expand All @@ -79,7 +89,9 @@ def wait_for_screens_to_sync():

print(f"...waiting for {len(out_of_sync_screens)} screen(s) to sync:")
for screen in out_of_sync_screens:
print(f" OUT OF SYNC: {screen['name']}({screen['hostname']}) — {screen['status'].lower()}")
screen_statuses = screen.get('screen_statuses', {})
status = screen_statuses.get('status', 'unknown').lower()
print(f" OUT OF SYNC: {screen['name']}({screen['hostname']}) — {status}")

raise AssertionError("Not all online screens synchronized")

Expand All @@ -89,7 +101,7 @@ def get_qc_playlist_ids():
Get all playlists starting with 'PLAYLIST_PREFIX'.
"""

response = requests.get("https://api.screenlyapp.com/v4/playlists", headers=REQUEST_HEADERS)
response = requests.get("https://api.screenlyapp.com/v4.1/playlists", headers=REQUEST_HEADERS)
response.raise_for_status()

qc_playlists = []
Expand All @@ -102,17 +114,17 @@ def get_qc_playlist_ids():

def delete_playlist(playlist_id):
"""
Delete a playlist and its items. In v4, playlist items must be
Delete a playlist and its items. Playlist items must be
removed before the playlist itself can be deleted.
"""
items_response = requests.delete(
f"https://api.screenlyapp.com/v4/playlist-items?playlist_id=eq.{playlist_id}",
f"https://api.screenlyapp.com/v4.1/playlist-items?playlist_id=eq.{playlist_id}",
headers=REQUEST_HEADERS,
)
if not items_response.ok:
return False
response = requests.delete(
f"https://api.screenlyapp.com/v4/playlists?id=eq.{playlist_id}",
f"https://api.screenlyapp.com/v4.1/playlists?id=eq.{playlist_id}",
headers=REQUEST_HEADERS,
)
return response.ok
Expand All @@ -123,7 +135,7 @@ def get_all_screens_label_id():
Return the ID of the built-in 'all-screens' label.
"""
response = requests.get(
"https://api.screenlyapp.com/v4/labels?type=eq.all-screens",
"https://api.screenlyapp.com/v4.1/labels?type=eq.all-screens",
headers=REQUEST_HEADERS,
)
response.raise_for_status()
Expand All @@ -135,15 +147,15 @@ def get_all_screens_label_id():

def add_asset_to_playlist(playlist_id, asset_id):
"""
Add a single asset to a playlist via the v4 playlist-items endpoint.
Add a single asset to a playlist via the playlist-items endpoint.
"""
payload = {
"playlist_id": playlist_id,
"asset_id": asset_id,
"duration": 10,
}
response = requests.post(
"https://api.screenlyapp.com/v4/playlist-items",
"https://api.screenlyapp.com/v4.1/playlist-items",
Comment thread
nicomiguelino marked this conversation as resolved.
headers={**REQUEST_HEADERS, "Prefer": "return=representation"},
json=payload,
)
Expand All @@ -161,7 +173,7 @@ def assign_playlist_to_all_screens(playlist_id):
"playlist_id": playlist_id,
}
response = requests.post(
"https://api.screenlyapp.com/v4/labels/playlists",
"https://api.screenlyapp.com/v4.1/labels/playlists",
headers={**REQUEST_HEADERS, "Prefer": "return=representation"},
json=payload,
)
Expand All @@ -184,7 +196,7 @@ def create_qc_playlist():
}

response = requests.post(
"https://api.screenlyapp.com/v4/playlists",
"https://api.screenlyapp.com/v4.1/playlists",
headers={**REQUEST_HEADERS, "Prefer": "return=representation"},
json=payload,
)
Expand Down Expand Up @@ -243,14 +255,25 @@ def main():
print(f"Warning: {error}. Fetching final screen status...")
try:
final_screens = get_screens()
not_synced = [s for s in final_screens if not s['in_sync']]
offline = [s for s in not_synced if s['status'].lower() == 'offline']
out_of_sync = [s for s in not_synced if s['status'].lower() != 'offline']
not_synced = [
screen for screen in final_screens
if not screen.get('screen_statuses', {}).get('in_sync', False)
]
offline = []
out_of_sync = []
for s in not_synced:
screen_statuses = s.get('screen_statuses', {})
if screen_statuses.get('status', '').lower() == 'offline':
offline.append(s)
else:
out_of_sync.append(s)
print(f"Final status: {len(offline)} offline, {len(out_of_sync)} out of sync after timeout:")
for s in offline:
print(f" OFFLINE: {s['name']}({s['hostname']})")
for s in out_of_sync:
print(f" OUT OF SYNC: {s['name']}({s['hostname']}) — {s['status'].lower()}")
screen_statuses = s.get('screen_statuses', {})
status = screen_statuses.get('status', 'unknown').lower()
print(f" OUT OF SYNC: {s['name']}({s['hostname']}) — {status}")
except Exception as fetch_error:
print(f"Could not fetch final screen status: {fetch_error}")

Expand Down
Loading