Add customisable time formatting

This commit is contained in:
Serene-Arc
2021-05-02 13:56:39 +10:00
committed by Serene
parent eda12e5274
commit afa3e2548f
7 changed files with 63 additions and 37 deletions

View File

@@ -25,6 +25,7 @@ _common_options = [
click.option('--upvoted', is_flag=True, default=None),
click.option('--saved', is_flag=True, default=None),
click.option('--search', default=None, type=str),
click.option('--time-format', type=str, default=None),
click.option('-u', '--user', type=str, default=None),
click.option('-t', '--time', type=click.Choice(('all', 'hour', 'day', 'week', 'month', 'year')), default=None),
click.option('-S', '--sort', type=click.Choice(('hot', 'top', 'new',

View File

@@ -33,6 +33,7 @@ class Configuration(Namespace):
self.submitted: bool = False
self.subreddit: list[str] = []
self.time: str = 'all'
self.time_format = None
self.upvoted: bool = False
self.user: Optional[str] = None
self.verbose: int = 0

View File

@@ -3,4 +3,5 @@ client_id = U-6gk4ZCh3IeNQ
client_secret = 7CZHY6AmKweZME5s50SfDGylaPg
scopes = identity, history, read, save
backup_log_count = 3
max_wait_time = 120
max_wait_time = 120
time_format = ISO

View File

@@ -105,6 +105,12 @@ class RedditDownloader:
logger.log(9, 'Wrote default download wait time download to config file')
self.args.max_wait_time = self.cfg_parser.getint('DEFAULT', 'max_wait_time')
logger.debug(f'Setting maximum download wait time to {self.args.max_wait_time} seconds')
if self.args.time_format is None:
option = self.cfg_parser.get('DEFAULT', 'time_format', fallback='ISO')
if re.match(r'^[ \'\"]*$', option):
option = 'ISO'
logger.debug(f'Setting datetime format string to {option}')
self.args.time_format = option
# Update config on disk
with open(self.config_location, 'w') as file:
self.cfg_parser.write(file)
@@ -358,7 +364,7 @@ class RedditDownloader:
raise errors.BulkDownloaderException(f'User {name} is banned')
def _create_file_name_formatter(self) -> FileNameFormatter:
return FileNameFormatter(self.args.file_scheme, self.args.folder_scheme)
return FileNameFormatter(self.args.file_scheme, self.args.folder_scheme, self.args.time_format)
def _create_time_filter(self) -> RedditTypes.TimeType:
try:

View File

@@ -26,18 +26,18 @@ class FileNameFormatter:
'upvotes',
)
def __init__(self, file_format_string: str, directory_format_string: str):
def __init__(self, file_format_string: str, directory_format_string: str, time_format_string: str):
if not self.validate_string(file_format_string):
raise BulkDownloaderException(f'"{file_format_string}" is not a valid format string')
self.file_format_string = file_format_string
self.directory_format_string: list[str] = directory_format_string.split('/')
self.time_format_string = time_format_string
@staticmethod
def _format_name(submission: (Comment, Submission), format_string: str) -> str:
def _format_name(self, submission: (Comment, Submission), format_string: str) -> str:
if isinstance(submission, Submission):
attributes = FileNameFormatter._generate_name_dict_from_submission(submission)
attributes = self._generate_name_dict_from_submission(submission)
elif isinstance(submission, Comment):
attributes = FileNameFormatter._generate_name_dict_from_comment(submission)
attributes = self._generate_name_dict_from_comment(submission)
else:
raise BulkDownloaderException(f'Cannot name object {type(submission).__name__}')
result = format_string
@@ -65,8 +65,7 @@ class FileNameFormatter:
in_string = in_string.replace(match, converted_match)
return in_string
@staticmethod
def _generate_name_dict_from_submission(submission: Submission) -> dict:
def _generate_name_dict_from_submission(self, submission: Submission) -> dict:
submission_attributes = {
'title': submission.title,
'subreddit': submission.subreddit.display_name,
@@ -74,17 +73,18 @@ class FileNameFormatter:
'postid': submission.id,
'upvotes': submission.score,
'flair': submission.link_flair_text,
'date': FileNameFormatter._convert_timestamp(submission.created_utc),
'date': self._convert_timestamp(submission.created_utc),
}
return submission_attributes
@staticmethod
def _convert_timestamp(timestamp: float) -> str:
def _convert_timestamp(self, timestamp: float) -> str:
input_time = datetime.datetime.fromtimestamp(timestamp)
return input_time.isoformat()
if self.time_format_string.upper().strip() == 'ISO':
return input_time.isoformat()
else:
return input_time.strftime(self.time_format_string)
@staticmethod
def _generate_name_dict_from_comment(comment: Comment) -> dict:
def _generate_name_dict_from_comment(self, comment: Comment) -> dict:
comment_attributes = {
'title': comment.submission.title,
'subreddit': comment.subreddit.display_name,
@@ -92,7 +92,7 @@ class FileNameFormatter:
'postid': comment.id,
'upvotes': comment.score,
'flair': '',
'date': FileNameFormatter._convert_timestamp(comment.created_utc),
'date': self._convert_timestamp(comment.created_utc),
}
return comment_attributes
@@ -160,9 +160,8 @@ class FileNameFormatter:
result = any([f'{{{key}}}' in test_string.lower() for key in FileNameFormatter.key_terms])
if result:
if 'POSTID' not in test_string:
logger.warning(
'Some files might not be downloaded due to name conflicts as filenames are'
' not guaranteed to be be unique without {POSTID}')
logger.warning('Some files might not be downloaded due to name conflicts as filenames are'
' not guaranteed to be be unique without {POSTID}')
return True
else:
return False