From 1b7fa758f36f62a1525e998f2e6f72ca66d5d033 Mon Sep 17 00:00:00 2001 From: Wen Date: Tue, 22 Feb 2022 21:53:07 +0800 Subject: [PATCH 1/3] fix(items): update LemonPicPostItem super class closes #5 --- src/favorites_crawler/items.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/favorites_crawler/items.py b/src/favorites_crawler/items.py index 0f974a0..225e58b 100644 --- a/src/favorites_crawler/items.py +++ b/src/favorites_crawler/items.py @@ -64,19 +64,8 @@ def get_filename(self): @dataclass -class LemonPicPostItem: - id: int = field(default=None) - title: str = field(default=None) - image_urls: List = field(default_factory=list) - tags: List = field(default_factory=list) - referer: str = field(default=None) - - def get_filename(self, url): - tags = ' '.join(self.tags) - folder = f'{self.title} [{tags}]' - name = url.rsplit('/', maxsplit=1)[1] - filename = os.path.join(folder, name) - return drop_illegal_characters(filename) +class LemonPicPostItem(BaseItem): + pass @dataclass From 81bed6c5c2e5a8a2f16f68fe2fcfbdc0eb84ea71 Mon Sep 17 00:00:00 2001 From: Wen Date: Tue, 22 Feb 2022 21:55:42 +0800 Subject: [PATCH 2/3] refactor(items): add some method in BaseItem that subclass can overwrite. --- src/favorites_crawler/items.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/favorites_crawler/items.py b/src/favorites_crawler/items.py index 225e58b..6740802 100644 --- a/src/favorites_crawler/items.py +++ b/src/favorites_crawler/items.py @@ -24,11 +24,19 @@ def get_filename(self, url): return unquote(url.rsplit('/', maxsplit=1)[1]) def get_folder_name(self): + name = self.title + prefix = self.get_folder_prefix() + subfix = self.get_folder_subfix() + return f'{prefix}{name}{subfix}' + + def get_folder_prefix(self): + return f'[{self.id}] ' + + def get_folder_subfix(self): tags = ' '.join(self.tags) - prefix = f'[{self.id}] {self.title}' - if tags: - return prefix + f' [{tags}]' - return prefix + if not tags: + return '' + return f' [{tags}]' @dataclass From 5ed2584f68c7fb7d9939c4dd27f5622643d310ab Mon Sep 17 00:00:00 2001 From: Wen Date: Tue, 22 Feb 2022 21:56:09 +0800 Subject: [PATCH 3/3] refactor(items): overwrite LemonPicPostItem.get_folder_prefix --- src/favorites_crawler/items.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/favorites_crawler/items.py b/src/favorites_crawler/items.py index 6740802..e86a69d 100644 --- a/src/favorites_crawler/items.py +++ b/src/favorites_crawler/items.py @@ -73,7 +73,9 @@ def get_filename(self): @dataclass class LemonPicPostItem(BaseItem): - pass + + def get_folder_prefix(self): + return '' @dataclass