Skip to content

快速开始

本指南将帮助您创建第一个 OmniBox 爬虫源。

前置要求

  • 已安装并运行 OmniBox
  • 具有管理后台访问权限
  • 了解基本的 JavaScript 或 Python 编程

创建爬虫源

1. 进入管理后台

  1. 打开 OmniBox 管理后台
  2. 导航到"爬虫源管理"页面
  3. 点击"新建爬虫源"按钮

2. 填写基本信息

  • 名称:爬虫源的显示名称(如:"示例视频站")
  • 类型:选择 JavaScriptPython
  • 描述:可选,描述爬虫源的功能

3. 选择模板

系统提供了两种模板:

  • JavaScript 模板:适用于熟悉 Node.js 的开发者
  • Python 模板:适用于熟悉 Python 的开发者

选择模板后,系统会自动填充基础代码。

编写第一个脚本

JavaScript 示例

javascript
const OmniBox = require("omnibox_sdk");
const runner = require("spider_runner");

module.exports = {
  home,
  category,
  detail,
  search,
  play,
};

runner.run(module.exports);

async function home(params) {
  OmniBox.log("info", "获取首页数据");
  
  // 返回分类和视频列表
  return {
    class: [
      { type_id: "1", type_name: "电影" },
      { type_id: "2", type_name: "电视剧" },
    ],
    list: [
      {
        vod_id: "1",
        vod_name: "示例视频",
        vod_pic: "https://example.com/pic.jpg",
        type_id: "1",
        type_name: "电影",
        vod_remarks: "HD",
        vod_year: "2024",
      },
    ],
  };
}

async function category(params) {
  const categoryId = params.categoryId || "1";
  const page = params.page || 1;
  
  return {
    page: page,
    pagecount: 10,
    total: 100,
    list: [
      {
        vod_id: `${categoryId}_${page}_1`,
        vod_name: `分类视频 ${page}-1`,
        vod_pic: "https://example.com/pic.jpg",
        type_id: categoryId,
        type_name: "电影",
        vod_remarks: "HD",
        vod_year: "2024",
      },
    ],
  };
}

async function detail(params) {
  const videoId = params.videoId;
  
  return {
    list: [
      {
        vod_id: videoId,
        vod_name: "示例视频详情",
        vod_pic: "https://example.com/pic.jpg",
        vod_content: "视频简介",
        vod_play_from: "线路1",
        vod_play_url: "第1集$https://example.com/play.m3u8",
      },
    ],
  };
}

async function search(params) {
  const keyword = params.keyword;
  const page = params.page || 1;
  
  return {
    page: page,
    pagecount: 5,
    total: 50,
    list: [
      {
        vod_id: `search_${keyword}_1`,
        vod_name: `搜索结果: ${keyword}`,
        vod_pic: "https://example.com/pic.jpg",
        type_id: "1",
        type_name: "电影",
        vod_remarks: "HD",
        vod_year: "2024",
      },
    ],
  };
}

async function play(params) {
  const playId = params.playId;
  
  return {
    url: `https://example.com/video/${playId}.m3u8`,
    header: {
      "User-Agent": "Mozilla/5.0",
      "Referer": "https://example.com/",
    },
    parse: 0,
  };
}

Python 示例

python
import asyncio
import json
import sys
from omnibox_sdk import OmniBox

async def home(params):
    await OmniBox.log("info", "获取首页数据")
    
    return {
        "class": [
            {"type_id": "1", "type_name": "电影"},
            {"type_id": "2", "type_name": "电视剧"},
        ],
        "list": [
            {
                "vod_id": "1",
                "vod_name": "示例视频",
                "vod_pic": "https://example.com/pic.jpg",
                "type_id": "1",
                "type_name": "电影",
                "vod_remarks": "HD",
                "vod_year": "2024",
            },
        ],
    }

async def category(params):
    category_id = params.get("categoryId", "1")
    page = params.get("page", 1)
    
    return {
        "page": page,
        "pagecount": 10,
        "total": 100,
        "list": [
            {
                "vod_id": f"{category_id}_{page}_1",
                "vod_name": f"分类视频 {page}-1",
                "vod_pic": "https://example.com/pic.jpg",
                "type_id": category_id,
                "type_name": "电影",
                "vod_remarks": "HD",
                "vod_year": "2024",
            },
        ],
    }

async def detail(params):
    video_id = params.get("videoId")
    
    return {
        "list": [
            {
                "vod_id": video_id,
                "vod_name": "示例视频详情",
                "vod_pic": "https://example.com/pic.jpg",
                "vod_content": "视频简介",
                "vod_play_from": "线路1",
                "vod_play_url": "第1集$https://example.com/play.m3u8",
            },
        ],
    }

async def search(params):
    keyword = params.get("keyword", "")
    page = params.get("page", 1)
    
    return {
        "page": page,
        "pagecount": 5,
        "total": 50,
        "list": [
            {
                "vod_id": f"search_{keyword}_1",
                "vod_name": f"搜索结果: {keyword}",
                "vod_pic": "https://example.com/pic.jpg",
                "type_id": "1",
                "type_name": "电影",
                "vod_remarks": "HD",
                "vod_year": "2024",
            },
        ],
    }

async def play(params):
    play_id = params.get("playId")
    
    return {
        "url": f"https://example.com/video/{play_id}.m3u8",
        "header": {
            "User-Agent": "Mozilla/5.0",
            "Referer": "https://example.com/",
        },
        "parse": 0,
    }

# 主函数
def main():
    input_data = sys.stdin.read()
    request = json.loads(input_data)
    method = request.get("method")
    params = request.get("params", {})
    
    if method == "home":
        result = asyncio.run(home(params))
    elif method == "category":
        result = asyncio.run(category(params))
    elif method == "detail":
        result = asyncio.run(detail(params))
    elif method == "search":
        result = asyncio.run(search(params))
    elif method == "play":
        result = asyncio.run(play(params))
    else:
        result = {"error": f"未知方法: {method}"}
    
    output = {"success": True, "data": result, "error": None}
    print(json.dumps(output, ensure_ascii=False))

if __name__ == "__main__":
    main()

调试脚本

1. 使用调试功能

  1. 在脚本编辑器中,点击"调试"按钮
  2. 选择要测试的方法(如:home
  3. 输入测试参数(JSON 格式)
  4. 点击"执行"按钮

2. 查看日志

  • 脚本执行过程中的日志会实时显示在调试面板
  • 使用 OmniBox.log() 记录自定义日志
  • 错误信息会自动显示

3. 查看结果

  • 执行结果会显示在调试面板
  • 可以查看返回的数据结构
  • 支持格式化 JSON 显示

保存和启用

  1. 点击"保存"按钮保存脚本
  2. 在源列表中启用该源
  3. 在前端页面测试源是否正常工作

使用模板

系统提供了完整的模板文件,包含:

  • 所有必需方法的实现框架
  • 详细的注释说明
  • 示例数据
  • SDK 使用示例

您可以直接使用模板,然后根据实际需求修改代码。

下一步

基于 MIT 许可发布