Skip to content

Commit

Permalink
v8_headless
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmole committed Sep 25, 2024
1 parent caffde7 commit 13ff019
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
12 changes: 10 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
dist/
output/
screenshots/
tribler.dist-info

src/tribler/run.py
src/ec_multichain.pem
src/secondary_key.pem

src/state_directory/
src/venv/

src/tribler/src/tribler/gui/screenshots/

build/debian/tribler/debian/.debhelper/
build/debian/tribler/debian/debhelper-build-stamp
build/debian/tribler/debian/files
build/debian/tribler/debian/*.debhelper
build/debian/tribler/debian/tribler.substvars
build/debian/tribler/debian/tribler/
build/debian/tribler/usr/share/tribler/
build/debian/tribler_*
40 changes: 24 additions & 16 deletions src/run_tribler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import webbrowser
from pathlib import Path

import pystray
from aiohttp import ClientSession
from PIL import Image

Expand All @@ -35,10 +34,13 @@ def parse_args() -> Arguments:
"""
Parse the command-line arguments.
"""
parser = argparse.ArgumentParser(prog='Tribler [Experimental]', description='Run Tribler BitTorrent client')
parser.add_argument('torrent', help='torrent file to download', default='', nargs='?')
parser.add_argument('--log-level', default="INFO", action="store_true", help="set the log level",
parser = argparse.ArgumentParser(prog='Tribler', description='Run Tribler BitTorrent client')
parser.add_argument('torrent', help='Torrent file to download', default='', nargs='?')
parser.add_argument('--log-level', default="INFO", action="store", nargs='?',
help="Set the log level. The default is 'INFO'",
choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'],
dest="log_level")
parser.add_argument('-s', '--server', action='store_true', help="Run headless as a server without graphical pystray interface")
return vars(parser.parse_args())


Expand Down Expand Up @@ -102,12 +104,17 @@ async def main() -> None:
torrent_uri = Path(torrent_uri).read_text()
server_url = await session.find_api_server()

headless = parsed_args.get('server')
if not headless:
import pystray

if server_url:
logger.info("Core already running at %s", server_url)
if torrent_uri:
logger.info("Starting torrent using existing core")
await start_download(config, server_url, torrent_uri)
webbrowser.open_new_tab(server_url + f"?key={config.get('api/key')}")
if not headless:
webbrowser.open_new_tab(server_url + f"?key={config.get('api/key')}")
logger.info("Shutting down")
return

Expand All @@ -116,20 +123,21 @@ async def main() -> None:
server_url = await session.find_api_server()
if server_url and torrent_uri:
await start_download(config, server_url, torrent_uri)

image_path = tribler.get_webui_root() / "public" / "tribler.png"
image = Image.open(image_path.resolve())
api_port = session.rest_manager.get_api_port()
url = f"http://{config.get('api/http_host')}:{api_port}/ui/#/downloads/all?key={config.get('api/key')}"
menu = (pystray.MenuItem('Open', lambda: webbrowser.open_new_tab(url)),
pystray.MenuItem('Quit', lambda: session.shutdown_event.set()))
icon = pystray.Icon("Tribler", icon=image, title="Tribler", menu=menu)
webbrowser.open_new_tab(url)
threading.Thread(target=icon.run).start()
if not headless:
image_path = tribler.get_webui_root() / "public" / "tribler.png"
image = Image.open(image_path.resolve())
api_port = session.rest_manager.get_api_port()
url = f"http://{config.get('api/http_host')}:{api_port}/ui/#/downloads/all?key={config.get('api/key')}"
menu = (pystray.MenuItem('Open', lambda: webbrowser.open_new_tab(url)),
pystray.MenuItem('Quit', lambda: session.shutdown_event.set()))
icon = pystray.Icon("Tribler", icon=image, title="Tribler", menu=menu)
webbrowser.open_new_tab(url)
threading.Thread(target=icon.run).start()

await session.shutdown_event.wait()
await session.shutdown()
icon.stop()
if not headless:
icon.stop()
logger.info("Tribler shutdown completed")


Expand Down

0 comments on commit 13ff019

Please sign in to comment.