Skip to content

Commit

Permalink
Switch to using shutil as .rename doesn't work across partitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Caligatio committed Apr 22, 2020
1 parent 31fad38 commit 98c6661
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 11 additions & 6 deletions backend/src/youtube_archiver/downloader.py
Expand Up @@ -3,9 +3,9 @@
import json
import logging
import os
import shutil
from functools import partial
from pathlib import Path
from shutil import rmtree
from subprocess import run # noqa: S404
from tempfile import mkdtemp
from typing import Any, Dict, List, Optional, Tuple
Expand Down Expand Up @@ -67,14 +67,16 @@ def process_output_dir(

# This was touched during existence checks if subdirectories aren't being made. If the file exists, it's fine.
try:
info_file = info_file.rename(output_dir / f"{sanitized_title}.json")
shutil.move(str(info_file), str(output_dir / f"{sanitized_title}.json"))
info_file = output_dir / f"{sanitized_title}.json"
except FileExistsError:
pass

audio_file: Optional[Path] = None
if extract_audio:
audio_file = list(download_dir.glob("*.mp3"))[0]
audio_file = audio_file.rename(output_dir / f"{sanitized_title}{audio_file.suffix}")
shutil.move(str(audio_file), str(output_dir / f"{sanitized_title}{audio_file.suffix}"))
audio_file = output_dir / f"{sanitized_title}{audio_file.suffix}"

video_file: Optional[Path] = None
# Audio identification performed first otherwise the mp3 would be picked as the fallback option if no mkv present
Expand All @@ -92,7 +94,8 @@ def process_output_dir(
break

if video_file is not None:
video_file = video_file.rename(output_dir / f"{sanitized_title}{video_file.suffix}")
shutil.move(str(video_file), str(output_dir / f"{sanitized_title}{video_file.suffix}"))
video_file = output_dir / f"{sanitized_title}{video_file.suffix}"

return DownloadResult(pretty_name, sanitized_title, info_file, video_file, audio_file)

Expand Down Expand Up @@ -205,13 +208,15 @@ def download(
"merge_output_format": "mkv",
"keepvideo": True if download_video else False,
"postprocessors": postprocessors,
"ffmpeg_location": str(ffmpeg_dir),
"logger": ytdl_logger,
"writesubtitles": True,
"writeautomaticsub": True,
"subtitleslangs": ["en"],
}

if ffmpeg_dir:
ytdl_opt["ffmpeg_location"] = str(ffmpeg_dir)

try:
with YoutubeDL(ytdl_opt) as ytdl:
info = ytdl.extract_info(url, download=False)
Expand All @@ -234,6 +239,6 @@ def download(

download_result = process_output_dir(tmp_out, output_dir, download_video, extract_audio)
finally:
rmtree(tmp_out)
shutil.rmtree(tmp_out)

return download_result
4 changes: 1 addition & 3 deletions backend/src/youtube_archiver/server.py
Expand Up @@ -49,9 +49,7 @@ async def update_publisher(app: web.Application) -> None:
update["info_file"] = (
app["download_prefix"] / update["info_file"].relative_to(app["download_dir"])
).as_posix()
update["path"] = (
app["download_prefix"] / update["path"].relative_to(app["download_dir"])
).as_posix()
update["path"] = (app["download_prefix"] / update["path"].relative_to(app["download_dir"])).as_posix()

update["status"] = update["status"].name
# The websocket updates are best effort, not required. Don't wait for it to finish
Expand Down

0 comments on commit 98c6661

Please sign in to comment.