Construct paths using path libs

Fixes issues with paths on windows.

issue #35
This commit is contained in:
Ivan Habunek 2020-09-03 12:24:36 +02:00
parent a245ffb6a4
commit 041689bee9
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
2 changed files with 11 additions and 12 deletions

View File

@ -1,12 +1,11 @@
import m3u8 import m3u8
import os
import pathlib
import re import re
import requests import requests
import shutil import shutil
import subprocess import subprocess
import tempfile import tempfile
from os import path
from pathlib import Path from pathlib import Path
from urllib.parse import urlparse from urllib.parse import urlparse
@ -155,10 +154,10 @@ def _get_vod_paths(playlist, start, end):
def _crete_temp_dir(base_uri): def _crete_temp_dir(base_uri):
"""Create a temp dir to store downloads if it doesn't exist.""" """Create a temp dir to store downloads if it doesn't exist."""
path = urlparse(base_uri).path path = urlparse(base_uri).path.lstrip("/")
directory = '{}/twitch-dl{}'.format(tempfile.gettempdir(), path) temp_dir = Path(tempfile.gettempdir(), "twitch-dl", path)
pathlib.Path(directory).mkdir(parents=True, exist_ok=True) temp_dir.mkdir(parents=True, exist_ok=True)
return directory return temp_dir
VIDEO_PATTERNS = [ VIDEO_PATTERNS = [
@ -274,9 +273,9 @@ def _download_video(video_id, args):
vod_paths = _get_vod_paths(playlist, args.start, args.end) vod_paths = _get_vod_paths(playlist, args.start, args.end)
# Save playlists for debugging purposes # 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) 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) f.write(response.text)
print_out("\nDownloading {} VODs using {} workers to {}".format( print_out("\nDownloading {} VODs using {} workers to {}".format(
@ -292,7 +291,7 @@ def _download_video(video_id, args):
segment.uri = path_map[segment.uri] segment.uri = path_map[segment.uri]
playlist.segments.append(segment) playlist.segments.append(segment)
playlist_path = target_dir + "playlist_downloaded.m3u8" playlist_path = path.join(target_dir, "playlist_downloaded.m3u8")
playlist.dump(playlist_path) playlist.dump(playlist_path)
print_out("\n\nJoining files...") print_out("\n\nJoining files...")
@ -300,9 +299,9 @@ def _download_video(video_id, args):
_join_vods(playlist_path, target) _join_vods(playlist_path, target)
if args.keep: if args.keep:
print_out("\nTemporary files not deleted: {}".format(target_dir)) print_out("\n<dim>Temporary files not deleted: {}</dim>".format(target_dir))
else: else:
print_out("\nDeleting temporary files...") print_out("\n<dim>Deleting temporary files...</dim>")
shutil.rmtree(target_dir) shutil.rmtree(target_dir)
print_out("\nDownloaded: <green>{}</green>".format(target)) print_out("\nDownloaded: <green>{}</green>".format(target))

View File

@ -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. `vod_paths`, returning a dict which maps the paths to the downloaded files.
""" """
urls = [base_url + path for path in vod_paths] 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)) partials = (partial(download_file, url, path) for url, path in zip(urls, targets))
with ThreadPoolExecutor(max_workers=max_workers) as executor: with ThreadPoolExecutor(max_workers=max_workers) as executor: