Skip to content

Commit

Permalink
Improved code for stripping invalid filename characters. Fixes #70.
Browse files Browse the repository at this point in the history
  • Loading branch information
PetterKraabol committed Jul 21, 2019
1 parent 707ec6d commit 54b1d7b
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions tcd/pipe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import hashlib
import string
import re
from datetime import datetime, timedelta
from typing import List, Optional, Dict, Any

Expand All @@ -22,7 +22,6 @@ def __init__(self, format_dictionary: Dict[str, Any]):
:param format_dictionary: Comment format
"""
self.format_dictionary: Dict[str, Any] = format_dictionary
self.valid_directory_characters: str = f'-_.() {string.ascii_letters}{string.digits}'

# Combine regular format and action_format if provided.
self.combined_formats: str = ''
Expand Down Expand Up @@ -55,10 +54,24 @@ def output(self, video_data: Dict[str, Any]) -> str:
:param video_data: Video data
:return: Output string
"""
video_data['title'] = ''.join(c for c in video_data['title'] if c in self.valid_directory_characters)
output_string = ''.join(c for c in self.format(video_data) if c in self.valid_directory_characters + '/')

# Video title
video_data['title'] = Pipe.get_valid_filename(video_data['title'])

# Output directory and file
output_string = '/'.join([Pipe.get_valid_filename(s) for s in self.format(video_data).split('/')])

return '{}/{}'.format(Arguments().output.rstrip('/').rstrip('\\'), output_string)

@staticmethod
def get_valid_filename(string: str) -> str:
"""
Strip invalid filename characters from value.
:param string: Filename with potentially invalid characters
:return: Valid filename
"""
return re.sub(r'(?u)[^-\w.]', '', string.strip().replace(' ', '_'))

@staticmethod
def timestamp(date_format: str, date_value: str, timezone_name: Optional[str] = None) -> str:
"""
Expand Down

0 comments on commit 54b1d7b

Please sign in to comment.