-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (71 loc) · 2.45 KB
/
main.py
File metadata and controls
89 lines (71 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import asyncio
import logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
try:
import uvloop # type: ignore[import]
uvloop.install()
except ImportError:
logging.warning("uvloop is not installed, using default event loop")
from uvicorn.config import Config as UvicornConfig
from uvicorn.server import Server
from tgfs.app import create_app
from tgfs.config import Config, get_config
from tgfs.core import Client, Clients
from tgfs.telegram import PyrogramAPI, TDLibApi, TelethonAPI, pyrogram, telethon
async def create_clients(config: Config) -> Clients:
if config.telegram.lib == "pyrogram":
tdlib_api = TDLibApi(
account=(
PyrogramAPI(await pyrogram.login_as_account(config))
if config.telegram.account
else None
),
bots=[PyrogramAPI(bot) for bot in await pyrogram.login_as_bots(config)],
)
else:
tdlib_api = TDLibApi(
account=(
TelethonAPI(await telethon.login_as_account(config))
if config.telegram.account
else None
),
bots=[TelethonAPI(bot) for bot in await telethon.login_as_bots(config)],
)
clients: Clients = {}
for channel_id in config.telegram.private_file_channel:
metadata_cfg = config.tgfs.metadata[channel_id]
clients[metadata_cfg.name] = await Client.create(
channel_id,
metadata_cfg,
tdlib_api,
(
config.telegram.account.used_to_upload
if config.telegram.account
else False
),
)
return clients
async def run_server(app, host: str, port: int, name: str):
"""Run a server with proper configuration"""
logger = logging.getLogger(__name__)
logger.info(f"Starting {name} server on {host}:{port}")
server_config = UvicornConfig(
app,
host=host,
port=port,
loop="none",
log_level="info",
)
server = Server(config=server_config)
await server.serve()
async def main():
config = get_config()
clients = await create_clients(config)
app = create_app(clients, config)
await run_server(app, config.tgfs.server.host, config.tgfs.server.port, "TGFS")
if __name__ == "__main__":
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main())