-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from RyouMon/fix-load-cookie
Fix load_cookie
- Loading branch information
Showing
7 changed files
with
61 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ unidecode==1.3.8 | |
langdetect==1.0.9 | ||
pykakasi==2.2.1 | ||
gppt==4.1.0 | ||
typer>=0.14.0 | ||
typer>=0.14.0 | ||
loguru>=0.7.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
from pathlib import Path | ||
from http.cookiejar import MozillaCookieJar | ||
|
||
|
||
cookie_home = os.path.expanduser('~/.favorites_crawler') | ||
from loguru import logger | ||
|
||
|
||
def load_cookie(domain): | ||
def load_cookie(domain: str, home: str | Path) -> dict: | ||
"""Load 'Netscape HTTP Cookie File' as dict""" | ||
cookiejar = MozillaCookieJar() | ||
cookiejar.load(os.path.join(cookie_home, f'{domain}_cookies.txt')) | ||
try: | ||
cookiejar = MozillaCookieJar() | ||
cookie_file = os.path.join(home, f'{domain}_cookies.txt') | ||
cookiejar.load(cookie_file) | ||
except Exception as e: | ||
logger.error('Failed to load cookie {}, {!r}', cookie_file, e) | ||
return {} | ||
return {getattr(c, 'name'): getattr(c, 'value') for c in cookiejar} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from favorites_crawler.utils.cookies import load_cookie | ||
|
||
|
||
class TestLoadCookie: | ||
def test_load_cookie_when_file_exists(self, tmp_path): | ||
domain = 'localhost' | ||
cookie_file = tmp_path / f'{domain}_cookies.txt' | ||
cookie_file.touch() | ||
cookie_file.write_text( | ||
"""# Netscape HTTP Cookie File | ||
# http://curl.haxx.se/rfc/cookie_spec.html | ||
# This is a generated file! Do not edit. | ||
localhost FALSE / TRUE 9933144989 User-Agent Test | ||
""" | ||
) | ||
|
||
cookie = load_cookie(domain, tmp_path) | ||
|
||
assert cookie == {'User-Agent': 'Test'} | ||
|
||
def test_load_cookie_when_file_not_exists(self, tmp_path): | ||
domain = 'localhost' | ||
|
||
cookie = load_cookie(domain, tmp_path) | ||
|
||
assert cookie == {} | ||
|
||
def test_load_cookie_when_file_invalid(self, tmp_path): | ||
domain = 'localhost' | ||
cookie_file = tmp_path / f'{domain}_cookies.txt' | ||
cookie_file.touch() | ||
cookie_file.write_text('') | ||
cookie = load_cookie(domain, tmp_path) | ||
|
||
assert cookie == {} |