use api_clients method to update file size of items on the server

This commit is contained in:
Kyle Robbertze 2021-08-20 13:35:33 +02:00
parent 0c76ed72fb
commit d8780e8add
3 changed files with 20 additions and 13 deletions

View File

@ -91,7 +91,7 @@ class ApiRequest:
self.logger = logger
self.auth = KeyAuth(api_key)
def __call__(self, _post_data=None, params=None, **kwargs):
def __call__(self, _post_data=None, _put_data=None, params=None, **kwargs):
final_url = self.url.params(**kwargs).url()
self.logger.debug(final_url)
try:
@ -102,6 +102,13 @@ class ApiRequest:
auth=self.auth,
timeout=ApiRequest.API_HTTP_REQUEST_TIMEOUT,
)
elif _put_data is not None:
res = requests.put(
final_url,
data=_put_data,
auth=self.auth,
timeout=ApiRequest.API_HTTP_REQUEST_TIMEOUT,
)
else:
res = requests.get(
final_url,

View File

@ -130,3 +130,8 @@ class AirtimeApiClient:
"independent_event": current["independent_event"],
}
return result
def update_file(self, file_id, payload):
data = self.services.file_url(id=file_id)
data.update(payload)
return self.services.file_url(id=file_id, _put_data=data)

View File

@ -88,8 +88,8 @@ class PypoFile(Thread):
os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
if media_item["filesize"] == 0:
file_size = self.report_file_size_and_md5_to_airtime(
dst, media_item["id"], host, username
file_size = self.report_file_size_and_md5_to_libretime(
dst, media_item["id"]
)
media_item["filesize"] = file_size
@ -98,7 +98,7 @@ class PypoFile(Thread):
self.logger.error("Could not copy from %s to %s" % (src, dst))
self.logger.error(e)
def report_file_size_and_md5_to_airtime(self, file_path, file_id, host, api_key):
def report_file_size_and_md5_to_libretime(self, file_path, file_id):
try:
file_size = os.path.getsize(file_path)
@ -117,18 +117,13 @@ class PypoFile(Thread):
)
self.logger.error(e)
# Make PUT request to Airtime to update the file size and hash
# Make PUT request to LibreTime to update the file size and hash
error_msg = (
"Could not update media file %s with file size and md5 hash" % file_id
"Could not update media file %s with file size and md5 hash:" % file_id
)
try:
put_url = "%s://%s:%s/rest/media/%s" % (host[0], host[1], host[2], file_id)
payload = json.dumps({"filesize": file_size, "md5": md5_hash})
response = requests.put(
put_url, data=payload, auth=requests.auth.HTTPBasicAuth(api_key, "")
)
if not response.ok:
self.logger.error(error_msg)
payload = {"filesize": file_size, "md5": md5_hash}
response = self.api_client.update_file(file_id, payload)
except (ConnectionError, Timeout):
self.logger.error(error_msg)
except Exception as e: