1
0
Fork 0
mirror of synced 2024-09-28 15:22:16 +12:00

Use ISO format for timestamps in names

This commit is contained in:
Serene-Arc 2021-04-22 10:38:32 +10:00
parent 2eab4052c5
commit d960bc0b7b
2 changed files with 22 additions and 5 deletions

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# coding=utf-8 # coding=utf-8
import datetime
import logging import logging
import platform import platform
import re import re
@ -74,10 +74,15 @@ class FileNameFormatter:
'postid': submission.id, 'postid': submission.id,
'upvotes': submission.score, 'upvotes': submission.score,
'flair': submission.link_flair_text, 'flair': submission.link_flair_text,
'date': submission.created_utc 'date': FileNameFormatter._convert_timestamp(submission.created_utc),
} }
return submission_attributes return submission_attributes
@staticmethod
def _convert_timestamp(timestamp: float) -> str:
input_time = datetime.datetime.fromtimestamp(timestamp)
return input_time.isoformat()
@staticmethod @staticmethod
def _generate_name_dict_from_comment(comment: Comment) -> dict: def _generate_name_dict_from_comment(comment: Comment) -> dict:
comment_attributes = { comment_attributes = {
@ -87,7 +92,7 @@ class FileNameFormatter:
'postid': comment.id, 'postid': comment.id,
'upvotes': comment.score, 'upvotes': comment.score,
'flair': '', 'flair': '',
'date': comment.created_utc, 'date': FileNameFormatter._convert_timestamp(comment.created_utc),
} }
return comment_attributes return comment_attributes

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# coding=utf-8 # coding=utf-8
from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
from unittest.mock import MagicMock from unittest.mock import MagicMock
@ -21,7 +22,7 @@ def submission() -> MagicMock:
test.id = '12345' test.id = '12345'
test.score = 1000 test.score = 1000
test.link_flair_text = 'test_flair' test.link_flair_text = 'test_flair'
test.created_utc = 123456789 test.created_utc = datetime(2021, 4, 21, 9, 30, 0).timestamp()
test.__class__ = praw.models.Submission test.__class__ = praw.models.Submission
return test return test
@ -37,7 +38,7 @@ def reddit_submission(reddit_instance: praw.Reddit) -> praw.models.Submission:
('{POSTID}', '12345'), ('{POSTID}', '12345'),
('{UPVOTES}', '1000'), ('{UPVOTES}', '1000'),
('{FLAIR}', 'test_flair'), ('{FLAIR}', 'test_flair'),
('{DATE}', '123456789'), ('{DATE}', '2021-04-21T09:30:00'),
('{REDDITOR}_{TITLE}_{POSTID}', 'person_name_12345'), ('{REDDITOR}_{TITLE}_{POSTID}', 'person_name_12345'),
('{RANDOM}', '{RANDOM}'), ('{RANDOM}', '{RANDOM}'),
)) ))
@ -318,3 +319,14 @@ def test_preserve_emojis(test_name_string: str, expected: str, submission: Magic
def test_convert_unicode_escapes(test_string: str, expected: str): def test_convert_unicode_escapes(test_string: str, expected: str):
result = FileNameFormatter._convert_unicode_escapes(test_string) result = FileNameFormatter._convert_unicode_escapes(test_string)
assert result == expected assert result == expected
@pytest.mark.parametrize(('test_datetime', 'expected'), (
(datetime(2020, 1, 1, 8, 0, 0), '2020-01-01T08:00:00'),
(datetime(2020, 1, 1, 8, 0), '2020-01-01T08:00:00'),
(datetime(2021, 4, 21, 8, 30, 21), '2021-04-21T08:30:21'),
))
def test_convert_timestamp(test_datetime: datetime, expected: str):
test_timestamp = test_datetime.timestamp()
result = FileNameFormatter._convert_timestamp(test_timestamp)
assert result == expected