From f2946c0a8750a9f16571057665185ae39cdd1f66 Mon Sep 17 00:00:00 2001 From: Serene <33189705+Serene-Arc@users.noreply.github.com> Date: Sat, 27 Mar 2021 21:14:08 +1000 Subject: [PATCH] Strip emojis from filenames on Windows (#222) --- bulkredditdownloader/file_name_formatter.py | 6 ++++++ .../tests/test_file_name_formatter.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/bulkredditdownloader/file_name_formatter.py b/bulkredditdownloader/file_name_formatter.py index 78af753..852f661 100644 --- a/bulkredditdownloader/file_name_formatter.py +++ b/bulkredditdownloader/file_name_formatter.py @@ -96,4 +96,10 @@ class FileNameFormatter: invalid_characters = r'<>:"\/|?*' for char in invalid_characters: input_string = input_string.replace(char, '') + input_string = FileNameFormatter._strip_emojis(input_string) return input_string + + @staticmethod + def _strip_emojis(input_string: str) -> str: + result = input_string.encode('ascii', errors='ignore').decode('utf-8') + return result diff --git a/bulkredditdownloader/tests/test_file_name_formatter.py b/bulkredditdownloader/tests/test_file_name_formatter.py index a5676a5..7a433ef 100644 --- a/bulkredditdownloader/tests/test_file_name_formatter.py +++ b/bulkredditdownloader/tests/test_file_name_formatter.py @@ -188,12 +188,24 @@ def test_shorten_filenames(tmp_path: Path): @pytest.mark.parametrize(('test_string', 'expected'), ( ('test', 'test'), + ('test๐Ÿ˜', 'test'), ('test.png', 'test.png'), ('test*', 'test'), ('test**', 'test'), ('test?*', 'test'), ('test_???.png', 'test_.png'), + ('test_???๐Ÿ˜.png', 'test_.png'), )) def test_format_file_name_for_windows(test_string: str, expected: str): result = FileNameFormatter._format_for_windows(test_string) assert result == expected + + +@pytest.mark.parametrize(('test_string', 'expected'), ( + ('test', 'test'), + ('test๐Ÿ˜', 'test'), + ('๐Ÿ˜', ''), +)) +def test_strip_emojies(test_string: str, expected: str): + result = FileNameFormatter._strip_emojis(test_string) + assert result == expected