implemented a wrapper helping with verbosity
This commit is contained in:
		
							
								
								
									
										24
									
								
								coub-dl.py
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								coub-dl.py
									
									
									
									
									
								
							@@ -6,6 +6,7 @@ from enum import Enum
 | 
				
			|||||||
from datetime import timedelta
 | 
					from datetime import timedelta
 | 
				
			||||||
from math import floor
 | 
					from math import floor
 | 
				
			||||||
from argparse import ArgumentParser
 | 
					from argparse import ArgumentParser
 | 
				
			||||||
 | 
					from functools import wraps
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -31,6 +32,16 @@ class File(Enum):
 | 
				
			|||||||
    FRACTION = 3
 | 
					    FRACTION = 3
 | 
				
			||||||
    OUTPUT = 4
 | 
					    OUTPUT = 4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def call_verbose(before_message=''):
 | 
				
			||||||
 | 
					    def wrap(f):
 | 
				
			||||||
 | 
					        @wraps(f)
 | 
				
			||||||
 | 
					        def wrapper(*args, **kwargs):
 | 
				
			||||||
 | 
					            print_opt(before_message+' ', end='', flush=True)
 | 
				
			||||||
 | 
					            f(*args, **kwargs)
 | 
				
			||||||
 | 
					            print_opt('Done!')
 | 
				
			||||||
 | 
					        return wrapper
 | 
				
			||||||
 | 
					    return wrap
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def print_opt(*args, **kwargs):
 | 
					def print_opt(*args, **kwargs):
 | 
				
			||||||
    if VERBOSE:
 | 
					    if VERBOSE:
 | 
				
			||||||
        print(*args, **kwargs)
 | 
					        print(*args, **kwargs)
 | 
				
			||||||
@@ -48,20 +59,20 @@ def getDuration(ffprobe_output):
 | 
				
			|||||||
        raise ValueError('Cannot process ffprobe output!')
 | 
					        raise ValueError('Cannot process ffprobe output!')
 | 
				
			||||||
    return duration
 | 
					    return duration
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def downloadStreams():
 | 
					@call_verbose(before_message='Downloading audio stream...')
 | 
				
			||||||
    print_opt('Downloading audio stream... ', end='', flush=True)
 | 
					def download_audio_stream():
 | 
				
			||||||
    call(('youtube-dl', '--ignore-config',
 | 
					    call(('youtube-dl', '--ignore-config',
 | 
				
			||||||
                        '--extract-audio',
 | 
					                        '--extract-audio',
 | 
				
			||||||
                        '--output', '{}.%(ext)s'.format(FILES[Stream.AUDIO]),
 | 
					                        '--output', '{}.%(ext)s'.format(FILES[Stream.AUDIO]),
 | 
				
			||||||
          URL),
 | 
					          URL),
 | 
				
			||||||
         stdout=DEVNULL, stderr=DEVNULL)
 | 
					         stdout=DEVNULL, stderr=DEVNULL)
 | 
				
			||||||
    print_opt('Done!')
 | 
					
 | 
				
			||||||
    print_opt('Downloading video stream... ', end='', flush=True)
 | 
					@call_verbose(before_message='Downloading video stream...')
 | 
				
			||||||
 | 
					def download_video_stream():
 | 
				
			||||||
    call(('youtube-dl', '--ignore-config',
 | 
					    call(('youtube-dl', '--ignore-config',
 | 
				
			||||||
                        '--output', '{}.%(ext)s'.format(FILES[Stream.VIDEO]),
 | 
					                        '--output', '{}.%(ext)s'.format(FILES[Stream.VIDEO]),
 | 
				
			||||||
          URL),
 | 
					          URL),
 | 
				
			||||||
         stdout=DEVNULL, stderr=DEVNULL)
 | 
					         stdout=DEVNULL, stderr=DEVNULL)
 | 
				
			||||||
    print_opt('Done!')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def readExtensions():
 | 
					def readExtensions():
 | 
				
			||||||
    for file in listdir():
 | 
					    for file in listdir():
 | 
				
			||||||
@@ -116,7 +127,8 @@ if exists(FILES[File.OUTPUT]):
 | 
				
			|||||||
        remove(FILES[File.OUTPUT])
 | 
					        remove(FILES[File.OUTPUT])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# download streams and update FILE dict with extensions
 | 
					# download streams and update FILE dict with extensions
 | 
				
			||||||
downloadStreams()
 | 
					download_audio_stream()
 | 
				
			||||||
 | 
					download_video_stream()
 | 
				
			||||||
readExtensions()
 | 
					readExtensions()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# get stream lengths via ffprobe
 | 
					# get stream lengths via ffprobe
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user