Skip to content

Commit 0ec64b1

Browse files
committed
loading clearance and user agent from env automatically
1 parent b8b6393 commit 0ec64b1

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/datafunctions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def finish_backend_init():
4545

4646
enter_splash(self)
4747
load_build_file(self, self.config['autosave_filename'], update_ui=False)
48+
self.downloader.default_session_from_env()
4849
exec_in_thread(
4950
self, populate_cache, self, finished=finish_backend_init,
5051
update_splash=lambda new_text: splash_text(self, new_text))

src/downloader.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from json import loads as json__loads, JSONDecodeError
2+
from os import getenv as os__getenv
23
from pathlib import Path
34
from requests import Session
45
from requests.exceptions import Timeout
@@ -54,6 +55,21 @@ def configure_default_session(self, cookies: dict[str, str] = {}, headers: dict[
5455
self._session.headers.clear()
5556
self._session.headers.update(self._headers)
5657

58+
def default_session_from_env(self):
59+
"""
60+
Configures default session from env variables.
61+
"""
62+
cf_clearance = os__getenv('SETS_CF_CLEARANCE')
63+
if cf_clearance is not None:
64+
self._cookies['cf_clearance'] = cf_clearance
65+
user_agent = os__getenv('SETS_USER_AGENT')
66+
if user_agent is not None:
67+
self._headers['User-Agent'] = user_agent
68+
self._session.cookies.clear()
69+
self._session.cookies.update(self._cookies)
70+
self._session.headers.clear()
71+
self._session.headers.update(self._headers)
72+
5773
def fetch_json(self, url: str) -> dict | list | None:
5874
"""
5975
Fetches json from url and returns parsed object. Returns `None` if retrieveing the json

0 commit comments

Comments
 (0)