Initial commit.

master
Ben Casling 5 years ago
commit e73ed046e4
  1. 1
      .gitignore
  2. 10
      Dockerfile
  3. 143
      Jiffy.py
  4. 25
      cli_run.py
  5. 2
      requirements.txt

1
.gitignore vendored

@ -0,0 +1 @@
__pycache__/

@ -0,0 +1,10 @@
# Built and maintained by:
# Ben Casling <ben@casling.io>,
# Cameron Sharp <me@cazagen.me>
FROM python:3.7.3-alpine
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt

@ -0,0 +1,143 @@
import time
import json
import requests
import logging
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)
# # If they forgot the filename.
# if not len(sys.argv):
# raise Exception('You forgot the file url. Moron.')
# file_url = sys.argv[1]
# if len(sys.argv) >= 3:
# duration = sys.argv[2]
# else:
# duration = 10
# resp = requests.get(file_url)
# # If they didn't give us a valid url.
# if resp.status_code != 200:
# raise Exception('Yeah no, that thing doesn\'t exist.')
# # If by some miracle the user gave us a filename, and it actually exists somewhere.
# asset_upload_url = f'http://{DASHPI_HOST}/{DASHPI_API_v1_2}/assets'
# data_to_upload = UPLOAD_IMAGE_JSON % (file_url, duration, file_url)
# # Upload the file to dashpi so we can switch to it.
# resp2 = requests.post(url=asset_upload_url, data=data_to_upload, auth=DASHPI_CREDS)
# resp2_json = resp2.json()
# # Get the asset ID back from dashpi, or break.
# if not isinstance(resp2_json, dict):
# raise Exception('NO JSON FOUND')
# else:
# asset_id = resp2_json.get('asset_id')
# # Tell dashpi we want to view the image we just uploaded, obviously.
# resp3 = requests.get(f'http://{DASHPI_HOST}/{DASHPI_API_V1_0}/assets/control/asset&{asset_id}', auth=DASHPI_CREDS)
# # Wait till we are done displaying the gif (plus 5s for safety) then delete it from dashpi.
# time.sleep(duration+1)
# resp4 = requests.delete(f'http://{DASHPI_HOST}/{DASHPI_API_v1_2}/assets/{asset_id}', auth=DASHPI_CREDS)

@ -0,0 +1,25 @@
import sys
from Jiffy import Jiffy
jiffy = Jiffy(
host='dashpi-g1',
api_url_v1_0='api/v1',
api_url_v1_2='api/v1.2',
credentials=(
'admin',
'hacklab'
)
)
if not len(sys.argv):
raise Exception('You forgot the file url. Moron.')
file_url = sys.argv[1]
if len(sys.argv) >= 3:
duration = sys.argv[2]
else:
duration = 10
jiffy.display_media_from_url(file_url, duration)

@ -0,0 +1,2 @@
paho-mqtt==1.4.0
Loading…
Cancel
Save