6
File size: 3,954 Bytes
fff98cd
5979c4d
fff98cd
90af1e1
5979c4d
fff98cd
 
 
5979c4d
90af1e1
fff98cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90af1e1
fff98cd
 
5979c4d
 
fff98cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90af1e1
fff98cd
90af1e1
fff98cd
5979c4d
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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()