Spaces:
Sleeping
Sleeping
File size: 2,686 Bytes
eb3cd52 ca3a389 eb3cd52 417f0eb | 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 | import os
import telebot
import requests
from dotenv import load_dotenv
# Đọc token từ file .env
load_dotenv()
TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
if not TOKEN:
raise ValueError("❌ Chưa cấu hình TELEGRAM_BOT_TOKEN trong file .env!")
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, "🎬 Chào bạn! Nhập tên phim muốn xem, mình sẽ lấy link trực tiếp cho nhé.")
@bot.message_handler(func=lambda message: True)
def search_and_get_link(message):
query = message.text
# API tìm kiếm để lấy slug
search_url = f"https://ophim1.com/v1/api/tim-kiem?keyword={query}"
try:
search_res = requests.get(search_url).json()
items = search_res.get('data', {}).get('items', [])
if not items:
bot.reply_to(message, "❌ Không tìm thấy phim này rồi!")
return
# Lấy slug của bộ phim đầu tiên tìm thấy
movie_slug = items[0]['slug']
# Gọi API chi tiết để lấy danh sách tập phim và link
detail_url = f"https://ophim1.com/phim/{movie_slug}"
detail_res = requests.get(detail_url).json()
movie_info = detail_res.get('movie', {})
episodes = detail_res.get('episodes', [])
if episodes:
# Lấy server đầu tiên và tập mới nhất (thường là phần tử cuối trong server_data)
server_data = episodes[0].get('server_data', [])
if server_data:
latest_ep = server_data[-1]
watch_link = latest_ep.get('link_embed', '') # Lấy link Embed như trong ảnh bạn gửi
response_msg = (
f"✅ **Đã tìm thấy: {movie_info.get('name')}**\n"
f"🎭 Tên gốc: {movie_info.get('origin_name')}\n"
f"📅 Năm: {movie_info.get('year')}\n"
f"🚩 Trạng thái: {movie_info.get('episode_current')}\n\n"
f"📺 **[NHẤN VÀO ĐÂY ĐỂ XEM PHIM NGAY]({watch_link})**"
)
bot.send_message(message.chat.id, response_msg, parse_mode="Markdown")
else:
bot.reply_to(message, "Hic, phim này hiện chưa có link xem.")
else:
bot.reply_to(message, "Phim này hiện chưa cập nhật tập nào.")
except Exception as e:
print(f"Lỗi: {e}")
bot.reply_to(message, "⚠️ Có lỗi khi lấy dữ liệu từ máy chủ phim!")
print("Bot xem phim trực tiếp đang chạy...")
bot.infinity_polling() |