refactor: move imports in main.py

this change allows to avoid multiple and very specific imports in other python files

Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
Macintxsh 2025-03-26 14:28:15 +03:00
parent d363fa9afd
commit 4fbdb198ae
Signed by: mctaylors
GPG key ID: 4EEF4F949A266EE2
3 changed files with 15 additions and 17 deletions

View file

@ -1,9 +1,11 @@
import html_parser
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.constants import ParseMode
from telegram.ext import ContextTypes
import html_parser
import main
from main import config
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text(
@ -26,8 +28,6 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
async def about_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
import main
from main import config
reply_markup = None
source_url = config.get('General', 'SourceUrl')
if source_url.startswith('http://') or source_url.startswith('https://'):

View file

@ -1,4 +1,3 @@
from configparser import ConfigParser
from uuid import uuid4
import requests
@ -7,7 +6,9 @@ from telegram import Update, InlineKeyboardButton, InlineQueryResultArticle, Inp
from telegram.constants import ParseMode
from telegram.ext import ContextTypes
import main
from extensions import humanize_tags_from_json, format_rating
from main import config
# noinspection PyUnusedLocal
@ -20,7 +21,6 @@ async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
if not query.isdigit():
return
from main import config
response = requests.get(f"http://{config.get('App', 'Host')}/posts/{query}.json")
if response.status_code != 200:
@ -29,12 +29,10 @@ async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
data = response.json()
await answer_query(update, query, config, data)
await answer_query(update, query, data)
async def answer_query(update: Update, query: str,
config: ConfigParser, data) -> None:
import main
async def answer_query(update: Update, query: str, data) -> None:
characters = humanize_tags_from_json(data['tag_string_character'], "no characters")
copyrights = humanize_tags_from_json(data['tag_string_copyright'], "unknown copyright")
artists = humanize_tags_from_json(data['tag_string_artist'], "unknown artist")

14
main.py
View file

@ -5,9 +5,6 @@ import os
import requests
from telegram.ext import Application, CommandHandler, InlineQueryHandler
from commands import *
from inline_query import inline_query
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
@ -22,17 +19,20 @@ config.read("config.ini")
hostname = config.get('App', 'HostName')
hostname = hostname if hostname != "" else config.get('App', 'Host')
def main() -> None:
validate_config()
application = Application.builder().token(get_token()).build()
application.add_handler(CommandHandler("start", start_command))
application.add_handler(CommandHandler("help", help_command))
application.add_handler(CommandHandler("about", about_command))
import commands
application.add_handler(CommandHandler("start", commands.start_command))
application.add_handler(CommandHandler("help", commands.help_command))
application.add_handler(CommandHandler("about", commands.about_command))
from inline_query import inline_query
application.add_handler(InlineQueryHandler(inline_query))
application.run_polling(allowed_updates=Update.ALL_TYPES)
application.run_polling(allowed_updates=commands.Update.ALL_TYPES)
def validate_config() -> None: