From 358357590fc1cfd5777e94629bb9f4dd2b76af9d Mon Sep 17 00:00:00 2001 From: Serene-Arc Date: Thu, 11 Feb 2021 09:08:47 +1000 Subject: [PATCH] Add file name formatter class --- bulkredditdownloader/file_name_formatter.py | 40 ++++++++++ .../tests/test_file_name_formatter.py | 74 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 bulkredditdownloader/file_name_formatter.py create mode 100644 bulkredditdownloader/tests/test_file_name_formatter.py diff --git a/bulkredditdownloader/file_name_formatter.py b/bulkredditdownloader/file_name_formatter.py new file mode 100644 index 0000000..461947e --- /dev/null +++ b/bulkredditdownloader/file_name_formatter.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# coding=utf-8 + +import re +from pathlib import Path + +import praw.models + +from bulkredditdownloader.resource import Resource + + +class FileNameFormatter: + def __init__(self, file_format_string: str, directory_format_string: str): + self.file_format_string = file_format_string + self.directory_format_string = directory_format_string + + @staticmethod + def _format_name(submission: praw.models.Submission, format_string: str) -> str: + submission_attributes = { + 'title': submission.title, + 'subreddit': submission.subreddit.display_name, + 'redditor': submission.author.name, + 'postid': submission.id, + 'upvotes': submission.score, + 'flair': submission.link_flair_text, + 'date': submission.created_utc + } + result = format_string + for key in submission_attributes.keys(): + if re.search(r'(?i).*{{{}}}.*'.format(key), result): + result = re.sub(r'(?i){{{}}}'.format(key), str(submission_attributes.get(key, 'unknown')), result) + + result = result.replace('/', '') + return result + + def format_path(self, resource: Resource, destination_directory: Path) -> Path: + subfolder = destination_directory / self._format_name(resource.source_submission, self.directory_format_string) + file_path = subfolder / (str(self._format_name(resource.source_submission, + self.file_format_string)) + resource.extension) + return file_path diff --git a/bulkredditdownloader/tests/test_file_name_formatter.py b/bulkredditdownloader/tests/test_file_name_formatter.py new file mode 100644 index 0000000..94a6245 --- /dev/null +++ b/bulkredditdownloader/tests/test_file_name_formatter.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +# coding=utf-8 + +from pathlib import Path +from unittest.mock import Mock + +import praw.models +import pytest + +from bulkredditdownloader.file_name_formatter import FileNameFormatter +from bulkredditdownloader.resource import Resource + + +@pytest.fixture() +def submission() -> Mock: + test = Mock() + test.title = 'name' + test.subreddit.display_name = 'randomreddit' + test.author.name = 'person' + test.id = '12345' + test.score = 1000 + test.link_flair_text = 'test_flair' + test.created_utc = 123456789 + return test + + +@pytest.fixture() +def reddit_submission() -> praw.models.Submission: + rd = praw.Reddit(client_id='U-6gk4ZCh3IeNQ', client_secret='7CZHY6AmKweZME5s50SfDGylaPg', user_agent='test') + return rd.submission(id='lgilgt') + + +@pytest.mark.parametrize(('format_string', 'expected'), (('{SUBREDDIT}', 'randomreddit'), + ('{REDDITOR}', 'person'), + ('{POSTID}', '12345'), + ('{UPVOTES}', '1000'), + ('{FLAIR}', 'test_flair'), + ('{DATE}', '123456789'), + ('{REDDITOR}_{TITLE}_{POSTID}', 'person_name_12345') + )) +def test_format_name_mock(format_string: str, expected: str, submission: Mock): + result = FileNameFormatter._format_name(submission, format_string) + assert result == expected + + +@pytest.mark.parametrize(('format_string', 'expected'), + (('{SUBREDDIT}', 'Mindustry'), + ('{REDDITOR}', 'Gamer_player_boi'), + ('{POSTID}', 'lgilgt'), + ('{FLAIR}', 'Art'), + ('{SUBREDDIT}_{TITLE}', 'Mindustry_Toxopid that is NOT humane >:('), + ('{REDDITOR}_{TITLE}_{POSTID}', 'Gamer_player_boi_Toxopid that is NOT humane >:(_lgilgt') + )) +def test_format_name_real(format_string: str, expected: str, reddit_submission: praw.models.Submission): + result = FileNameFormatter._format_name(reddit_submission, format_string) + assert result == expected + + +@pytest.mark.parametrize(('format_string_directory', 'format_string_file', 'expected'), + (('{SUBREDDIT}', '{POSTID}', 'test/Mindustry/lgilgt.png'), + ('{SUBREDDIT}', '{TITLE}_{POSTID}', + 'test/Mindustry/Toxopid that is NOT humane >:(_lgilgt.png'), + ('{SUBREDDIT}', '{REDDITOR}_{TITLE}_{POSTID}', + 'test/Mindustry/Gamer_player_boi_Toxopid that is NOT humane >:(_lgilgt.png') + )) +def test_format_full( + format_string_directory: str, + format_string_file: str, + expected: str, + reddit_submission: praw.models.Submission): + test_resource = Resource(reddit_submission, 'i.reddit.com/blabla.png', b'') + test_formatter = FileNameFormatter(format_string_file, format_string_directory) + result = test_formatter.format_path(test_resource, Path('test')) + assert str(result) == expected