From 041689bee9a3c15434cd06243b1ddc3accda081f Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Thu, 3 Sep 2020 12:24:36 +0200 Subject: [PATCH] Construct paths using path libs Fixes issues with paths on windows. issue #35 --- twitchdl/commands.py | 21 ++++++++++----------- twitchdl/download.py | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/twitchdl/commands.py b/twitchdl/commands.py index 9dec4b2..c6bef20 100644 --- a/twitchdl/commands.py +++ b/twitchdl/commands.py @@ -1,12 +1,11 @@ import m3u8 -import os -import pathlib import re import requests import shutil import subprocess import tempfile +from os import path from pathlib import Path from urllib.parse import urlparse @@ -155,10 +154,10 @@ def _get_vod_paths(playlist, start, end): def _crete_temp_dir(base_uri): """Create a temp dir to store downloads if it doesn't exist.""" - path = urlparse(base_uri).path - directory = '{}/twitch-dl{}'.format(tempfile.gettempdir(), path) - pathlib.Path(directory).mkdir(parents=True, exist_ok=True) - return directory + path = urlparse(base_uri).path.lstrip("/") + temp_dir = Path(tempfile.gettempdir(), "twitch-dl", path) + temp_dir.mkdir(parents=True, exist_ok=True) + return temp_dir VIDEO_PATTERNS = [ @@ -274,9 +273,9 @@ def _download_video(video_id, args): vod_paths = _get_vod_paths(playlist, args.start, args.end) # Save playlists for debugging purposes - with open(target_dir + "playlists.m3u8", "w") as f: + with open(path.join(target_dir, "playlists.m3u8"), "w") as f: f.write(playlists_m3u8) - with open(target_dir + "playlist.m3u8", "w") as f: + with open(path.join(target_dir, "playlist.m3u8"), "w") as f: f.write(response.text) print_out("\nDownloading {} VODs using {} workers to {}".format( @@ -292,7 +291,7 @@ def _download_video(video_id, args): segment.uri = path_map[segment.uri] playlist.segments.append(segment) - playlist_path = target_dir + "playlist_downloaded.m3u8" + playlist_path = path.join(target_dir, "playlist_downloaded.m3u8") playlist.dump(playlist_path) print_out("\n\nJoining files...") @@ -300,9 +299,9 @@ def _download_video(video_id, args): _join_vods(playlist_path, target) if args.keep: - print_out("\nTemporary files not deleted: {}".format(target_dir)) + print_out("\nTemporary files not deleted: {}".format(target_dir)) else: - print_out("\nDeleting temporary files...") + print_out("\nDeleting temporary files...") shutil.rmtree(target_dir) print_out("\nDownloaded: {}".format(target)) diff --git a/twitchdl/download.py b/twitchdl/download.py index 1d54e96..c5c8365 100644 --- a/twitchdl/download.py +++ b/twitchdl/download.py @@ -82,7 +82,7 @@ def download_files(base_url, target_dir, vod_paths, max_workers): `vod_paths`, returning a dict which maps the paths to the downloaded files. """ urls = [base_url + path for path in vod_paths] - targets = ["{}{:05d}.ts".format(target_dir, k) for k, _ in enumerate(vod_paths)] + targets = [os.path.join(target_dir, "{:05d}.ts".format(k)) for k, _ in enumerate(vod_paths)] partials = (partial(download_file, url, path) for url, path in zip(urls, targets)) with ThreadPoolExecutor(max_workers=max_workers) as executor: