Skip to content

Commit

Permalink
move some logic from _download_file() and _copy_file() to download()
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunsi committed Jan 25, 2024
1 parent ea685e8 commit 1360fc8
Showing 1 changed file with 42 additions and 62 deletions.
104 changes: 42 additions & 62 deletions voctopublish/voctopublish.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,45 @@ def download(self):
download or copy a file for processing
:return:
"""
# we name our input video file uncut ts so tracker will find it. This is not the nicest way to go
# TODO find a better integration in to the pipeline
path = os.path.join(
self.ticket.fuse_path, self.ticket.room, self.ticket.fahrplan_id
)
file = os.path.join(path, "uncut.ts")
logging.info(
"Downloading input file from: " + self.ticket.download_url + " to " + file
)

if not os.path.exists(path):
try:
os.makedirs(path)
except Exception as e:
logging.error(e)
logging.exception(e)
raise PublisherException(e)

if os.path.exists(file) and not self.ticket.redownload_enabled:
logging.error(
'video file "' + path + '" already exists, please remove file'
)
raise PublisherException("video file already exists, please remove file")

url = self.ticket.download_url
url_decoded = urllib.parse.unquote(url)
if len(url) > len(url_decoded):
logging.warning(
f"Download URL {url} was urlencoded, using {url_decoded} instead"
)
url = url_decoded

# if its an URL it probably will start with http ....
if self.ticket.download_url.startswith(
"http"
) or self.ticket.download_url.startswith("ftp"):
self._download_file()
self._download_file(url, file)
else:
self._copy_file()
self._copy_file(url, file)

# set recording language TODO multilang
try:
Expand All @@ -548,79 +580,27 @@ def download(self):
# tell the tracker that we finished the import
self.c3tt.set_ticket_done(self.ticket_id)

def _copy_file(self):
def _copy_file(self, source, target):
"""
copy a file from a local folder to the fake fuse and name it uncut.ts
this hack to import files not produced with the tracker into the workflow to publish it on the voctoweb / youtube
:return:
"""
path = os.path.join(
self.ticket.fuse_path, self.ticket.room, self.ticket.fahrplan_id
)
file = os.path.join(path, "uncut.ts")
logging.info(
"Copying input file from: " + self.ticket.download_url + " to " + file
)
if not os.path.exists(path):
try:
os.makedirs(path)
except Exception as e:
logging.error(e)
logging.exception(e)
raise PublisherException(e)

if os.path.exists(file):
# TODO think about rereleasing here
logging.warning("video file already exists, please remove file")
raise PublisherException("video file already exists, please remove file")

try:
shutil.copyfile(self.ticket.download_url, file)
shutil.copyfile(source, target)
except IOError as e_:
raise PublisherException(e_)

def _download_file(self):
def _download_file(self, source, target):
"""
download a file from an http / https / ftp URL an place it as a uncut.ts in the fuse folder.
this hack to import files not produced with the tracker into the workflow to publish it on the voctoweb / youtube
:return:
"""
# we name our input video file uncut ts so tracker will find it. This is not the nicest way to go
# TODO find a better integration in to the pipeline
path = os.path.join(
self.ticket.fuse_path, self.ticket.room, self.ticket.fahrplan_id
)
file = os.path.join(path, "uncut.ts")
logging.info(
"Downloading input file from: " + self.ticket.download_url + " to " + file
)

if not os.path.exists(path):
try:
os.makedirs(path)
except Exception as e:
logging.error(e)
logging.exception(e)
raise PublisherException(e)

if os.path.exists(file) and not self.ticket.redownload_enabled:
logging.error(
'video file "' + path + '" already exists, please remove file'
)
raise PublisherException("video file already exists, please remove file")

url = self.ticket.download_url
url_decoded = urllib.parse.unquote(url)
if len(url) > len(url_decoded):
logging.warning(
f"Download URL {url} was urlencoded, using {url_decoded} instead"
)
url = url_decoded

logging.info("Downloading file from: " + url)
logging.info("Downloading file from: " + source)
if not self.ticket.download_command:
with open(file, "wb") as fh:
with urllib.request.urlopen(urllib.parse.quote(url, safe=":/")) as df:
with open(target, "wb") as fh:
with urllib.request.urlopen(urllib.parse.quote(source, safe=":/")) as df:
# original version tried to write whole file to ram and ran out of memory
# read in 16 kB chunks instead
while True:
Expand All @@ -632,9 +612,9 @@ def _download_file(self):
command = []
for part in self.ticket.download_command:
if part == "--TARGETPATH--":
command.append(file)
command.append(source)
elif part == "--DOWNLOADURL--":
command.append(url)
command.append(target)
else:
command.append(part)
logging.debug(f"download command is: {command}")
Expand Down

0 comments on commit 1360fc8

Please sign in to comment.