New optoin --restrict-filenames
This commit is contained in:
		
							parent
							
								
									71f36332dd
								
							
						
					
					
						commit
						1c469a9480
					
				
					 7 changed files with 77 additions and 41 deletions
				
			
		| 
						 | 
				
			
			@ -44,37 +44,38 @@ class FileDownloader(object):
 | 
			
		|||
 | 
			
		||||
	Available options:
 | 
			
		||||
 | 
			
		||||
	username:         Username for authentication purposes.
 | 
			
		||||
	password:         Password for authentication purposes.
 | 
			
		||||
	usenetrc:         Use netrc for authentication instead.
 | 
			
		||||
	quiet:            Do not print messages to stdout.
 | 
			
		||||
	forceurl:         Force printing final URL.
 | 
			
		||||
	forcetitle:       Force printing title.
 | 
			
		||||
	forcethumbnail:   Force printing thumbnail URL.
 | 
			
		||||
	forcedescription: Force printing description.
 | 
			
		||||
	forcefilename:    Force printing final filename.
 | 
			
		||||
	simulate:         Do not download the video files.
 | 
			
		||||
	format:           Video format code.
 | 
			
		||||
	format_limit:     Highest quality format to try.
 | 
			
		||||
	outtmpl:          Template for output names.
 | 
			
		||||
	ignoreerrors:     Do not stop on download errors.
 | 
			
		||||
	ratelimit:        Download speed limit, in bytes/sec.
 | 
			
		||||
	nooverwrites:     Prevent overwriting files.
 | 
			
		||||
	retries:          Number of times to retry for HTTP error 5xx
 | 
			
		||||
	continuedl:       Try to continue downloads if possible.
 | 
			
		||||
	noprogress:       Do not print the progress bar.
 | 
			
		||||
	playliststart:    Playlist item to start at.
 | 
			
		||||
	playlistend:      Playlist item to end at.
 | 
			
		||||
	matchtitle:       Download only matching titles.
 | 
			
		||||
	rejecttitle:      Reject downloads for matching titles.
 | 
			
		||||
	logtostderr:      Log messages to stderr instead of stdout.
 | 
			
		||||
	consoletitle:     Display progress in console window's titlebar.
 | 
			
		||||
	nopart:           Do not use temporary .part files.
 | 
			
		||||
	updatetime:       Use the Last-modified header to set output file timestamps.
 | 
			
		||||
	writedescription: Write the video description to a .description file
 | 
			
		||||
	writeinfojson:    Write the video description to a .info.json file
 | 
			
		||||
	writesubtitles:   Write the video subtitles to a .srt file
 | 
			
		||||
	subtitleslang:    Language of the subtitles to download
 | 
			
		||||
	username:          Username for authentication purposes.
 | 
			
		||||
	password:          Password for authentication purposes.
 | 
			
		||||
	usenetrc:          Use netrc for authentication instead.
 | 
			
		||||
	quiet:             Do not print messages to stdout.
 | 
			
		||||
	forceurl:          Force printing final URL.
 | 
			
		||||
	forcetitle:        Force printing title.
 | 
			
		||||
	forcethumbnail:    Force printing thumbnail URL.
 | 
			
		||||
	forcedescription:  Force printing description.
 | 
			
		||||
	forcefilename:     Force printing final filename.
 | 
			
		||||
	simulate:          Do not download the video files.
 | 
			
		||||
	format:            Video format code.
 | 
			
		||||
	format_limit:      Highest quality format to try.
 | 
			
		||||
	outtmpl:           Template for output names.
 | 
			
		||||
	restrictfilenames: Do not allow "&" and spaces in file names
 | 
			
		||||
	ignoreerrors:      Do not stop on download errors.
 | 
			
		||||
	ratelimit:         Download speed limit, in bytes/sec.
 | 
			
		||||
	nooverwrites:      Prevent overwriting files.
 | 
			
		||||
	retries:           Number of times to retry for HTTP error 5xx
 | 
			
		||||
	continuedl:        Try to continue downloads if possible.
 | 
			
		||||
	noprogress:        Do not print the progress bar.
 | 
			
		||||
	playliststart:     Playlist item to start at.
 | 
			
		||||
	playlistend:       Playlist item to end at.
 | 
			
		||||
	matchtitle:        Download only matching titles.
 | 
			
		||||
	rejecttitle:       Reject downloads for matching titles.
 | 
			
		||||
	logtostderr:       Log messages to stderr instead of stdout.
 | 
			
		||||
	consoletitle:      Display progress in console window's titlebar.
 | 
			
		||||
	nopart:            Do not use temporary .part files.
 | 
			
		||||
	updatetime:        Use the Last-modified header to set output file timestamps.
 | 
			
		||||
	writedescription:  Write the video description to a .description file
 | 
			
		||||
	writeinfojson:     Write the video description to a .info.json file
 | 
			
		||||
	writesubtitles:    Write the video subtitles to a .srt file
 | 
			
		||||
	subtitleslang:     Language of the subtitles to download
 | 
			
		||||
	"""
 | 
			
		||||
 | 
			
		||||
	params = None
 | 
			
		||||
| 
						 | 
				
			
			@ -349,7 +350,7 @@ class FileDownloader(object):
 | 
			
		|||
	def process_info(self, info_dict):
 | 
			
		||||
		"""Process a single dictionary returned by an InfoExtractor."""
 | 
			
		||||
 | 
			
		||||
		info_dict['stitle'] = sanitize_filename(info_dict['title'])
 | 
			
		||||
		info_dict['stitle'] = sanitize_filename(info_dict['title'], self.params.get('restrictfilenames'))
 | 
			
		||||
 | 
			
		||||
		reason = self._match_entry(info_dict)
 | 
			
		||||
		if reason is not None:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -272,6 +272,9 @@ def parseOpts():
 | 
			
		|||
			help='number downloaded files starting from 00000', default=False)
 | 
			
		||||
	filesystem.add_option('-o', '--output',
 | 
			
		||||
			dest='outtmpl', metavar='TEMPLATE', help='output filename template. Use %(title)s to get the title, %(uploader)s for the uploader name, %(autonumber)s to get an automatically incremented number, %(ext)s for the filename extension, %(upload_date)s for the upload date (YYYYMMDD), %(extractor)s for the provider (youtube, metacafe, etc), %(id)s for the video id and %% for a literal percent. Use - to output to stdout.')
 | 
			
		||||
	filesystem.add_option('--restrict-filenames',
 | 
			
		||||
			action='store_true', dest='restrictfilenames',
 | 
			
		||||
			help='Avoid some characters such as "&" and spaces in filenames', default=False)
 | 
			
		||||
	filesystem.add_option('-a', '--batch-file',
 | 
			
		||||
			dest='batchfile', metavar='FILE', help='file containing URLs to download (\'-\' for stdin)')
 | 
			
		||||
	filesystem.add_option('-w', '--no-overwrites',
 | 
			
		||||
| 
						 | 
				
			
			@ -485,6 +488,7 @@ def _real_main():
 | 
			
		|||
			or (opts.useid and u'%(id)s.%(ext)s')
 | 
			
		||||
			or (opts.autonumber and u'%(autonumber)s-%(id)s.%(ext)s')
 | 
			
		||||
			or u'%(id)s.%(ext)s'),
 | 
			
		||||
		'restrictfilenames': opts.restrictfilenames,
 | 
			
		||||
		'ignoreerrors': opts.ignoreerrors,
 | 
			
		||||
		'ratelimit': opts.ratelimit,
 | 
			
		||||
		'nooverwrites': opts.nooverwrites,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -194,18 +194,22 @@ def timeconvert(timestr):
 | 
			
		|||
	if timetuple is not None:
 | 
			
		||||
		timestamp = email.utils.mktime_tz(timetuple)
 | 
			
		||||
	return timestamp
 | 
			
		||||
	
 | 
			
		||||
def sanitize_filename(s):
 | 
			
		||||
	"""Sanitizes a string so it could be used as part of a filename."""
 | 
			
		||||
 | 
			
		||||
def sanitize_filename(s, restricted=False):
 | 
			
		||||
	"""Sanitizes a string so it could be used as part of a filename.
 | 
			
		||||
	If restricted is set, use a stricter subset of allowed characters.
 | 
			
		||||
	"""
 | 
			
		||||
	def replace_insane(char):
 | 
			
		||||
		if char == '?' or ord(char) < 32 or ord(char) == 127:
 | 
			
		||||
			return ''
 | 
			
		||||
		elif char == '"':
 | 
			
		||||
			return '\''
 | 
			
		||||
			return '' if restricted else 'FOO\''
 | 
			
		||||
		elif char == ':':
 | 
			
		||||
			return ' -'
 | 
			
		||||
			return '_-' if restricted else ' -'
 | 
			
		||||
		elif char in '\\/|*<>':
 | 
			
		||||
			return '-'
 | 
			
		||||
		if restricted and (char in '&\'' or char.isspace()):
 | 
			
		||||
			return '_'
 | 
			
		||||
		return char
 | 
			
		||||
 | 
			
		||||
	result = u''.join(map(replace_insane, s))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue