Skip to content

Commit d5fe54c

Browse files
authored
Merge pull request #66 from RyouMon/fix-crawl-pixiv
fix: fix crawl pixiv
2 parents f68bcbe + 82006d1 commit d5fe54c

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

src/favorites_crawler/commands/crawl.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from favorites_crawler.utils.config import load_config, overwrite_spider_settings
1212
from favorites_crawler.constants.path import DEFAULT_FAVORS_HOME
13+
from favorites_crawler.utils.auth import refresh_pixiv_token
1314

1415
app = typer.Typer(help='Crawl your favorites from websites.', no_args_is_help=True)
1516

@@ -27,7 +28,9 @@ def crawl_yandere():
2728
@app.command('pixiv')
2829
def crawl_pixiv():
2930
"""Crawl your favorite illustrations from pixiv."""
30-
crawl('pixiv')
31+
favors_home = os.getenv('FAVORS_HOME', DEFAULT_FAVORS_HOME)
32+
access_token = refresh_pixiv_token(favors_home)
33+
crawl('pixiv', access_token=access_token)
3134

3235

3336
@app.command('nhentai')

src/favorites_crawler/middlewares.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
# See documentation in:
44
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html
55

6-
from favorites_crawler.utils.auth import refresh_pixiv
7-
86

97
class PixivAuthorizationMiddleware:
10-
def __init__(self):
11-
self.access_token = refresh_pixiv()
12-
138
def process_request(self, request, spider):
14-
if self.access_token:
15-
request.headers.setdefault(b'Authorization', f'Bearer {self.access_token}')
9+
request.headers.setdefault(b'Authorization', f'Bearer {spider.access_token}')

src/favorites_crawler/utils/auth.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from __future__ import annotations
2+
13
import json
24
import re
5+
from pathlib import Path
36
from urllib.parse import unquote
47

58
from gppt import GetPixivToken
@@ -23,8 +26,8 @@ def _GetPixivToken__wait_for_redirect(self) -> None:
2326
raise ValueError(msg) from err
2427

2528

26-
def refresh_pixiv():
27-
config = load_config()
29+
def refresh_pixiv_token(home: str | Path):
30+
config = load_config(home)
2831
pixiv_config = config.get('pixiv', {})
2932
refresh_token = pixiv_config.get('REFRESH_TOKEN')
3033
if not refresh_token:
@@ -33,7 +36,7 @@ def refresh_pixiv():
3336
login_info = token_getter.refresh(refresh_token)
3437
access_token = login_info['access_token']
3538
pixiv_config['ACCESS_TOKEN'] = access_token
36-
dump_config(config)
39+
dump_config(config, home)
3740
return access_token
3841

3942

src/favorites_crawler/utils/config.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from __future__ import annotations
2+
13
import os
4+
from pathlib import Path
25
from copy import deepcopy
36

47
import yaml
@@ -37,7 +40,7 @@
3740
}
3841

3942

40-
def load_config(home: str) -> dict:
43+
def load_config(home: str | Path) -> dict:
4144
"""Load config from user home"""
4245
home = os.path.expanduser(home)
4346
create_favors_home(home)
@@ -49,7 +52,7 @@ def load_config(home: str) -> dict:
4952
return yaml.safe_load(f)
5053

5154

52-
def dump_config(data: dict, home: str):
55+
def dump_config(data: dict, home: str | Path):
5356
"""Dump config data to user home"""
5457
home = os.path.expanduser(home)
5558
create_favors_home(home)
@@ -58,7 +61,7 @@ def dump_config(data: dict, home: str):
5861
yaml.safe_dump(data, f, allow_unicode=True)
5962

6063

61-
def create_favors_home(path: str):
64+
def create_favors_home(path: str | Path):
6265
"""Create favors home if not exists"""
6366
if not os.path.exists(path):
6467
os.makedirs(path, exist_ok=True)

tests/test_utils/test_auth.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
from unittest.mock import patch
2+
13
import pytest
24

3-
from favorites_crawler.utils.auth import parse_twitter_likes_url, parser_twitter_likes_features
5+
from favorites_crawler.utils.auth import parse_twitter_likes_url, parser_twitter_likes_features, \
6+
refresh_pixiv_token
7+
from favorites_crawler.utils.config import dump_config, load_config
48

59

610
def test_parser_twitter_likes_features():
@@ -36,3 +40,35 @@ def test_parser_twitter_likes_features():
3640
))
3741
def test_twitter_parse_likes_url(url, expected):
3842
assert parse_twitter_likes_url(url) == expected
43+
44+
45+
@patch('favorites_crawler.utils.auth.CustomGetPixivToken')
46+
class TestRefreshPixivToken:
47+
def test_should_return_access_token_if_refresh_token_exists(self, mock_gppt, tmp_path):
48+
favors_home = tmp_path / 'home'
49+
refresh_token = 'refresh_token'
50+
new_access_token = 'new_access_token'
51+
config = {
52+
'pixiv': {
53+
'ACCESS_TOKEN': 'old_access_token',
54+
'REFRESH_TOKEN': refresh_token,
55+
}
56+
}
57+
dump_config(config, favors_home)
58+
mock_refresh = mock_gppt.return_value.refresh
59+
mock_refresh.return_value = {'access_token': new_access_token}
60+
61+
access_token = refresh_pixiv_token(favors_home)
62+
63+
mock_refresh.assert_called_once_with(refresh_token)
64+
assert access_token == new_access_token
65+
assert load_config(favors_home)['pixiv']['ACCESS_TOKEN'] == new_access_token
66+
67+
def test_should_raise_value_error_if_refresh_token_not_exists(self, mock_gppt, tmp_path):
68+
favors_home = tmp_path / 'home'
69+
mock_refresh = mock_gppt.return_value.refresh
70+
71+
with pytest.raises(ValueError):
72+
refresh_pixiv_token(favors_home)
73+
74+
mock_refresh.assert_not_called()

0 commit comments

Comments
 (0)