diff --git a/potoken_generator/extractor.py b/potoken_generator/extractor.py index 7563cd8..7e51e0a 100644 --- a/potoken_generator/extractor.py +++ b/potoken_generator/extractor.py @@ -4,6 +4,7 @@ import logging import time from dataclasses import dataclass +from pathlib import Path from tempfile import mkdtemp from typing import Optional @@ -26,8 +27,11 @@ def to_json(self) -> str: class PotokenExtractor: - def __init__(self, loop: asyncio.AbstractEventLoop, update_interval: float = 3600) -> None: + def __init__(self, loop: asyncio.AbstractEventLoop, + update_interval: float = 3600, + browser_path: Optional[Path] = None) -> None: self.update_interval: float = update_interval + self.browser_path: Optional[Path] = browser_path self.profile_path = mkdtemp() # cleaned up on exit by nodriver self._loop = loop self._token_info: Optional[TokenInfo] = None @@ -96,8 +100,13 @@ async def _perform_update(self) -> None: async with self._ongoing_update: logger.info('update started') self._extraction_done.clear() - - browser = await nodriver.start(headless=False, user_data_dir=self.profile_path) + try: + browser = await nodriver.start(headless=False, + browser_executable_path=self.browser_path, + user_data_dir=self.profile_path) + except FileNotFoundError as e: + msg = "could not find Chromium. Make sure it's installed or provide direct path to the executable" + raise FileNotFoundError(msg) from e tab = browser.main_tab tab.add_handler(nodriver.cdp.network.RequestWillBeSent, self._send_handler) await tab.get('https://www.youtube.com/embed/jNQXAC9IVRw') diff --git a/potoken_generator/main.py b/potoken_generator/main.py index d9859cc..0f88a81 100644 --- a/potoken_generator/main.py +++ b/potoken_generator/main.py @@ -2,6 +2,7 @@ import asyncio import logging import sys +from pathlib import Path from typing import Optional import nodriver @@ -28,8 +29,9 @@ def print_token_and_exit(token_info: Optional[TokenInfo]): async def run(loop: asyncio.AbstractEventLoop, oneshot: bool, - update_interval: int, bind_address: str, port: int) -> None: - potoken_extractor = PotokenExtractor(loop, update_interval=update_interval) + update_interval: int, bind_address: str, port: int, + browser_path: Optional[Path] = None) -> None: + potoken_extractor = PotokenExtractor(loop, update_interval=update_interval, browser_path=browser_path) token = await potoken_extractor.run_once() if oneshot: print_token_and_exit(token) @@ -78,6 +80,8 @@ def args_parse() -> argparse.Namespace: help='Port webserver is listening on (default: %(default)s)') parser.add_argument('--bind', '-b', default='0.0.0.0', help='Address webserver binds to (default: %(default)s)') + parser.add_argument('--chrome-path', '-c', type=Path, default=None, + help='Path to the Chromiun executable') return parser.parse_args() @@ -85,5 +89,10 @@ def main() -> None: args = args_parse() set_logging(logging.WARNING if args.oneshot else logging.INFO) loop = nodriver.loop() - main_task = run(loop, oneshot=args.oneshot, update_interval=args.update_interval, bind_address=args.bind, port=args.port) + main_task = run(loop, oneshot=args.oneshot, + update_interval=args.update_interval, + bind_address=args.bind, + port=args.port, + browser_path=args.chrome_path + ) loop.run_until_complete(main_task)