13
13
14
14
import pystray
15
15
import tribler
16
+ from aiohttp import ClientSession
16
17
from PIL import Image
17
18
from tribler .core .session import Session
18
19
from tribler .tribler_config import TriblerConfigManager
@@ -50,6 +51,19 @@ def get_root_state_directory(requested_path: os.PathLike | None) -> Path:
50
51
return root_state_dir
51
52
52
53
54
+ async def start_download (config : TriblerConfigManager , server_url : str , torrent_uri : str ) -> None :
55
+ """
56
+ Start a download by calling the REST API.
57
+ """
58
+ async with ClientSession () as client , client .put (server_url + "/api/downloads" ,
59
+ headers = {"X-Api-Key" : config .get ("api/key" )},
60
+ json = {"uri" : torrent_uri }) as response :
61
+ if response .status == 200 :
62
+ logger .info ("Successfully started torrent %s" , torrent_uri )
63
+ else :
64
+ logger .warning ("Failed to start torrent %s: %s" , torrent_uri , await response .text ())
65
+
66
+
53
67
async def main () -> None :
54
68
"""
55
69
The main script entry point.
@@ -60,38 +74,55 @@ async def main() -> None:
60
74
61
75
root_state_dir = get_root_state_directory (os .environ .get ('TSTATEDIR' , 'state_directory' ))
62
76
logger .info ("Root state dir: %s" , root_state_dir )
63
-
64
- api_port , api_key = int (os .environ .get ('CORE_API_PORT' , '0' )), os .environ .get ('CORE_API_KEY' )
65
-
66
77
config = TriblerConfigManager (root_state_dir / "configuration.json" )
67
78
config .set ("state_dir" , str (root_state_dir ))
68
79
69
- if config .get ("api/refresh_port_on_start" ):
70
- config .set ("api/http_port" , 0 )
71
- config .set ("api/https_port" , 0 )
72
-
73
- if api_key is None and config .get ("api/key" ) is None :
74
- api_key = os .urandom (16 ).hex ()
75
-
76
- if api_key is not None and api_key != config .get ("api/key" ):
77
- config .set ("api/key" , api_key )
80
+ if "CORE_API_PORT" in os .environ :
81
+ config .set ("api/http_port" , int (os .environ .get ("CORE_API_PORT" )))
78
82
config .write ()
79
83
80
- if api_port is not None and api_port != config . get ( "api/http_port" ) :
81
- config .set ("api/http_port " , api_port )
84
+ if "CORE_API_KEY" in os . environ :
85
+ config .set ("api/key " , os . environ . get ( "CORE_API_KEY" ) )
82
86
config .write ()
83
87
84
- logger .info ("Start tribler core. API port: %d. API key: %s." , api_port , config .get ("api/key" ))
88
+ if config .get ("api/key" ) is None :
89
+ config .set ("api/key" , os .urandom (16 ).hex ())
90
+ config .write ()
85
91
92
+ logger .info ("Creating session. API port: %d. API key: %s." , config .get ("api/http_port" ), config .get ("api/key" ))
86
93
session = Session (config )
94
+
95
+ torrent_uri = parsed_args .get ('torrent' )
96
+ if torrent_uri and os .path .exists (torrent_uri ):
97
+ if torrent_uri .endswith (".torrent" ):
98
+ torrent_uri = Path (torrent_uri ).as_uri ()
99
+ if torrent_uri .endswith (".magnet" ):
100
+ torrent_uri = Path (torrent_uri ).read_text ()
101
+ server_url = await session .find_api_server ()
102
+
103
+ if server_url :
104
+ logger .info ("Core already running at %s" , server_url )
105
+ if torrent_uri :
106
+ logger .info ("Starting torrent using existing core" )
107
+ await start_download (config , server_url , torrent_uri )
108
+ webbrowser .open_new_tab (server_url + f"?key={ config .get ('api/key' )} " )
109
+ logger .info ("Shutting down" )
110
+ return
111
+
87
112
await session .start ()
88
113
114
+ server_url = await session .find_api_server ()
115
+ if server_url and torrent_uri :
116
+ await start_download (config , server_url , torrent_uri )
117
+
89
118
image_path = Path (tribler .__file__ ).parent / "ui/public/tribler.png"
90
119
image = Image .open (image_path .resolve ())
91
- url = f"http://localhost:{ session .rest_manager .get_api_port ()} /ui/#/downloads/all?key={ config .get ('api/key' )} "
120
+ api_port = session .rest_manager .get_api_port ()
121
+ url = f"http://{ config .get ('api/http_host' )} :{ api_port } /ui/#/downloads/all?key={ config .get ('api/key' )} "
92
122
menu = (pystray .MenuItem ('Open' , lambda : webbrowser .open_new_tab (url )),
93
123
pystray .MenuItem ('Quit' , lambda : session .shutdown_event .set ()))
94
124
icon = pystray .Icon ("Tribler" , icon = image , title = "Tribler" , menu = menu )
125
+ webbrowser .open_new_tab (url )
95
126
threading .Thread (target = icon .run ).start ()
96
127
97
128
await session .shutdown_event .wait ()
0 commit comments