TienPhat commited on
Commit
417f0eb
·
verified ·
1 Parent(s): b370723

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. app.py +84 -0
  3. bot.py +67 -0
  4. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ WORKDIR /app
8
+
9
+ COPY --chown=user ./requirements.txt requirements.txt
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ COPY --chown=user . /app
13
+
14
+ EXPOSE 7860
15
+
16
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import threading
3
+ import telebot
4
+ import requests
5
+ from http.server import HTTPServer, BaseHTTPRequestHandler
6
+
7
+ # --- Bot Setup ---
8
+ TOKEN = os.environ.get('TELEGRAM_BOT_TOKEN')
9
+ if not TOKEN:
10
+ raise ValueError("❌ Chưa cấu hình TELEGRAM_BOT_TOKEN!")
11
+
12
+ bot = telebot.TeleBot(TOKEN)
13
+
14
+ # --- Bot Handlers ---
15
+
16
+ @bot.message_handler(commands=['start'])
17
+ def send_welcome(message):
18
+ 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é.")
19
+
20
+ @bot.message_handler(func=lambda message: True)
21
+ def search_and_get_link(message):
22
+ query = message.text
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
+ movie_slug = items[0]['slug']
34
+
35
+ detail_url = f"https://ophim1.com/phim/{movie_slug}"
36
+ detail_res = requests.get(detail_url).json()
37
+ movie_info = detail_res.get('movie', {})
38
+ episodes = detail_res.get('episodes', [])
39
+
40
+ if episodes:
41
+ server_data = episodes[0].get('server_data', [])
42
+ if server_data:
43
+ latest_ep = server_data[-1]
44
+ watch_link = latest_ep.get('link_embed', '')
45
+
46
+ response_msg = (
47
+ f"✅ **Đã tìm thấy: {movie_info.get('name')}**\n"
48
+ f"🎭 Tên gốc: {movie_info.get('origin_name')}\n"
49
+ f"📅 Năm: {movie_info.get('year')}\n"
50
+ f"🚩 Trạng thái: {movie_info.get('episode_current')}\n\n"
51
+ f"📺 **[NHẤN VÀO ĐÂY ĐỂ XEM PHIM NGAY]({watch_link})**"
52
+ )
53
+ bot.send_message(message.chat.id, response_msg, parse_mode="Markdown")
54
+ else:
55
+ bot.reply_to(message, "Hic, phim này hiện chưa có link xem.")
56
+ else:
57
+ bot.reply_to(message, "Phim này hiện chưa cập nhật tập nào.")
58
+
59
+ except Exception as e:
60
+ print(f"Lỗi: {e}")
61
+ bot.reply_to(message, "⚠️ Có lỗi khi lấy dữ liệu từ máy chủ phim!")
62
+
63
+ # --- Run bot in background thread ---
64
+ def run_bot():
65
+ print("🤖 Bot Telegram đang chạy...")
66
+ bot.infinity_polling()
67
+
68
+ bot_thread = threading.Thread(target=run_bot, daemon=True)
69
+ bot_thread.start()
70
+
71
+ # --- Simple web server for HF Spaces health check (port 7860) ---
72
+ class HealthHandler(BaseHTTPRequestHandler):
73
+ def do_GET(self):
74
+ self.send_response(200)
75
+ self.send_header('Content-type', 'text/html; charset=utf-8')
76
+ self.end_headers()
77
+ self.wfile.write('🎬 Bot Telegram Xem Phim đang hoạt động!'.encode('utf-8'))
78
+
79
+ def log_message(self, format, *args):
80
+ pass # Tắt log HTTP
81
+
82
+ print("🌐 Web server chạy trên port 7860...")
83
+ server = HTTPServer(('0.0.0.0', 7860), HealthHandler)
84
+ server.serve_forever()
bot.py ADDED
@@ -0,0 +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('TELEGRAM_BOT_TOKEN')
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()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ pyTelegramBotAPI>=4.14.0
2
+ requests>=2.31.0
3
+ python-dotenv>=1.0.0
4
+ flask>=3.0.0