upload_script.js
3.7 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 自动化视频上传脚本模板
```javascript
// upload.js - 通用的视频上传脚本
const { chromium } = require('playwright');
const fs = require('fs');
class VideoUploader {
constructor(platform, config) {
this.platform = platform;
this.config = config;
this.browser = null;
this.context = null;
this.page = null;
}
async init() {
this.browser = await chromium.launch({ headless: false });
this.context = await this.browser.newContext({
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X)',
viewport: { width: 390, height: 844 },
});
this.page = await this.context.newPage();
}
async login(username, password) {
await this.page.goto(this.config.entry);
await this.page.fill('input[type="text"]', username);
await this.page.fill('input[type="password"]', password);
await this.page.click('button[type="submit"]');
// 等待验证码输入(需手动)
await this.page.waitForTimeout(10000);
}
async upload(videoPath, { title, description, cover }) {
// 1. 选择文件
await this.page.setInputFiles(this.config.fileInput, videoPath);
// 2. 等待上传
await this.page.waitForTimeout(this.config.waitForUpload);
// 3. 填写标题
await this.page.fill(this.config.titleInput, title);
// 4. 填写描述
if (this.config.descInput) {
await this.page.fill(this.config.descInput, description);
}
// 5. 选择封面(如果有)
if (cover && this.config.coverBtn) {
await this.page.click(this.config.coverBtn);
await this.page.setInputFiles('input[type="file"]', cover);
}
// 6. 发布
await this.page.click(this.config.publishBtn);
// 7. 等待发布成功
await this.page.waitForTimeout(5000);
return { success: true, platform: this.platform };
}
async close() {
await this.browser.close();
}
// 保存登录态
async saveState(path) {
await this.context.storageState({ path });
}
// 加载登录态
async loadState(path) {
if (fs.existsSync(path)) {
this.context = await this.browser.newContext({
storageState: path,
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X)',
viewport: { width: 390, height: 844 },
});
this.page = await this.context.newPage();
}
}
}
// 使用示例
// const config = require('./playwright_config')[platform];
// const uploader = new VideoUploader(platform, config);
// await uploader.init();
// await uploader.loadState('./auth/douyin.json');
// const result = await uploader.upload('./video.mp4', { title: '标题', description: '描述' });
// await uploader.close();
```
```python
# upload_py.py - Python 版本(可选)
from playwright.sync_api import sync_playwright
import time
def upload_video(platform, video_path, title, description):
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context(
user_agent='Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X)',
viewport={'width': 390, 'height': 844}
)
page = context.new_page()
# 登录
page.goto(f'https://{platform}.com/upload')
page.wait_for_timeout(5000)
# 上传文件
page.set_input_files('input[type="file"]', video_path)
page.wait_for_timeout(10000)
# 填写信息
page.fill('input[placeholder="标题"]', title)
page.fill('textarea[placeholder="简介"]', description)
# 发布
page.click('button:has-text("发布")]')
page.wait_for_timeout(5000)
browser.close()
if __name__ == '__main__':
upload_video('douyin', './test.mp4', '测试标题', '测试描述')
```