improved joining of temp dir & filenames, continued refactoring.
This commit is contained in:
		
							
								
								
									
										45
									
								
								coub-dl.py
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								coub-dl.py
									
									
									
									
									
								
							@@ -1,6 +1,6 @@
 | 
				
			|||||||
from subprocess import call, Popen, PIPE, check_output, DEVNULL
 | 
					from subprocess import call, Popen, PIPE, check_output, DEVNULL
 | 
				
			||||||
from os import listdir, remove
 | 
					from os import listdir, remove
 | 
				
			||||||
from os.path import splitext, exists
 | 
					from os.path import splitext, exists, join
 | 
				
			||||||
from re import match
 | 
					from re import match
 | 
				
			||||||
from enum import Enum
 | 
					from enum import Enum
 | 
				
			||||||
from datetime import timedelta
 | 
					from datetime import timedelta
 | 
				
			||||||
@@ -73,7 +73,7 @@ def download_video_stream(url, file_dict):
 | 
				
			|||||||
def read_extensions(file_dict, directory):
 | 
					def read_extensions(file_dict, directory):
 | 
				
			||||||
    for file in listdir(directory):
 | 
					    for file in listdir(directory):
 | 
				
			||||||
        for filename in file_dict:
 | 
					        for filename in file_dict:
 | 
				
			||||||
            fullname = '{}/{}'.format(directory, file)
 | 
					            fullname = join(directory, file)
 | 
				
			||||||
            if match('^{}.*'.format(file_dict[filename]), fullname):
 | 
					            if match('^{}.*'.format(file_dict[filename]), fullname):
 | 
				
			||||||
                file_dict[filename] = fullname
 | 
					                file_dict[filename] = fullname
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -104,6 +104,27 @@ def get_length(file):
 | 
				
			|||||||
    return timedelta(hours=float(data[0]), minutes=float(data[1]), seconds=float(data[2]))
 | 
					    return timedelta(hours=float(data[0]), minutes=float(data[1]), seconds=float(data[2]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def determine_output_filename(url, user_supplied, extension, files_dict):
 | 
				
			||||||
 | 
					    if user_supplied is None:
 | 
				
			||||||
 | 
					        files_dict[File.OUTPUT] = check_output(('youtube-dl', '--get-title', url)).decode('utf-8').strip()
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        files_dict[File.OUTPUT] = user_supplied
 | 
				
			||||||
 | 
					    files_dict[File.OUTPUT] += extension
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def parse_cmd_arguments():
 | 
				
			||||||
 | 
					    parser = ArgumentParser(description='Download player-looped videos with youtube-dl & ffmpeg.')
 | 
				
			||||||
 | 
					    parser.add_argument('-nv', '--nonverbose', action='store_true', help='Turn off non-critical messages to user.')
 | 
				
			||||||
 | 
					    parser.add_argument('-o', '--output', default=None, help='Specify name of the output file (use -e for extension).')
 | 
				
			||||||
 | 
					    parser.add_argument('-e', '--extension', default='mp4', help='Set the container to use for the output.')
 | 
				
			||||||
 | 
					    parser.add_argument('url', type=str, help='The URL of the site containing the video to download.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    args = parser.parse_args()
 | 
				
			||||||
 | 
					    args.extension = '.' + args.extension
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return args
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def yes_no_question(question, default):
 | 
					def yes_no_question(question, default):
 | 
				
			||||||
    valid = {"yes": True, "y": True, "ye": True,
 | 
					    valid = {"yes": True, "y": True, "ye": True,
 | 
				
			||||||
             "no": False, "n": False}
 | 
					             "no": False, "n": False}
 | 
				
			||||||
@@ -160,15 +181,7 @@ def run(url, files_dict, directory):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    # parse arguments
 | 
					    args = parse_cmd_arguments()
 | 
				
			||||||
    parser = ArgumentParser(description='Download player-looped videos with youtube-dl & ffmpeg.')
 | 
					 | 
				
			||||||
    parser.add_argument('-nv', '--nonverbose', action='store_true', help='Turn off non-critical messages to user.')
 | 
					 | 
				
			||||||
    parser.add_argument('-o', '--output', default=None, help='Specify name of the output file (use -e for extension).')
 | 
					 | 
				
			||||||
    parser.add_argument('-e', '--extension', default='mp4', help='Set the container to use for the output.')
 | 
					 | 
				
			||||||
    parser.add_argument('url', type=str, help='The URL of the site containing the video to download.')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    args = parser.parse_args()
 | 
					 | 
				
			||||||
    args.extension = '.' + args.extension
 | 
					 | 
				
			||||||
    VERBOSE = False if args.nonverbose else True
 | 
					    VERBOSE = False if args.nonverbose else True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
 | 
					    FILES = {Stream.AUDIO: 'audio', Stream.VIDEO: 'video',
 | 
				
			||||||
@@ -177,12 +190,7 @@ if __name__ == '__main__':
 | 
				
			|||||||
    OUTPUT_KEYS = [File.OUTPUT]
 | 
					    OUTPUT_KEYS = [File.OUTPUT]
 | 
				
			||||||
    URL = args.url
 | 
					    URL = args.url
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # fetch video title if no filename was specified
 | 
					    determine_output_filename(URL, args.output, args.extension, FILES)
 | 
				
			||||||
    if args.output is None:
 | 
					 | 
				
			||||||
        FILES[File.OUTPUT] = check_output(('youtube-dl', '--get-title', args.url)).decode('utf-8').strip()
 | 
					 | 
				
			||||||
    else:
 | 
					 | 
				
			||||||
        FILES[File.OUTPUT] = args.output
 | 
					 | 
				
			||||||
    FILES[File.OUTPUT] += args.extension
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # ask what to do if output exists
 | 
					    # ask what to do if output exists
 | 
				
			||||||
    if exists(FILES[File.OUTPUT]):
 | 
					    if exists(FILES[File.OUTPUT]):
 | 
				
			||||||
@@ -195,8 +203,9 @@ if __name__ == '__main__':
 | 
				
			|||||||
            remove(FILES[File.OUTPUT])
 | 
					            remove(FILES[File.OUTPUT])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    with TemporaryDirectory() as dir:
 | 
					    with TemporaryDirectory() as dir:
 | 
				
			||||||
 | 
					        # update temporary file locations in FILES dict
 | 
				
			||||||
        for key in FILES:
 | 
					        for key in FILES:
 | 
				
			||||||
            if key not in OUTPUT_KEYS:
 | 
					            if key not in OUTPUT_KEYS:
 | 
				
			||||||
                FILES[key] = '{}/{}'.format(dir, FILES[key])
 | 
					                FILES[key] = join(dir, FILES[key])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        run(URL, FILES, dir)
 | 
					        run(URL, FILES, dir)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user