Skip to content

Save files to FAVORS_HOME by default #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -155,8 +155,8 @@ yandere:
```

## Download location
By default, pictures will download to working directory.
If you want to change download location, you can add FILES_STORE option to config.
By default, pictures will download to `${FAVORS_HOME}/{site_name}`
If you want to change download location, you can update FILES_STORE.
For example, if you want save pixiv files to `pictures/a`, and want save yandere files to `pictures/b`, you can modify config file like this:
```yaml
pixiv:
2 changes: 1 addition & 1 deletion src/favorites_crawler/commands/crawl.py
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@
"""
spider = spider_loader.load(name)
favors_home = os.getenv('FAVORS_HOME', DEFAULT_FAVORS_HOME)
overwrite_spider_settings(spider, scrapy_settings, load_config(favors_home))
overwrite_spider_settings(spider, favors_home, load_config(favors_home))

Check warning on line 75 in src/favorites_crawler/commands/crawl.py

Codecov / codecov/patch

src/favorites_crawler/commands/crawl.py#L75

Added line #L75 was not covered by tests
process = CrawlerProcess(scrapy_settings)
process.crawl(spider, **kwargs)
for crawler in process.crawlers:
4 changes: 3 additions & 1 deletion src/favorites_crawler/constants/path.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
DEFAULT_FAVORS_HOME = '~/.favorites_crawler'
import os

DEFAULT_FAVORS_HOME = os.path.join('~', '.favorites_crawler')
27 changes: 16 additions & 11 deletions src/favorites_crawler/utils/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import yaml
from copy import deepcopy

import yaml


DEFAULT_CONFIG = {
'global': {
@@ -10,28 +11,28 @@
'EXIF_TOOL_EXECUTABLE': None,
},
'pixiv': {
'FILES_STORE': 'favorites_crawler_files/pixiv',
'FILES_STORE': os.path.join('$FAVORS_HOME', 'pixiv'),
'USER_ID': '',
'ACCESS_TOKEN': '',
'REFRESH_TOKEN': '',
},
'yandere': {
'FILES_STORE': 'favorites_crawler_files/yandere',
'FILES_STORE': os.path.join('$FAVORS_HOME', 'yandere'),
'USERNAME': '',
},
'twitter': {
'FILES_STORE': 'favorites_crawler_files/twitter',
'FILES_STORE': os.path.join('$FAVORS_HOME', 'twitter'),
'USER_ID': '',
'AUTHORIZATION': '',
'LIKES_ID': '',
'X_CSRF_TOKEN': '',
},
'lemon': {
'FILES_STORE': 'favorites_crawler_files/lemon',
'FILES_STORE': os.path.join('$FAVORS_HOME', 'lemon'),
},
'nhentai': {
'USER_AGENT': '',
'FILES_STORE': 'favorites_crawler_files/nhentai',
'FILES_STORE': os.path.join('$FAVORS_HOME', 'nhentai'),
}
}

@@ -63,22 +64,26 @@ def create_favors_home(path: str):
os.makedirs(path, exist_ok=True)


def overwrite_spider_settings(spider, default_settings, user_config):
def overwrite_spider_settings(spider, home, user_config):
"""
Overwrite spider settings by user config
Priority: favors spider config > favors global config > spider custom settings > scrapy settings

:param spider: Spider class
:param default_settings: :class:`scrapy.settings.Settings`
:param home: favors home
:param user_config: favorites crawler config
"""
global_config = user_config.get('global')
if global_config:
spider.custom_settings.update(global_config)

spider_config = user_config.get(spider.name)
spider_config = user_config.get(spider.name, {})
if spider_config:
spider.custom_settings.update(spider_config)

default_files_store = os.path.join(default_settings.get('FILES_STORE', ''), spider.name)
spider.custom_settings.setdefault('FILES_STORE', default_files_store)
home = os.path.expanduser(home)
files_store = spider_config.get('FILES_STORE')
if files_store:
spider.custom_settings['FILES_STORE'] = files_store.replace('$FAVORS_HOME', home)
else:
spider.custom_settings['FILES_STORE'] = os.path.join(home, spider.name)
16 changes: 12 additions & 4 deletions tests/test_utils/test_config.py
Original file line number Diff line number Diff line change
@@ -84,7 +84,7 @@ def test_overwrite_spider_settings(self):
}
spider = spider_loader.load('pixiv')

overwrite_spider_settings(spider, scrapy_settings, user_config)
overwrite_spider_settings(spider, '~', user_config)

assert spider.custom_settings['FILES_STORE'] == user_config['pixiv']['FILES_STORE']
assert spider.custom_settings['ENABLE_ORGANIZE_BY_ARTIST'] == user_config['global']['ENABLE_ORGANIZE_BY_ARTIST']
@@ -100,14 +100,22 @@ def test_spider_config_priority_should_gt_global_config(self):
}
spider = spider_loader.load('yandere')

overwrite_spider_settings(spider, scrapy_settings, user_config)
overwrite_spider_settings(spider, '~', user_config)

assert spider.custom_settings['ENABLE_ORGANIZE_BY_ARTIST'] == user_config['yandere']['ENABLE_ORGANIZE_BY_ARTIST']

def test_should_set_default_file_store_when_user_doesnt_config_it(self):
user_config = {}
spider = spider_loader.load('nhentai')

overwrite_spider_settings(spider, scrapy_settings, user_config)
overwrite_spider_settings(spider, '~', user_config)

assert spider.custom_settings['FILES_STORE'] == os.path.join(scrapy_settings.get('FILES_STORE', ''), 'nhentai')
assert spider.custom_settings['FILES_STORE'] == os.path.expanduser(os.path.join('~', 'nhentai'))

def test_should_replace_favors_home_in_files_store(self):
user_config = {'twitter': {'FILES_STORE': os.path.join('$FAVORS_HOME', 'twitter')}}
spider = spider_loader.load('twitter')

overwrite_spider_settings(spider, '~', user_config)

assert spider.custom_settings['FILES_STORE'] == os.path.expanduser(os.path.join('~', 'twitter'))