Прощай, мерзкий config_python.py
This commit is contained in:
36
src/parse_config.py
Normal file
36
src/parse_config.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from dataclasses import dataclass, asdict
|
||||
import configparser
|
||||
|
||||
@dataclass(slots=True, frozen=True)
|
||||
class Config:
|
||||
streamer: str
|
||||
app_id: str
|
||||
app_secret: str
|
||||
target_tg_chat: str
|
||||
tg_token: str
|
||||
check_period: int
|
||||
cleanup_msg: bool
|
||||
|
||||
def load_config(config_path: str = "twitch-tgbot.cfg") -> Config:
|
||||
"""
|
||||
Эта функция либо читает существующий конфиг, либо создает новый.
|
||||
Возвращает объект конфига (configparser.ConfigParser())
|
||||
"""
|
||||
config_file = configparser.ConfigParser()
|
||||
# Читаем конфиг, если пустой - заполняем
|
||||
if not config_file.read(config_path) or not config_file.has_section('twitch'):
|
||||
config_file.add_section('twitch')
|
||||
twitch = config_file['twitch']
|
||||
config = Config(
|
||||
streamer=twitch.get('streamer',fallback='the_viox'),
|
||||
app_id=twitch.get('app_id',fallback=''),
|
||||
app_secret=twitch.get('app_secret',fallback=''),
|
||||
target_tg_chat=twitch.get('target_tg_chat',fallback=''),
|
||||
tg_token=twitch.get('tg_token',fallback=''),
|
||||
check_period=twitch.getint('check_period',fallback=10),
|
||||
cleanup_msg=twitch.getboolean('cleanup_msg',fallback=True)
|
||||
)
|
||||
with open(config_path, 'w') as cfg_file:
|
||||
config_file.read_dict({'twitch': asdict(config)})
|
||||
config_file.write(cfg_file)
|
||||
return config
|
||||
@@ -1,12 +1,9 @@
|
||||
#!/usr/bin/env python3.10
|
||||
#!/usr/bin/env python3
|
||||
import time
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
|
||||
sys.path.append(os.path.abspath(os.path.curdir))
|
||||
import config_python
|
||||
|
||||
from twitchAPI.twitch import Twitch
|
||||
from logging.handlers import TimedRotatingFileHandler
|
||||
from termcolor import colored
|
||||
@@ -17,9 +14,7 @@ from datetime import datetime
|
||||
import requests
|
||||
import shutil
|
||||
|
||||
streamer = config_python.streamer
|
||||
app_id = config_python.appid
|
||||
app_secret = config_python.appsecret
|
||||
from parse_config import Config, load_config
|
||||
|
||||
log_format = logging.Formatter('%(asctime)s %(levelname)s:%(message)s')
|
||||
log_file = 'output.log'
|
||||
@@ -92,7 +87,7 @@ def stream_message_worker(data: dict):
|
||||
|
||||
# Если стрим не идет
|
||||
def no_stream_msg_worker():
|
||||
log.info(streamer + " Не стримит")
|
||||
log.info(config_python.streamer + " Не стримит")
|
||||
# Проверяем наличие файла
|
||||
if os.path.exists('msgid'):
|
||||
# Если файл существует - читаем
|
||||
@@ -121,11 +116,11 @@ def check_alive():
|
||||
"""
|
||||
|
||||
# Получаем инфо о стримере, если не получается, выходим с ошибкой
|
||||
resolved_id = twitch_client.get_users(logins=[streamer])
|
||||
resolved_id = twitch_client.get_users(logins=[config_python.streamer])
|
||||
if not resolved_id['data']:
|
||||
log.error(
|
||||
colored(
|
||||
"Аккаунт " + streamer + " не найден",
|
||||
"Аккаунт " + config_python.streamer + " не найден",
|
||||
'red',
|
||||
)
|
||||
)
|
||||
@@ -165,7 +160,8 @@ def get_logger(logger_name):
|
||||
if __name__ == '__main__':
|
||||
log = get_logger("main")
|
||||
log.info("Запущен")
|
||||
twitch_client = Twitch(app_id, app_secret)
|
||||
config_python = load_config()
|
||||
twitch_client = Twitch(config_python.app_id, config_python.app_secret)
|
||||
bot = telebot.TeleBot(config_python.tgtoken)
|
||||
|
||||
while True:
|
||||
|
||||
Reference in New Issue
Block a user