-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
99 lines (78 loc) · 2.7 KB
/
run.py
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
90
91
92
93
94
95
96
97
98
99
import asyncio
import traceback
import argparse
import configparser
import logging
import signal
import discord
import prometheus_client
import os
from core.Core import Core
from downloaders.FileDownloader import FileDownloader
from downloaders.HtmlDownloader import HtmlDownloader
from downloaders.LinkDownloader import LinkDownloader
from downloaders.YoutubeDownloader import YoutubeDownloader
from VLC.VLCRadioEmitter import VLCStreamer
from downloaders.MasterDownloader import MasterDownloader
from telegram.TelegramFrontend import TgFrontend
from discord_.DiscordComponent import DiscordComponent
from web.server import StatusWebServer
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--config-file", type=str, default="config.ini")
args = parser.parse_args()
config = configparser.ConfigParser()
config.read(args.config_file)
for name in os.environ:
if name[:3] != "DJ_":
continue
value = os.environ[name]
name = name[3:]
section = None
for s in config.sections() + [config.default_section]:
if s == name[:len(s)]:
section = s
if section is None:
continue
key = name[len(section) + 1:]
config.set(section, key, value)
# Reload config on sighup signal
def hup_handler(_signum, _frame):
logging.info("Caught sighup signal. Reloading configuration...")
config.read(args.config_file)
logging.info("Config reloaded")
signal.signal(signal.SIGHUP, hup_handler)
# Setup logging
logging.basicConfig(format='%(asctime)s %(levelname)s [%(name)s - %(funcName)s]: %(message)s')
logger = logging.getLogger('tg_dj')
logger.setLevel(getattr(logging, config.get(config.default_section, "verbosity", fallback="warning").upper()))
# Start modules
main_loop = asyncio.get_event_loop()
downloader = MasterDownloader(config, [
YoutubeDownloader(config),
FileDownloader(config),
HtmlDownloader(config),
LinkDownloader(config),
])
# modules = [DiscordComponent(config, discord.Client(loop=main_loop)), TgFrontend(config)]
modules = [DiscordComponent(config, discord.Client(loop=main_loop)), StatusWebServer(config)]
# modules = [VLCStreamer(config), TgFrontend(config)]
core = Core(config, components=modules, downloader=downloader, loop=main_loop)
# Start prometheus server
prometheus_client.start_http_server(8910)
# Run event loop
try:
main_loop.run_forever()
except (KeyboardInterrupt, SystemExit):
pass
logger.info("Main event loop has ended")
logger.debug("Cleaning...")
try:
core.cleanup()
except AttributeError:
traceback.print_exc()
logger.info("Closing loop...")
main_loop.run_until_complete(asyncio.sleep(1))
main_loop.stop()
main_loop.close()
logger.debug("Exit")