6 / app.py
iq7se2's picture
Update app.py
fff98cd verified
import os, json, requests, threading, queue
import telebot
from telebot import types
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
TACHI_URL = "http://127.0.0.1:4567"
bot = telebot.TeleBot(BOT_TOKEN)
task_queue = queue.Queue()
ARCHIVE_FILE = "archive.json"
archive = json.load(open(ARCHIVE_FILE)) if os.path.exists(ARCHIVE_FILE) else {}
# ================= SEARCH =================
def search_manga(query):
url = f"{TACHI_URL}/api/v1/library/search?q={query}"
r = requests.get(url).json()
return r[:5]
# ================= GET CHAPTERS =================
def get_chapters(manga_id):
url = f"{TACHI_URL}/api/v1/manga/{manga_id}/chapters"
r = requests.get(url).json()
return r
# ================= DOWNLOAD =================
def get_pages(manga_id, chapter_id):
url = f"{TACHI_URL}/api/v1/manga/{manga_id}/chapter/{chapter_id}"
r = requests.get(url).json()
return r.get("pages", [])
# ================= WORKER =================
def worker():
while True:
task = task_queue.get()
chat = task["chat"]
pages = task["pages"]
key = task["key"]
if key in archive:
bot.send_message(chat, "📦 من الأرشيف")
bot.send_document(chat, archive[key])
continue
from PIL import Image
import io
imgs = []
for url in pages:
try:
r = requests.get(url, timeout=10)
img = Image.open(io.BytesIO(r.content)).convert("RGB")
imgs.append(img)
except:
pass
if not imgs:
bot.send_message(chat, "❌ فشل التحميل")
continue
filename = f"{key}.pdf"
imgs[0].save(filename, save_all=True, append_images=imgs[1:])
with open(filename, "rb") as f:
msg = bot.send_document(chat, f)
archive[key] = msg.document.file_id
json.dump(archive, open(ARCHIVE_FILE, "w"))
os.remove(filename)
# ================= HANDLERS =================
@bot.message_handler(commands=['start'])
def start(msg):
bot.send_message(msg.chat.id, "🔥 اكتب اسم مانجا")
@bot.message_handler(func=lambda m: True)
def handle(msg):
text = msg.text
results = search_manga(text)
if not results:
bot.send_message(msg.chat.id, "❌ ماكو نتائج")
return
markup = types.InlineKeyboardMarkup()
for r in results:
name = r.get("title", "Unknown")
mid = r.get("id")
markup.add(
types.InlineKeyboardButton(
name,
callback_data=f"manga|{mid}"
)
)
bot.send_message(msg.chat.id, "📚 اختر:", reply_markup=markup)
@bot.callback_query_handler(func=lambda call: True)
def callback(call):
if call.data.startswith("manga|"):
manga_id = call.data.split("|")[1]
chapters = get_chapters(manga_id)
markup = types.InlineKeyboardMarkup()
for ch in chapters[:20]:
cid = ch.get("id")
title = ch.get("title", "Chapter")
markup.add(
types.InlineKeyboardButton(
title,
callback_data=f"chapter|{manga_id}|{cid}"
)
)
bot.send_message(call.message.chat.id, "📖 اختر فصل:", reply_markup=markup)
elif call.data.startswith("chapter|"):
_, manga_id, chapter_id = call.data.split("|")
pages = get_pages(manga_id, chapter_id)
key = f"{manga_id}_{chapter_id}"
task_queue.put({
"chat": call.message.chat.id,
"pages": pages,
"key": key
})
bot.send_message(call.message.chat.id, "⏳ جاري التحميل")
# ================= RUN =================
if __name__ == "__main__":
threading.Thread(target=worker, daemon=True).start()
bot.infinity_polling()