Jiffy/Jiffy.py

107 lines
3.8 KiB
Python

import json
import logging
import time
import requests
logging.basicConfig(level=logging.DEBUG)
UPLOAD_IMAGE_JSON = json.dumps({
"mimetype": "image",
"is_enabled": 0,
"name": '%s',
"end_date": "9999-04-16T21:44:31.552Z",
"duration": '%d',
"play_order": 0,
"nocache": 0,
"uri": '%s',
"skip_asset_check": 0,
"start_date": "2019-04-16T21:44:31.552Z"
})
class Jiffy:
host = api_url_v1_0 = api_url_v1_2 = credentials = None
def __init__(self, host, api_url_v1_0, api_url_v1_2, credentials):
self.host = host
self.api_url_v1_0 = api_url_v1_0
self.api_url_v1_2 = api_url_v1_2
self.credentials = credentials
def _check_media_exists(self, media_url):
try:
logging.debug(f'Attempting to get file from {media_url}.')
resp = requests.get(media_url)
except Exception as e:
logging.critical(f'An exception occurred: {e}.')
return False
else:
media_check_status = resp.status_code if hasattr(resp, 'status_code') else None
logging.debug(f'Received status code {media_check_status}')
return media_check_status == 200
def _load_file_from_url(self, media_url, duration=10):
asset_upload_url = f'http://{self.host}/{self.api_url_v1_2}/assets'
data_to_upload = UPLOAD_IMAGE_JSON % (media_url, duration, media_url)
logging.debug(f'Attempting to upload {media_url} to {asset_upload_url}.')
file_upload_response = requests.post(url=asset_upload_url, data=data_to_upload, auth=self.credentials)
file_upload_response_json = file_upload_response.json()
if not isinstance(file_upload_response_json, dict):
logging.critical(f'Could not parse asset ID, received type {type(file_upload_response_json)}.')
return
else:
asset_id = file_upload_response_json.get('asset_id')
logging.debug(f'Received asset ID {asset_id}.')
return asset_id
def _display_asset(self, asset_id):
logging.debug(f'Attempting to display asset with ID {asset_id}.')
display_asset_response = requests.get(
f'http://{self.host}/{self.api_url_v1_0}/assets/control/asset&{asset_id}',
auth=self.credentials
)
display_asset_status_code = display_asset_response.status_code if getattr(display_asset_response, 'status_code') else None
logging.debug(
f'Received status code of {display_asset_status_code} when trying to display asset with ID {asset_id}.'
)
return display_asset_status_code == 200
def _delete_asset(self, asset_id):
logging.debug(f'Attempting to delete asset with ID {asset_id}.')
delete_asset_response = requests.delete(f'http://{self.host}/{self.api_url_v1_2}/assets/{asset_id}', auth=self.credentials)
delete_asset_status_code = delete_asset_response.status_code if getattr(delete_asset_response, 'status_code') else None
logging.debug(
f'Received status code of {delete_asset_status_code}, when trying to delete asset with ID "asset_id".'
)
return delete_asset_status_code
def display_media_from_url(self, media_url, duration=10):
if not self._check_media_exists(media_url):
logging.critical(f'Media at {media_url} cannot be found. Terminating.')
return
else:
asset_id = self._load_file_from_url(media_url, duration)
if asset_id is None:
logging.critical('No asset ID received. Terminating')
return
else:
self._display_asset(asset_id)
logging.debug(f'Sleeping for {duration} to allow media to play.')
time.sleep(duration)
self._delete_asset(asset_id)