Spaces:
iq7se2
/
7
Paused

7
File size: 6,502 Bytes
182e06f
 
9db3a84
90af1e1
182e06f
 
90af1e1
182e06f
9db3a84
182e06f
90af1e1
9db3a84
182e06f
 
9db3a84
182e06f
90af1e1
 
182e06f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90af1e1
9db3a84
90af1e1
9db3a84
 
182e06f
90af1e1
 
182e06f
9db3a84
182e06f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90af1e1
182e06f
 
 
 
90af1e1
182e06f
90af1e1
182e06f
90af1e1
182e06f
 
90af1e1
182e06f
90af1e1
182e06f
 
 
90af1e1
182e06f
 
 
90af1e1
182e06f
90af1e1
182e06f
 
90af1e1
182e06f
 
 
90af1e1
182e06f
 
90af1e1
182e06f
90af1e1
182e06f
 
 
90af1e1
182e06f
 
 
 
9db3a84
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os, time, threading, zipfile, io, re, json, hashlib, gc
from flask import Flask
import telebot
from telebot import types
from playwright.sync_api import sync_playwright
from PIL import Image

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# โš™๏ธ ุงู„ุฅุนุฏุงุฏุงุช
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
ARCHIVE_CHANNEL = os.getenv("ARCHIVE_CHANNEL_ID")  # ู‚ู†ุงุฉ ุฎุงุตุฉ
ADMIN_HANDLE = "@svipfast"

bot = telebot.TeleBot(BOT_TOKEN, threaded=False)
app = Flask(__name__)

archive_index = {}
archive_cache = {}
processing_now = set()

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# ๐Ÿ”‘ ู…ูุชุงุญ ุฐูƒูŠ
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def make_key(url, chapter):
    raw = f"{url}|{chapter}"
    return hashlib.md5(raw.encode()).hexdigest()

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# ๐Ÿ“ฆ ุชุญู…ูŠู„/ุญูุธ ุงู„ุฃุฑุดูŠู
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

def load_archive():
    global archive_index, archive_cache
    try:
        chat = bot.get_chat(ARCHIVE_CHANNEL)
        if chat.pinned_message:
            archive_index = json.loads(chat.pinned_message.text)
            archive_cache = archive_index.copy()
            print(f"โœ… Loaded archive: {len(archive_index)}")
    except:
        print("โ„น๏ธ New archive will be created")

def save_archive():
    try:
        text = json.dumps(archive_index)
        chat = bot.get_chat(ARCHIVE_CHANNEL)
        if chat.pinned_message:
            bot.edit_message_text(text, ARCHIVE_CHANNEL, chat.pinned_message.message_id)
        else:
            msg = bot.send_message(ARCHIVE_CHANNEL, text)
            bot.pin_chat_message(ARCHIVE_CHANNEL, msg.message_id)
    except Exception as e:
        print("โŒ Save error:", e)

def add_to_archive(key, file_path, caption):
    try:
        with open(file_path, 'rb') as f:
            msg = bot.send_document(ARCHIVE_CHANNEL, f, caption=caption)
            archive_index[key] = msg.message_id
            archive_cache[key] = msg.message_id
            save_archive()
            return msg.message_id
    except:
        return None

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# ๐ŸŒ Playwright (ู…ุฑุฉ ูˆุงุญุฏุฉ)
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=True)

def fetch_images(url):
    context = browser.new_context()
    page = context.new_page()
    try:
        page.goto(url, wait_until="networkidle", timeout=60000)
        for _ in range(5):
            page.mouse.wheel(0, 1200)
            time.sleep(0.5)

        images = page.query_selector_all("img")
        img_list = []

        for img in images:
            src = img.get_attribute("src") or img.get_attribute("data-src")
            if src and any(ext in src.lower() for ext in ['.jpg','.png','.webp']):
                try:
                    res = page.request.get(src)
                    if res.status == 200 and len(res.body()) > 20000:
                        img_list.append(Image.open(io.BytesIO(res.body())).convert('RGB'))
                except:
                    continue

        context.close()
        return img_list
    except:
        context.close()
        return []

def safe_fetch(url):
    for _ in range(3):
        imgs = fetch_images(url)
        if imgs:
            return imgs
    return []

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# ๐Ÿค– ุงู„ุจูˆุช
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

user_states = {}

@app.route('/')
def home():
    return "โœ… Bot Running"

@bot.message_handler(commands=['start'])
def start(msg):
    markup = types.InlineKeyboardMarkup()
    markup.add(types.InlineKeyboardButton("๐Ÿ“ฅ ุฅุฑุณุงู„ ุฑุงุจุท", callback_data="new"))
    bot.send_message(msg.chat.id, "๐Ÿš€ ุฃุฑุณู„ ุฑุงุจุท ุงู„ูุตู„", reply_markup=markup)

@bot.callback_query_handler(func=lambda c: True)
def handle(c):
    if c.data == "new":
        m = bot.send_message(c.message.chat.id, "๐Ÿ”— ุฃุฑุณู„ ุงู„ุฑุงุจุท")
        bot.register_next_step_handler(m, get_url)

def get_url(msg):
    user_states[msg.chat.id] = {"url": msg.text}
    m = bot.send_message(msg.chat.id, "๐Ÿ”ข ุงูƒุชุจ ุฑู‚ู… ุงู„ูุตู„")
    bot.register_next_step_handler(m, process)

def process(msg):
    chat_id = msg.chat.id
    url = user_states[chat_id]["url"]
    chapter = int(msg.text)

    key = make_key(url, chapter)

    # ๐Ÿง  cache
    if key in archive_cache:
        bot.send_message(chat_id, "โšก ู…ู† ุงู„ุฃุฑุดูŠู")
        bot.forward_message(chat_id, ARCHIVE_CHANNEL, archive_cache[key])
        return

    # ๐Ÿ”’ ู…ู†ุน ุชูƒุฑุงุฑ
    if key in processing_now:
        bot.send_message(chat_id, "โณ ูŠุชู… ู…ุนุงู„ุฌุชู‡...")
        return

    processing_now.add(key)

    bot.send_message(chat_id, "๐Ÿ”„ ุฌุงุฑูŠ ุงู„ุณุญุจ...")

    base = re.sub(r'/\d+$', '', url)
    target = f"{base}/{chapter}"

    imgs = safe_fetch(target)

    if imgs:
        pdf = f"ch_{chapter}.pdf"
        imgs[0].save(pdf, save_all=True, append_images=imgs[1:])

        zip_name = f"ch_{chapter}.zip"
        with zipfile.ZipFile(zip_name, 'w', compression=zipfile.ZIP_DEFLATED) as z:
            z.write(pdf)

        msg_id = add_to_archive(key, zip_name, f"Chapter {chapter}")

        if msg_id:
            bot.forward_message(chat_id, ARCHIVE_CHANNEL, msg_id)

        os.remove(pdf)
        os.remove(zip_name)
        gc.collect()

    else:
        bot.send_message(chat_id, "โŒ ูุดู„ ุงู„ุณุญุจ")

    processing_now.remove(key)

# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# ๐Ÿš€ ุงู„ุชุดุบูŠู„
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

if __name__ == "__main__":
    os.system("playwright install chromium")
    load_archive()
    threading.Thread(target=lambda: bot.infinity_polling(), daemon=True).start()
    app.run(host="0.0.0.0", port=7860)