Ryanus commited on
Commit
df00eeb
·
verified ·
1 Parent(s): cb0b509

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -69
app.py CHANGED
@@ -1,78 +1,71 @@
1
- import os
2
- import asyncio
3
- from pyrogram import Client, filters
4
- from pyrogram.types import Message
 
 
 
5
 
6
- # Hugging Face Spaces 的環境變數中讀取憑證
7
- # 這些變數需要在 Space Settings -> Variables 中設定
8
- api_id = int(os.getenv("API_ID"))
9
- api_hash = os.getenv("API_HASH")
10
- bot_token = os.getenv("BOT_TOKEN")
11
-
12
- # 初始化 Pyrogram 客戶端
13
- app = Client(
14
- "traffic_bot",
15
- api_id=api_id,
16
- api_hash=api_hash,
17
- bot_token=bot_token
18
- )
19
-
20
- # 定義 /start 指令的處理器
21
- @app.on_message(filters.command("start"))
22
- async def start_command(client: Client, message: Message):
23
  """
24
- 當用戶發送 /start 時,回應歡迎訊息和使用說明。
 
 
 
 
 
25
  """
26
- welcome_text = (
27
- "👋 歡迎使用流量機器人!\n\n"
28
- "這個機器人可以幫你產生看起來很真實的流量。\n\n"
29
- "請使用以下指令:\n"
30
- "• `/visit <網址>` - 開始訪問指定的網址\n"
31
- "• `/help` - 查看幫助訊息"
32
- )
33
- await message.reply_text(welcome_text)
34
 
35
- # 定義 /visit 指令的處理器
36
- @app.on_message(filters.command("visit"))
37
- async def visit_command(client: Client, message: Message):
38
- """
39
- 當用戶發送 /visit <網址> 時,模擬訪問該網址。
40
- """
41
- # 檢查是否提供了網址
42
- if len(message.command) < 2:
43
- await message.reply_text("❌ 請提供一個網址。\n範例: `/visit https://www.google.com`")
44
- return
45
 
46
- url = message.command[1]
 
 
 
 
 
47
 
48
- # 發送一個「正在處理」的訊息
49
- processing_message = await message.reply_text(f"🚀 正在準備訪問 {url}...")
 
 
 
 
 
 
 
 
 
 
50
 
51
- # 模擬一些處理時間
52
- await asyncio.sleep(3)
53
-
54
- # 更新訊息,表示已完成
55
- await processing_message.edit_text(f"✅ 已成功模擬訪問 {url}!\n\n💡 這只是一個演示,實際的流量生成需要更複雜的邏輯。")
56
-
57
- # 定義 /help 指令的處理器
58
- @app.on_message(filters.command("help"))
59
- async def help_command(client: Client, message: Message):
60
- """
61
- 提供幫助訊息。
62
- """
63
- help_text = (
64
- "📖 **幫助說明**\n\n"
65
- "這是一個基於 Pyrogram 的 Telegram 機器人範例,部署在 Hugging Face Spaces。\n\n"
66
- "**可用指令:**\n"
67
- "• `/start` - 開始使用機器人\n"
68
- "• `/visit <網址>` - 模擬訪問一個網址\n"
69
- "• `/help` - 顯示此幫助訊息\n\n"
70
- "**如何部署自己的機器人?**\n"
71
- "你可以 fork 這個專案,並在 Hugging Face Spaces 中設定你自己的 `API_ID`, `API_HASH`, 和 `BOT_TOKEN`。"
72
  )
73
- await message.reply_text(help_text)
74
 
75
- # 啟動機器人
76
- print("機器人正在啟動...")
77
- app.run()
78
- print("機器人已停止。")
 
1
+ import gradio as gr
2
+ import time
3
+ import random
4
+ from selenium import webdriver
5
+ from selenium.webdriver.firefox.service import Service
6
+ from selenium.webdriver.firefox.options import Options
7
+ from webdriver_manager.firefox import GeckoDriverManager
8
 
9
+ # --- 核心流量生成逻辑 ---
10
+ def generate_traffic(url, views, stay_duration_min, stay_duration_max):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  """
12
+ 使用 Selenium 无头浏览器模拟访问指定 URL
13
+ :param url: 目标网址
14
+ :param views: 访问次数
15
+ :param stay_duration_min: 每次访问停留最小时间(秒)
16
+ :param stay_duration_max: 每次访问停留最大时间(秒)
17
+ :return: 生成日志
18
  """
19
+ if not url.startswith('http'):
20
+ return "错误:请输入有效的网址(以 http:// 或 https:// 开头)。"
 
 
 
 
 
 
21
 
22
+ # 配置 Firefox 无头模式
23
+ options = Options()
24
+ options.add_argument("--headless") # 必须使用无头模式
25
+ options.add_argument("--disable-gpu")
26
+ options.add_argument("--no-sandbox")
27
+ options.add_argument("--disable-dev-shm-usage")
 
 
 
 
28
 
29
+ # 使用 webdriver-manager 自动安装和管理 GeckoDriver
30
+ try:
31
+ service = Service(GeckoDriverManager().install())
32
+ driver = webdriver.Firefox(service=service, options=options)
33
+ except Exception as e:
34
+ return f"初始化浏览器失败: {e}"
35
 
36
+ logs = []
37
+ for i in range(views):
38
+ try:
39
+ driver.get(url)
40
+ stay_time = random.randint(stay_duration_min, stay_duration_max)
41
+ logs.append(f"✅ 第 {i+1}/{views} 次访问成功,停留 {stay_time} 秒。")
42
+ time.sleep(stay_time)
43
+ except Exception as e:
44
+ logs.append(f"❌ 第 {i+1} 次访问失败: {e}")
45
+
46
+ driver.quit()
47
+ return "\n".join(logs)
48
 
49
+ # --- Gradio 界面 ---
50
+ with gr.Blocks(title="网站流量生成器") as demo:
51
+ gr.Markdown("## 🤖 网站流量生成器\n基于 Selenium 和 Hugging Face Spaces")
52
+
53
+ with gr.Row():
54
+ with gr.Column():
55
+ url_input = gr.Textbox(label="目标网址 (URL)", placeholder="https://example.com")
56
+ views_input = gr.Number(label="访问次数", value=10, minimum=1, maximum=100, precision=0)
57
+ stay_min_input = gr.Number(label="最小停留时间 (秒)", value=5, minimum=1, precision=0)
58
+ stay_max_input = gr.Number(label="最大停留时间 (秒)", value=15, minimum=1, precision=0)
59
+ run_btn = gr.Button("🚀 开始生成流量", variant="primary")
60
+
61
+ with gr.Column():
62
+ output_log = gr.Textbox(label="运行日志", lines=10, max_lines=20, interactive=False)
63
+
64
+ run_btn.click(
65
+ fn=generate_traffic,
66
+ inputs=[url_input, views_input, stay_min_input, stay_max_input],
67
+ outputs=output_log
 
 
68
  )
 
69
 
70
+ # 启动应用
71
+ demo.launch()