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) |