Spaces:
Sleeping
Sleeping
Update bot.py
Browse files
bot.py
CHANGED
|
@@ -1,67 +1,67 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import telebot
|
| 3 |
-
import requests
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
|
| 6 |
-
# Đọc token từ file .env
|
| 7 |
-
load_dotenv()
|
| 8 |
-
TOKEN = os.getenv('
|
| 9 |
-
|
| 10 |
-
if not TOKEN:
|
| 11 |
-
raise ValueError("❌ Chưa cấu hình TELEGRAM_BOT_TOKEN trong file .env!")
|
| 12 |
-
|
| 13 |
-
bot = telebot.TeleBot(TOKEN)
|
| 14 |
-
|
| 15 |
-
@bot.message_handler(commands=['start'])
|
| 16 |
-
def send_welcome(message):
|
| 17 |
-
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é.")
|
| 18 |
-
|
| 19 |
-
@bot.message_handler(func=lambda message: True)
|
| 20 |
-
def search_and_get_link(message):
|
| 21 |
-
query = message.text
|
| 22 |
-
# API tìm kiếm để lấy slug
|
| 23 |
-
search_url = f"https://ophim1.com/v1/api/tim-kiem?keyword={query}"
|
| 24 |
-
|
| 25 |
-
try:
|
| 26 |
-
search_res = requests.get(search_url).json()
|
| 27 |
-
items = search_res.get('data', {}).get('items', [])
|
| 28 |
-
|
| 29 |
-
if not items:
|
| 30 |
-
bot.reply_to(message, "❌ Không tìm thấy phim này rồi!")
|
| 31 |
-
return
|
| 32 |
-
|
| 33 |
-
# Lấy slug của bộ phim đầu tiên tìm thấy
|
| 34 |
-
movie_slug = items[0]['slug']
|
| 35 |
-
|
| 36 |
-
# Gọi API chi tiết để lấy danh sách tập phim và link
|
| 37 |
-
detail_url = f"https://ophim1.com/phim/{movie_slug}"
|
| 38 |
-
detail_res = requests.get(detail_url).json()
|
| 39 |
-
movie_info = detail_res.get('movie', {})
|
| 40 |
-
episodes = detail_res.get('episodes', [])
|
| 41 |
-
|
| 42 |
-
if episodes:
|
| 43 |
-
# Lấy server đầu tiên và tập mới nhất (thường là phần tử cuối trong server_data)
|
| 44 |
-
server_data = episodes[0].get('server_data', [])
|
| 45 |
-
if server_data:
|
| 46 |
-
latest_ep = server_data[-1]
|
| 47 |
-
watch_link = latest_ep.get('link_embed', '') # Lấy link Embed như trong ảnh bạn gửi
|
| 48 |
-
|
| 49 |
-
response_msg = (
|
| 50 |
-
f"✅ **Đã tìm thấy: {movie_info.get('name')}**\n"
|
| 51 |
-
f"🎭 Tên gốc: {movie_info.get('origin_name')}\n"
|
| 52 |
-
f"📅 Năm: {movie_info.get('year')}\n"
|
| 53 |
-
f"🚩 Trạng thái: {movie_info.get('episode_current')}\n\n"
|
| 54 |
-
f"📺 **[NHẤN VÀO ĐÂY ĐỂ XEM PHIM NGAY]({watch_link})**"
|
| 55 |
-
)
|
| 56 |
-
bot.send_message(message.chat.id, response_msg, parse_mode="Markdown")
|
| 57 |
-
else:
|
| 58 |
-
bot.reply_to(message, "Hic, phim này hiện chưa có link xem.")
|
| 59 |
-
else:
|
| 60 |
-
bot.reply_to(message, "Phim này hiện chưa cập nhật tập nào.")
|
| 61 |
-
|
| 62 |
-
except Exception as e:
|
| 63 |
-
print(f"Lỗi: {e}")
|
| 64 |
-
bot.reply_to(message, "⚠️ Có lỗi khi lấy dữ liệu từ máy chủ phim!")
|
| 65 |
-
|
| 66 |
-
print("Bot xem phim trực tiếp đang chạy...")
|
| 67 |
bot.infinity_polling()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import telebot
|
| 3 |
+
import requests
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Đọc token từ file .env
|
| 7 |
+
load_dotenv()
|
| 8 |
+
TOKEN = os.getenv('8354363462:AAGR1MyBo6mX69DPUdCaemR8-XcOcOoKEmg')
|
| 9 |
+
|
| 10 |
+
if not TOKEN:
|
| 11 |
+
raise ValueError("❌ Chưa cấu hình TELEGRAM_BOT_TOKEN trong file .env!")
|
| 12 |
+
|
| 13 |
+
bot = telebot.TeleBot(TOKEN)
|
| 14 |
+
|
| 15 |
+
@bot.message_handler(commands=['start'])
|
| 16 |
+
def send_welcome(message):
|
| 17 |
+
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é.")
|
| 18 |
+
|
| 19 |
+
@bot.message_handler(func=lambda message: True)
|
| 20 |
+
def search_and_get_link(message):
|
| 21 |
+
query = message.text
|
| 22 |
+
# API tìm kiếm để lấy slug
|
| 23 |
+
search_url = f"https://ophim1.com/v1/api/tim-kiem?keyword={query}"
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
search_res = requests.get(search_url).json()
|
| 27 |
+
items = search_res.get('data', {}).get('items', [])
|
| 28 |
+
|
| 29 |
+
if not items:
|
| 30 |
+
bot.reply_to(message, "❌ Không tìm thấy phim này rồi!")
|
| 31 |
+
return
|
| 32 |
+
|
| 33 |
+
# Lấy slug của bộ phim đầu tiên tìm thấy
|
| 34 |
+
movie_slug = items[0]['slug']
|
| 35 |
+
|
| 36 |
+
# Gọi API chi tiết để lấy danh sách tập phim và link
|
| 37 |
+
detail_url = f"https://ophim1.com/phim/{movie_slug}"
|
| 38 |
+
detail_res = requests.get(detail_url).json()
|
| 39 |
+
movie_info = detail_res.get('movie', {})
|
| 40 |
+
episodes = detail_res.get('episodes', [])
|
| 41 |
+
|
| 42 |
+
if episodes:
|
| 43 |
+
# Lấy server đầu tiên và tập mới nhất (thường là phần tử cuối trong server_data)
|
| 44 |
+
server_data = episodes[0].get('server_data', [])
|
| 45 |
+
if server_data:
|
| 46 |
+
latest_ep = server_data[-1]
|
| 47 |
+
watch_link = latest_ep.get('link_embed', '') # Lấy link Embed như trong ảnh bạn gửi
|
| 48 |
+
|
| 49 |
+
response_msg = (
|
| 50 |
+
f"✅ **Đã tìm thấy: {movie_info.get('name')}**\n"
|
| 51 |
+
f"🎭 Tên gốc: {movie_info.get('origin_name')}\n"
|
| 52 |
+
f"📅 Năm: {movie_info.get('year')}\n"
|
| 53 |
+
f"🚩 Trạng thái: {movie_info.get('episode_current')}\n\n"
|
| 54 |
+
f"📺 **[NHẤN VÀO ĐÂY ĐỂ XEM PHIM NGAY]({watch_link})**"
|
| 55 |
+
)
|
| 56 |
+
bot.send_message(message.chat.id, response_msg, parse_mode="Markdown")
|
| 57 |
+
else:
|
| 58 |
+
bot.reply_to(message, "Hic, phim này hiện chưa có link xem.")
|
| 59 |
+
else:
|
| 60 |
+
bot.reply_to(message, "Phim này hiện chưa cập nhật tập nào.")
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"Lỗi: {e}")
|
| 64 |
+
bot.reply_to(message, "⚠️ Có lỗi khi lấy dữ liệu từ máy chủ phim!")
|
| 65 |
+
|
| 66 |
+
print("Bot xem phim trực tiếp đang chạy...")
|
| 67 |
bot.infinity_polling()
|