Skip to content

Commit

Permalink
Add a command line argument allowing to specify the path to a Chromiu…
Browse files Browse the repository at this point in the history
…m binary
  • Loading branch information
15532th committed Sep 19, 2024
1 parent ae4dea4 commit a997539
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
15 changes: 12 additions & 3 deletions potoken_generator/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import time
from dataclasses import dataclass
from pathlib import Path
from tempfile import mkdtemp
from typing import Optional

Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand Down
15 changes: 12 additions & 3 deletions potoken_generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
import logging
import sys
from pathlib import Path
from typing import Optional

import nodriver
Expand All @@ -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)
Expand Down Expand Up @@ -78,12 +80,19 @@ 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()


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)

0 comments on commit a997539

Please sign in to comment.