2019-04-21 18:49:18 +00:00
|
|
|
import json
|
|
|
|
import logging
|
2019-04-21 19:09:22 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
import requests
|
2019-04-21 18:49:18 +00:00
|
|
|
|
|
|
|
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:
|
2019-04-21 19:34:06 +00:00
|
|
|
logging.debug(f'Attempting to get file from {media_url}.')
|
2019-04-21 18:49:18 +00:00
|
|
|
resp = requests.get(media_url)
|
|
|
|
except Exception as e:
|
2019-04-21 19:34:06 +00:00
|
|
|
logging.critical(f'An exception occurred: {e}.')
|
2019-04-21 18:49:18 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
media_check_status = resp.status_code if hasattr(resp, 'status_code') else None
|
2019-04-21 19:34:06 +00:00
|
|
|
logging.debug(f'Received status code {media_check_status}')
|
2019-04-21 18:49:18 +00:00
|
|
|
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)
|
|
|
|
|
2019-04-21 19:34:06 +00:00
|
|
|
logging.debug(f'Attempting to upload {media_url} to {asset_upload_url}.')
|
2019-04-21 18:49:18 +00:00
|
|
|
|
|
|
|
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):
|
2019-04-21 19:34:06 +00:00
|
|
|
logging.critical(f'Could not parse asset ID, received type {type(file_upload_response_json)}.')
|
2019-04-21 18:49:18 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
asset_id = file_upload_response_json.get('asset_id')
|
2019-04-21 19:34:06 +00:00
|
|
|
logging.debug(f'Received asset ID {asset_id}.')
|
2019-04-21 18:49:18 +00:00
|
|
|
|
|
|
|
return asset_id
|
|
|
|
|
|
|
|
def _display_asset(self, asset_id):
|
2019-04-21 19:34:06 +00:00
|
|
|
logging.debug(f'Attempting to display asset with ID {asset_id}.')
|
2019-04-21 18:49:18 +00:00
|
|
|
|
|
|
|
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(
|
2019-04-21 19:34:06 +00:00
|
|
|
f'Received status code of {display_asset_status_code} when trying to display asset with ID {asset_id}.'
|
2019-04-21 18:49:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return display_asset_status_code == 200
|
|
|
|
|
|
|
|
def _delete_asset(self, asset_id):
|
2019-04-21 19:34:06 +00:00
|
|
|
logging.debug(f'Attempting to delete asset with ID {asset_id}.')
|
2019-04-21 18:49:18 +00:00
|
|
|
|
|
|
|
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(
|
2019-04-21 19:34:06 +00:00
|
|
|
f'Received status code of {delete_asset_status_code}, when trying to delete asset with ID "asset_id".'
|
2019-04-21 18:49:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|