Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,147 @@
|
|
| 1 |
-
import os,
|
| 2 |
import telebot
|
| 3 |
-
from
|
| 4 |
|
| 5 |
-
# --- الإعدادات ---
|
| 6 |
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
bot = telebot.TeleBot(BOT_TOKEN)
|
| 9 |
-
app = Flask(__name__)
|
| 10 |
-
|
| 11 |
-
# --- وظائف التفاعل مع Tachidesk ---
|
| 12 |
-
|
| 13 |
-
def get_manga_id(query):
|
| 14 |
-
"""البحث عن المانجا والحصول على الـ ID الخاص بها"""
|
| 15 |
-
params = {"query": query}
|
| 16 |
-
res = requests.get(f"{TACHI_URL}/search", params=params)
|
| 17 |
-
results = res.json()
|
| 18 |
-
return results[0]['id'] if results else None
|
| 19 |
-
|
| 20 |
-
def get_chapter_pages(manga_id, chapter_num):
|
| 21 |
-
"""جلب روابط الصور للفصل المحدد"""
|
| 22 |
-
# 1. جلب قائمة الفصول
|
| 23 |
-
chapters = requests.get(f"{TACH_URL}/manga/{manga_id}/chapters").json()
|
| 24 |
-
|
| 25 |
-
# 2. البحث عن الفصل المطلوب (رقمياً)
|
| 26 |
-
chapter_id = None
|
| 27 |
-
for ch in chapters:
|
| 28 |
-
if str(chapter_num) in str(ch['chapterNumber']):
|
| 29 |
-
chapter_id = ch['id']
|
| 30 |
-
break
|
| 31 |
-
|
| 32 |
-
if not chapter_id: return None
|
| 33 |
-
|
| 34 |
-
# 3. جلب الصور
|
| 35 |
-
pages = requests.get(f"{TACHI_URL}/chapter/{chapter_id}/pages").json()
|
| 36 |
-
return [p['url'] for p in pages]
|
| 37 |
-
|
| 38 |
-
# --- أوامر البوت ---
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
@bot.message_handler(commands=['start'])
|
| 41 |
-
def
|
| 42 |
-
bot.
|
| 43 |
|
| 44 |
@bot.message_handler(func=lambda m: True)
|
| 45 |
-
def
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
if
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
|
|
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
threading.Thread(target=
|
| 66 |
bot.infinity_polling()
|
|
|
|
| 1 |
+
import os, json, requests, threading, queue
|
| 2 |
import telebot
|
| 3 |
+
from telebot import types
|
| 4 |
|
|
|
|
| 5 |
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 6 |
+
|
| 7 |
+
TACHI_URL = "http://127.0.0.1:4567"
|
| 8 |
+
|
| 9 |
bot = telebot.TeleBot(BOT_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
task_queue = queue.Queue()
|
| 12 |
+
|
| 13 |
+
ARCHIVE_FILE = "archive.json"
|
| 14 |
+
archive = json.load(open(ARCHIVE_FILE)) if os.path.exists(ARCHIVE_FILE) else {}
|
| 15 |
+
|
| 16 |
+
# ================= SEARCH =================
|
| 17 |
+
def search_manga(query):
|
| 18 |
+
url = f"{TACHI_URL}/api/v1/library/search?q={query}"
|
| 19 |
+
r = requests.get(url).json()
|
| 20 |
+
return r[:5]
|
| 21 |
+
|
| 22 |
+
# ================= GET CHAPTERS =================
|
| 23 |
+
def get_chapters(manga_id):
|
| 24 |
+
url = f"{TACHI_URL}/api/v1/manga/{manga_id}/chapters"
|
| 25 |
+
r = requests.get(url).json()
|
| 26 |
+
return r
|
| 27 |
+
|
| 28 |
+
# ================= DOWNLOAD =================
|
| 29 |
+
def get_pages(manga_id, chapter_id):
|
| 30 |
+
url = f"{TACHI_URL}/api/v1/manga/{manga_id}/chapter/{chapter_id}"
|
| 31 |
+
r = requests.get(url).json()
|
| 32 |
+
return r.get("pages", [])
|
| 33 |
+
|
| 34 |
+
# ================= WORKER =================
|
| 35 |
+
def worker():
|
| 36 |
+
while True:
|
| 37 |
+
task = task_queue.get()
|
| 38 |
+
|
| 39 |
+
chat = task["chat"]
|
| 40 |
+
pages = task["pages"]
|
| 41 |
+
key = task["key"]
|
| 42 |
+
|
| 43 |
+
if key in archive:
|
| 44 |
+
bot.send_message(chat, "📦 من الأرشيف")
|
| 45 |
+
bot.send_document(chat, archive[key])
|
| 46 |
+
continue
|
| 47 |
+
|
| 48 |
+
from PIL import Image
|
| 49 |
+
import io
|
| 50 |
+
|
| 51 |
+
imgs = []
|
| 52 |
+
|
| 53 |
+
for url in pages:
|
| 54 |
+
try:
|
| 55 |
+
r = requests.get(url, timeout=10)
|
| 56 |
+
img = Image.open(io.BytesIO(r.content)).convert("RGB")
|
| 57 |
+
imgs.append(img)
|
| 58 |
+
except:
|
| 59 |
+
pass
|
| 60 |
+
|
| 61 |
+
if not imgs:
|
| 62 |
+
bot.send_message(chat, "❌ فشل التحميل")
|
| 63 |
+
continue
|
| 64 |
+
|
| 65 |
+
filename = f"{key}.pdf"
|
| 66 |
+
|
| 67 |
+
imgs[0].save(filename, save_all=True, append_images=imgs[1:])
|
| 68 |
+
|
| 69 |
+
with open(filename, "rb") as f:
|
| 70 |
+
msg = bot.send_document(chat, f)
|
| 71 |
+
archive[key] = msg.document.file_id
|
| 72 |
+
|
| 73 |
+
json.dump(archive, open(ARCHIVE_FILE, "w"))
|
| 74 |
+
|
| 75 |
+
os.remove(filename)
|
| 76 |
+
|
| 77 |
+
# ================= HANDLERS =================
|
| 78 |
@bot.message_handler(commands=['start'])
|
| 79 |
+
def start(msg):
|
| 80 |
+
bot.send_message(msg.chat.id, "🔥 اكتب اسم مانجا")
|
| 81 |
|
| 82 |
@bot.message_handler(func=lambda m: True)
|
| 83 |
+
def handle(msg):
|
| 84 |
+
text = msg.text
|
| 85 |
+
|
| 86 |
+
results = search_manga(text)
|
| 87 |
+
|
| 88 |
+
if not results:
|
| 89 |
+
bot.send_message(msg.chat.id, "❌ ماكو نتائج")
|
| 90 |
+
return
|
| 91 |
+
|
| 92 |
+
markup = types.InlineKeyboardMarkup()
|
| 93 |
+
|
| 94 |
+
for r in results:
|
| 95 |
+
name = r.get("title", "Unknown")
|
| 96 |
+
mid = r.get("id")
|
| 97 |
+
|
| 98 |
+
markup.add(
|
| 99 |
+
types.InlineKeyboardButton(
|
| 100 |
+
name,
|
| 101 |
+
callback_data=f"manga|{mid}"
|
| 102 |
+
)
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
bot.send_message(msg.chat.id, "📚 اختر:", reply_markup=markup)
|
| 106 |
+
|
| 107 |
+
@bot.callback_query_handler(func=lambda call: True)
|
| 108 |
+
def callback(call):
|
| 109 |
+
if call.data.startswith("manga|"):
|
| 110 |
+
manga_id = call.data.split("|")[1]
|
| 111 |
+
|
| 112 |
+
chapters = get_chapters(manga_id)
|
| 113 |
+
|
| 114 |
+
markup = types.InlineKeyboardMarkup()
|
| 115 |
+
|
| 116 |
+
for ch in chapters[:20]:
|
| 117 |
+
cid = ch.get("id")
|
| 118 |
+
title = ch.get("title", "Chapter")
|
| 119 |
+
|
| 120 |
+
markup.add(
|
| 121 |
+
types.InlineKeyboardButton(
|
| 122 |
+
title,
|
| 123 |
+
callback_data=f"chapter|{manga_id}|{cid}"
|
| 124 |
+
)
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
bot.send_message(call.message.chat.id, "📖 اختر فصل:", reply_markup=markup)
|
| 128 |
+
|
| 129 |
+
elif call.data.startswith("chapter|"):
|
| 130 |
+
_, manga_id, chapter_id = call.data.split("|")
|
| 131 |
+
|
| 132 |
+
pages = get_pages(manga_id, chapter_id)
|
| 133 |
+
|
| 134 |
+
key = f"{manga_id}_{chapter_id}"
|
| 135 |
+
|
| 136 |
+
task_queue.put({
|
| 137 |
+
"chat": call.message.chat.id,
|
| 138 |
+
"pages": pages,
|
| 139 |
+
"key": key
|
| 140 |
+
})
|
| 141 |
+
|
| 142 |
+
bot.send_message(call.message.chat.id, "⏳ جاري التحميل")
|
| 143 |
|
| 144 |
+
# ================= RUN =================
|
| 145 |
if __name__ == "__main__":
|
| 146 |
+
threading.Thread(target=worker, daemon=True).start()
|
| 147 |
bot.infinity_polling()
|