2021-06-03 15:20:39 +02:00
|
|
|
import hashlib
|
|
|
|
import os
|
|
|
|
import stat
|
2015-06-22 21:49:23 +02:00
|
|
|
import time
|
2022-08-21 11:28:57 +02:00
|
|
|
from queue import Empty, Queue
|
2021-06-03 15:20:39 +02:00
|
|
|
from threading import Thread
|
|
|
|
|
2022-07-22 13:27:16 +02:00
|
|
|
from libretime_api_client.v2 import ApiClient
|
2022-01-13 16:11:37 +01:00
|
|
|
from loguru import logger
|
2022-07-22 15:41:38 +02:00
|
|
|
from requests.exceptions import ConnectionError, HTTPError, Timeout
|
2014-07-10 23:56:41 +02:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
|
|
|
|
class PypoFile(Thread):
|
2022-08-14 19:55:39 +02:00
|
|
|
name = "file"
|
|
|
|
|
2022-08-21 11:28:57 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-09-01 16:18:21 +02:00
|
|
|
file_queue: Queue,
|
2022-08-21 11:28:57 +02:00
|
|
|
api_client: ApiClient,
|
|
|
|
):
|
2012-03-01 23:58:44 +01:00
|
|
|
Thread.__init__(self)
|
2022-08-21 11:28:57 +02:00
|
|
|
self.media_queue = file_queue
|
2012-03-01 23:58:44 +01:00
|
|
|
self.media = None
|
2022-07-22 16:26:43 +02:00
|
|
|
self.api_client = api_client
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
def copy_file(self, media_item):
|
|
|
|
"""
|
|
|
|
Copy media_item from local library directory to local cache directory.
|
2012-06-26 23:00:14 +02:00
|
|
|
"""
|
2022-07-22 15:41:38 +02:00
|
|
|
file_id = media_item["id"]
|
2021-05-27 16:23:02 +02:00
|
|
|
dst = media_item["dst"]
|
2014-12-03 19:22:52 +01:00
|
|
|
|
2012-04-13 21:23:01 +02:00
|
|
|
dst_exists = True
|
|
|
|
try:
|
|
|
|
dst_size = os.path.getsize(dst)
|
2020-01-30 14:47:36 +01:00
|
|
|
if dst_size == 0:
|
|
|
|
dst_exists = False
|
2022-08-09 21:05:21 +02:00
|
|
|
except Exception:
|
2012-04-13 21:23:01 +02:00
|
|
|
dst_exists = False
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2012-04-13 21:23:01 +02:00
|
|
|
do_copy = False
|
|
|
|
if dst_exists:
|
2019-07-29 20:06:43 +02:00
|
|
|
# TODO: Check if the locally cached variant of the file is sane.
|
|
|
|
# This used to be a filesize check that didn't end up working.
|
|
|
|
# Once we have watched folders updated files from them might
|
|
|
|
# become an issue here... This needs proper cache management.
|
2022-03-29 13:07:38 +02:00
|
|
|
# https://github.com/libretime/libretime/issues/756#issuecomment-477853018
|
|
|
|
# https://github.com/libretime/libretime/pull/845
|
2022-07-22 15:41:38 +02:00
|
|
|
logger.debug(f"found file {file_id} in cache {dst}, skipping copy...")
|
2012-04-13 21:23:01 +02:00
|
|
|
else:
|
|
|
|
do_copy = True
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
media_item["file_ready"] = not do_copy
|
2012-07-03 23:06:35 +02:00
|
|
|
|
2012-04-13 21:23:01 +02:00
|
|
|
if do_copy:
|
2022-07-22 15:41:38 +02:00
|
|
|
logger.info(f"copying file {file_id} to cache {dst}")
|
2017-10-15 14:45:22 +02:00
|
|
|
try:
|
2014-12-03 19:22:52 +01:00
|
|
|
with open(dst, "wb") as handle:
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info(media_item)
|
2022-07-22 15:41:38 +02:00
|
|
|
try:
|
2022-08-12 15:14:19 +02:00
|
|
|
response = self.api_client.download_file(file_id, stream=True)
|
|
|
|
for chunk in response.iter_content(chunk_size=2048):
|
2022-07-22 15:41:38 +02:00
|
|
|
handle.write(chunk)
|
2021-01-19 15:23:50 +01:00
|
|
|
|
2022-07-22 15:41:38 +02:00
|
|
|
except HTTPError as exception:
|
2021-05-27 16:23:02 +02:00
|
|
|
raise Exception(
|
2022-07-22 15:41:38 +02:00
|
|
|
f"could not download file {media_item['id']}"
|
|
|
|
) from exception
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
# make file world readable and owner writable
|
2017-09-28 11:12:57 +02:00
|
|
|
os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
|
2013-05-29 00:06:23 +02:00
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
if media_item["filesize"] == 0:
|
2021-09-01 15:25:38 +02:00
|
|
|
file_size = self.report_file_size_and_md5_to_api(
|
2021-08-20 13:35:33 +02:00
|
|
|
dst, media_item["id"]
|
2021-05-27 16:23:02 +02:00
|
|
|
)
|
2015-02-24 17:00:41 +01:00
|
|
|
media_item["filesize"] = file_size
|
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
media_item["file_ready"] = True
|
2022-08-09 21:05:21 +02:00
|
|
|
except Exception as exception:
|
|
|
|
logger.exception(f"could not copy file {file_id} to {dst}: {exception}")
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2021-09-01 15:25:38 +02:00
|
|
|
def report_file_size_and_md5_to_api(self, file_path, file_id):
|
2015-02-24 17:00:41 +01:00
|
|
|
try:
|
|
|
|
file_size = os.path.getsize(file_path)
|
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
with open(file_path, "rb") as fh:
|
2015-02-24 17:00:41 +01:00
|
|
|
m = hashlib.md5()
|
|
|
|
while True:
|
|
|
|
data = fh.read(8192)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
m.update(data)
|
|
|
|
md5_hash = m.hexdigest()
|
2022-08-09 21:05:21 +02:00
|
|
|
except OSError as exception:
|
2015-02-24 17:00:41 +01:00
|
|
|
file_size = 0
|
2022-08-09 21:05:21 +02:00
|
|
|
logger.exception(
|
|
|
|
f"Error getting file size and md5 hash for file id {file_id}: {exception}"
|
2021-05-27 16:23:02 +02:00
|
|
|
)
|
2015-02-24 17:00:41 +01:00
|
|
|
|
2021-08-20 13:35:33 +02:00
|
|
|
# Make PUT request to LibreTime to update the file size and hash
|
2022-08-09 21:05:21 +02:00
|
|
|
error_msg = f"Could not update media file {file_id} with file size and md5 hash"
|
2017-10-03 03:37:06 +02:00
|
|
|
try:
|
2022-07-22 15:41:38 +02:00
|
|
|
self.api_client.update_file(
|
|
|
|
file_id,
|
|
|
|
json={"filesize": file_size, "md5": md5_hash},
|
|
|
|
)
|
2015-02-24 17:00:41 +01:00
|
|
|
except (ConnectionError, Timeout):
|
2022-08-09 21:05:21 +02:00
|
|
|
logger.exception(error_msg)
|
|
|
|
except Exception as exception:
|
|
|
|
logger.exception(f"{error_msg}: {exception}")
|
2015-02-24 17:00:41 +01:00
|
|
|
|
|
|
|
return file_size
|
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
def get_highest_priority_media_item(self, schedule):
|
|
|
|
"""
|
|
|
|
Get highest priority media_item in the queue. Currently the highest
|
|
|
|
priority is decided by how close the start time is to "now".
|
|
|
|
"""
|
2012-04-16 17:45:48 +02:00
|
|
|
if schedule is None or len(schedule) == 0:
|
2012-03-01 23:58:44 +01:00
|
|
|
return None
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
sorted_keys = sorted(schedule.keys())
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
if len(sorted_keys) == 0:
|
|
|
|
return None
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
highest_priority = sorted_keys[0]
|
|
|
|
media_item = schedule[highest_priority]
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.debug("Highest priority item: %s" % highest_priority)
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2022-07-01 12:23:18 +02:00
|
|
|
# Remove this media_item from the dictionary. On the next iteration
|
|
|
|
# (from the main function) we won't consider it for prioritization
|
|
|
|
# anymore. If on the next iteration we have received a new schedule,
|
|
|
|
# it is very possible we will have to deal with the same media_items
|
|
|
|
# again. In this situation, the worst possible case is that we try to
|
|
|
|
# copy the file again and realize we already have it (thus aborting the copy).
|
2012-06-26 23:00:14 +02:00
|
|
|
del schedule[highest_priority]
|
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
return media_item
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
def main(self):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
if self.media is None or len(self.media) == 0:
|
2022-07-01 12:23:18 +02:00
|
|
|
# We have no schedule, so we have nothing else to do. Let's
|
|
|
|
# do a blocked wait on the queue
|
2012-03-01 23:58:44 +01:00
|
|
|
self.media = self.media_queue.get(block=True)
|
|
|
|
else:
|
2022-07-01 12:23:18 +02:00
|
|
|
# We have a schedule we need to process, but we also want
|
|
|
|
# to check if a newer schedule is available. In this case
|
|
|
|
# do a non-blocking queue.get and in either case (we get something
|
|
|
|
# or we don't), get back to work on preparing getting files.
|
2012-03-01 23:58:44 +01:00
|
|
|
try:
|
|
|
|
self.media = self.media_queue.get_nowait()
|
2022-08-09 21:05:21 +02:00
|
|
|
except Empty:
|
2012-03-01 23:58:44 +01:00
|
|
|
pass
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
media_item = self.get_highest_priority_media_item(self.media)
|
2012-04-16 17:45:48 +02:00
|
|
|
if media_item is not None:
|
2014-12-03 00:46:17 +01:00
|
|
|
self.copy_file(media_item)
|
2022-08-09 21:05:21 +02:00
|
|
|
except Exception as exception:
|
|
|
|
logger.exception(exception)
|
|
|
|
raise exception
|
2012-06-26 23:00:14 +02:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
def run(self):
|
|
|
|
"""
|
|
|
|
Entry point of the thread
|
|
|
|
"""
|
2021-05-27 16:23:02 +02:00
|
|
|
try:
|
|
|
|
self.main()
|
2022-08-09 21:05:21 +02:00
|
|
|
except Exception as exception:
|
|
|
|
logger.exception(exception)
|
2015-06-22 21:49:23 +02:00
|
|
|
time.sleep(5)
|
2022-08-09 21:05:21 +02:00
|
|
|
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("PypoFile thread exiting")
|