mirror of
https://github.com/blw1138/Zordon.git
synced 2025-12-17 16:58:12 +00:00
108 lines
3.7 KiB
Python
108 lines
3.7 KiB
Python
import json
|
|
import requests
|
|
from requests.auth import HTTPBasicAuth
|
|
from datetime import datetime, timezone
|
|
import time
|
|
import logging
|
|
|
|
|
|
class OpenProject:
|
|
|
|
def __init__(self):
|
|
# self.server_url = "http://localhost:8080"
|
|
self.server_url = "http://17.114.221.240:8080"
|
|
# self.api_key = "bb5897eb1daf9bdc4b400675de8e1e52bd64e1e8bce95b341a61a036431c850e"
|
|
self.api_key = "b902d975fcf6a29558e611e665145282acffa1e7109bfb462ef25266f7f9ed6e"
|
|
|
|
def create_shot(self, scene, shot, project, sequence=None):
|
|
url = self.server_url + "/api/v3/work_packages"
|
|
|
|
project_url = 1
|
|
attributes = {
|
|
"subject": "SC{}_{}".format(scene, shot),
|
|
"customField2": scene,
|
|
"customField1": shot,
|
|
"_links": {
|
|
"project": {"href": "/api/v3/projects/{}".format(project_url)},
|
|
"type": {"href": "/api/v3/types/1"}
|
|
}
|
|
}
|
|
|
|
return self._send_command(url, attributes)
|
|
|
|
def add_comment(self, work_project_id, comment, notify=False):
|
|
|
|
url = self.server_url + "/api/v3/work_packages/{}/activities?notify={}".format(str(work_project_id), str(notify))
|
|
attributes = {"comment": {"raw": comment}}
|
|
|
|
return self._send_command(url, attributes)
|
|
|
|
def get_work_package(self, identifier=None, attribute=None):
|
|
url = self.server_url + "/api/v3/work_packages/"
|
|
if identifier:
|
|
url = url + str(identifier)
|
|
return self._send_command(url, attribute)
|
|
|
|
def get_projects(self, identifier=None):
|
|
url = self.server_url + "/api/v3/projects/"
|
|
if identifier:
|
|
url = url + str(identifier)
|
|
return self._send_command(url, None)
|
|
|
|
def _send_command(self, url, body):
|
|
|
|
if body:
|
|
response = requests.post(url, json=body,
|
|
auth=HTTPBasicAuth('apikey', self.api_key))
|
|
else:
|
|
response = requests.get(url, auth=HTTPBasicAuth('apikey', self.api_key))
|
|
|
|
if not response.ok:
|
|
logging.error('Response error: {}'.format(response.reason))
|
|
|
|
return response.json()
|
|
|
|
|
|
class OpenProjectWatcher:
|
|
|
|
def __init__(self, op_instance, interval=30):
|
|
self.op = OpenProject()
|
|
self.interval = interval
|
|
self.last_check = None
|
|
|
|
def _check_projects(self):
|
|
projects = self.op.get_projects()
|
|
for project in projects['_embedded']['elements']:
|
|
# last_update = datetime.datetime.fromisoformat(project['updatedAt'])
|
|
last_update = datetime.strptime(project['updatedAt'], "%Y-%m-%dT%H:%M:%S%z")
|
|
if not self.last_check or last_update > self.last_check:
|
|
logging.info("Update found for project: {}".format(project['name']))
|
|
# todo: do something with updated info
|
|
|
|
def _check_work_projects(self):
|
|
packages = self.op.get_work_package()
|
|
for pkg in packages['_embedded']['elements']:
|
|
# print(pkg.keys())
|
|
last_update = datetime.strptime(pkg['updatedAt'], "%Y-%m-%dT%H:%M:%S%z")
|
|
if not self.last_check or last_update > self.last_check:
|
|
logging.info("Update found for shot: {}".format(pkg['subject']))
|
|
# todo: do something with updated info
|
|
|
|
def watch(self):
|
|
while True:
|
|
now = datetime.now(timezone.utc)
|
|
self._check_projects()
|
|
self._check_work_projects()
|
|
self.last_check = now
|
|
time.sleep(self.interval)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
op = OpenProject()
|
|
op.add_comment(42, "After Effects Render completed successfully. Log available here.", True)
|
|
# print(op.get_projects())
|
|
watcher = OpenProjectWatcher(OpenProject())
|
|
watcher.watch()
|