-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: catch exceptions raised by browser-cookie3 by default
(ref #10)
- Loading branch information
1 parent
f90c207
commit 02a92d3
Showing
5 changed files
with
65 additions
and
40 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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import browser_cookie3 as bc3 | ||
|
||
from .logger import logger | ||
|
||
|
||
def load_browser_cookies(domain_name: str = "", verbose=True) -> dict: | ||
""" | ||
Try to load cookies from all supported browsers and return combined cookiejar. | ||
Optionally pass in a domain name to only load cookies from the specified domain. | ||
Parameters | ||
---------- | ||
domain_name : str, optional | ||
Domain name to filter cookies by, by default will load all cookies without filtering. | ||
verbose : bool, optional | ||
If `True`, will print more infomation in logs. | ||
Returns | ||
------- | ||
`dict` | ||
Dictionary with cookie name as key and cookie value as value. | ||
""" | ||
cookies = {} | ||
for cookie_fn in [ | ||
bc3.chrome, | ||
bc3.chromium, | ||
bc3.opera, | ||
bc3.opera_gx, | ||
bc3.brave, | ||
bc3.edge, | ||
bc3.vivaldi, | ||
bc3.firefox, | ||
bc3.librewolf, | ||
bc3.safari, | ||
]: | ||
try: | ||
for cookie in cookie_fn(domain_name=domain_name): | ||
cookies[cookie.name] = cookie.value | ||
except bc3.BrowserCookieError: | ||
pass | ||
except PermissionError as e: | ||
if verbose: | ||
logger.warning( | ||
f"Permission denied while trying to load cookies from {cookie_fn.__name__}. {e}" | ||
) | ||
except Exception as e: | ||
if verbose: | ||
logger.error( | ||
f"Error happened while trying to load cookies from {cookie_fn.__name__}. {e}" | ||
) | ||
|
||
return cookies |
File renamed without changes.