Duyệt tài liệu
Hướng dẫn
Thêm siêu dữ liệu vào danh sách phát nhạc, đọc danh sách hoặc đặt dữ liệu ứng dụng trong khi giữ hồ sơ gốc IDs và mọi kết quả.
Trên trang này
Từ hồ sơ của bạn đến một bộ dữ liệu phong phú hơn
Bắt đầu với ba đĩa nói về hai video. Truy vấn mỗi video riêng một lần, sau đó tham gia siêu dữ liệu vào cả ba tài liệu. Dòng làm việc này sử dụng một yêu cầu Batch nhỏ và giữ cho các mục không thành công hiển thị.
| Trường xuất | Mục đích |
|---|---|
| record_id · input | Hồ sơ và lĩnh vực của anh, theo thứ tự ban đầu. |
| video_id · query_index · job_id | Theo dõi từng hồ sơ truy vấn tạo ra nó. |
| status · metadata · result_file | Kết quả đã hoàn tất, hay rõ ràng pending/failed/read-error Kết quả. |
1. Kiểm tra công cụ và truy cập của bạn
Dùng FlashData CLI v1.2.1 trên PATH và Node.js 22+. Chạy trong thư mục mới với sự cho phép ủy nhiệm cho người sở hữu đã có và youtube_metadata. Bash và PowerShell sử dụng cùng một lệnh bên dưới.
Hãy ghi nhớ cùng một tài khoản và hồ sơ. Đối với hồ sơ tên, set FLASHDATA_PROFILE trong nhà đón khách này, người sưu tầm cũng sử dụng nó. Tác nhân có thể nạp đạn. FLASHDATA_API_KEY từ cửa hàng bí mật của họ.
flashdata --version
node --version
flashdata auth status --json
mkdir video-enrichment
cd video-enrichmentKiểm tra sự số dư và khả năng
Xác nhận quyền truy cập chủ sở hữu, youtube_metadata Cho phép và cho hai mẻ đá và chờ việc làm. Nếu truy cập tạm thời vẫn còn hoạt động, hãy yêu cầu hoặc sử dụng công thức riêng. Xem lại dự đoán chạy cạn trong bước 3 trước khi gửi.
flashdata account balance --json
flashdata account limits --json2. Chuẩn bị hồ sơ nhập
Lưu vào videos.json. Thay thế các giá trị với hồ sơ riêng của bạn IDs và 11- Hồ sơ. YouTube video IDs. Các trường phụ nằm trong bản đồ địa phương và không được gửi đến FlashData. Video đã lặp lại IDs được phép; record_id Chắc là độc nhất vô nhị.
[
{
"record_id": "lesson-intro",
"video_id": "jNQXAC9IVRw",
"collection": "Course"
},
{
"record_id": "playlist-song",
"video_id": "dQw4w9WgXcQ",
"collection": "Playlist"
},
{
"record_id": "saved-intro",
"video_id": "jNQXAC9IVRw",
"collection": "Reading list"
}
]Lưu prepare.mjs
Tạo prepare.mjs bên cạnh videos.json trong thư mục làm giàu video. Hãy sao chép toàn bộ mã lệnh bên dưới và lưu nó lại.
Văn lệnh xác thực mọi hàng trước khi tạo prepared/. Nó làm hỏng video IDs yêu cầu siêu dữ liệu này trong khi vẫn còn giữ hồ sơ và thứ tự gốc.
// prepare.mjs — local preparation; sends no API requests.
import { mkdir, readFile, writeFile } from 'node:fs/promises';
const rows = JSON.parse((await readFile('videos.json', 'utf8')).replace(/^\uFEFF/, ''));
if (!Array.isArray(rows) || rows.length === 0) throw new Error('Supply a nonempty array of records.');
const recordIds = new Set(), videoIndexes = new Map(), queries = [];
const mapping = rows.map((input, index) => {
if (!input || typeof input !== 'object' || Array.isArray(input)
|| typeof input.record_id !== 'string' || !input.record_id.trim()
|| recordIds.has(input.record_id) || typeof input.video_id !== 'string'
|| !/^[A-Za-z0-9_-]{11}$/.test(input.video_id)) {
throw new Error('Invalid or duplicate record_id, or invalid video_id at row ' + (index + 1));
}
recordIds.add(input.record_id);
if (!videoIndexes.has(input.video_id)) {
videoIndexes.set(input.video_id, queries.length);
queries.push({ source: 'youtube_metadata', video_id: input.video_id,
params: { include_formats: false } });
}
return { input, query_index: videoIndexes.get(input.video_id) };
});
// Existing preparation is kept intact. Use it to resume, or choose a new folder.
await mkdir('prepared');
await writeFile('prepared/batch.json', JSON.stringify(queries, null, 2), { flag: 'wx' });
await writeFile('prepared/input-map.json', JSON.stringify(mapping, null, 2), { flag: 'wx' });
console.log(JSON.stringify({ inputRecords: rows.length, uniqueQueries: queries.length,
reusedVideoIds: rows.length - queries.length }, null, 2));Xây dựng danh sách truy vấn cục bộ
Sau khi lưu prepare.mjsChạy một lần đi. Mong đợi inputRecords: 3, uniqueQueries: 2 và reusedVideoIds: 1. Giữ prepared/batch.json và prepared/input-map.json Không thay đổi cho lần chạy này. Nếu đã chuẩn bị/ đã có, hãy dùng lại nó để tiếp tục lại; hãy dùng thư mục mới cho bộ dữ liệu khác.
node prepare.mjs3. Xem thử và gửi các video độc nhất
Cuộc chạy cạn kiểm tra số lượng truy vấn thực sự, giá cả hiện tại và giới hạn tài khoản. Xem lại, rồi nộp một lần. Mẫu này có hai bản sao, mặc dù đầu vào có ba đĩa.
Giữ submission.json và CLI Thư mục chính quy. Dùng lại cùng một lần chạy ID và đầu vào sau khi bị gián đoạn. Một cuộc chạy đua mới ID tạo ra công việc mới; kiểm tra một sự phục tùng không chắc chắn trước khi thử lại.
flashdata batch submit --input prepared/batch.json --dry-run --jsonflashdata batch submit --input prepared/batch.json --run-id video-metadata-001 --output submission.json --json4. Thu thập và tham gia kết quả
Lưu collect.mjs Bên dưới, rồi chạy. Người sưu tầm chờ đến hai phút mỗi lần làm việc chưa hoàn thành, xác nhận kết quả đó thuộc về video đã yêu cầu, và tham gia vào mỗi kết quả của bản gốc. Điều hành từng nhà sưu tập một.
Chạy lại nó để tiếp tục công việc IDs. Nó tái sử dụng kết quả cục bộ và không gửi các yêu cầu mới. Mã thoát 1 có nghĩa là một số kết quả vẫn cần được chú ý; thanh tra chúng thay vì phục hồi danh sách.
Lưu collect.mjs
Tạo collect.mjs trong cùng một thư mục làm giàu phim, bên cạnh submission.json và prepared/. Hãy sao chép toàn bộ mã lệnh bên dưới và lưu nó lại.
// collect.mjs — reads saved jobs; never submits another query.
import { execFileSync } from 'node:child_process';
import { mkdir, readFile, writeFile, rename } from 'node:fs/promises';
const readJSON = async file => JSON.parse((await readFile(file, 'utf8')).replace(/^\uFEFF/, ''));
const cli = (...args) => JSON.parse(execFileSync('flashdata', [
...args, '--json', '--non-interactive', '--timeout', '2m',
], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 16 * 1024 * 1024 }));
const inputs = await readJSON('prepared/batch.json');
const mapping = await readJSON('prepared/input-map.json');
const saved = await readJSON('submission.json');
const receipts = (saved.submission ?? saved).queries;
if (!Array.isArray(inputs) || !inputs.length || !Array.isArray(receipts)
|| receipts.length !== inputs.length || new Set(receipts.map(r => r.id)).size !== receipts.length
|| inputs.some(q => q.source !== 'youtube_metadata') || !Array.isArray(mapping) || !mapping.length
|| mapping.some(row => !Number.isInteger(row.query_index)
|| inputs[row.query_index]?.video_id !== row.input?.video_id)) {
throw new Error('Input/receipt mismatch. Keep the original prepared files and receipt.');
}
await mkdir('results', { recursive: true });
const binding = JSON.stringify({ inputs, mapping, receipts });
try { await writeFile('results/input-receipts.json', binding, { flag: 'wx' }); }
catch (error) {
if (error.code !== 'EEXIST') throw error;
if (await readFile('results/input-receipts.json', 'utf8') !== binding) {
throw new Error('Inputs or receipts changed. Restore the original files.');
}
}
const rows = inputs.map((input, query_index) => ({ query_index,
video_id: input.video_id, job_id: receipts[query_index].id, status: 'unread' }));
const saveRows = async (path, values) => {
await writeFile(path + '.next', values.map(row => JSON.stringify(row)).join('\n') + '\n');
await rename(path + '.next', path);
};
for (const row of rows) {
try {
if (!/^[a-zA-Z0-9_-]+$/.test(row.job_id ?? '')) throw new Error('Invalid job ID.');
const file = 'results/' + row.job_id + '.json';
let result;
try { result = await readJSON(file); }
catch (error) { if (error.code !== 'ENOENT') throw error; }
if (!result) {
let job;
try { job = cli('jobs', 'wait', row.job_id).data; }
catch { job = cli('jobs', 'get', row.job_id).data; }
if (job.videoId !== row.video_id || job.source !== 'youtube_metadata') {
throw new Error('Job/input mismatch. Inspect the saved input and receipt.');
}
row.status = job.status;
row.error = job.error ?? null;
if (job.status === 'completed') {
cli('jobs', 'result', row.job_id, '--output', file);
result = await readJSON(file);
}
}
if (result) {
if (result.status !== 'done' || result.source !== 'youtube_metadata'
|| result.results?.length !== 1 || result.results[0].videoId !== row.video_id) {
throw new Error('Result/video mismatch. Inspect the saved JSON before continuing.');
}
Object.assign(row, { status: 'completed', metadata: result.results[0], error: null,
credits: result.usage?.credits ?? null, result_file: file });
}
} catch (error) {
row.status = 'read_error';
row.error = error.stderr?.toString().trim() || error.message;
}
await saveRows('metadata.ndjson', rows); // One outcome and charge per unique query.
await saveRows('enriched.ndjson', mapping.map(({ input, query_index }) => {
const { credits, ...outcome } = rows[query_index]; // Do not duplicate a charge across input records.
return { record_id: input.record_id, input, ...outcome };
}));
}
const failed = rows.filter(row => ['failed', 'cancelled'].includes(row.status));
await writeFile('failed-videos.json', JSON.stringify(failed, null, 2));
console.table(rows.map(({ video_id, job_id, status }) => ({ video_id, job_id, status })));
console.log('Saved enriched.ndjson, metadata.ndjson and results/*.json.');
if (rows.some(row => row.status !== 'completed')) process.exitCode = 1;Chạy hoặc tiếp tục sưu tập
Chạy khỏi việc làm giàu phim ảnh sau khi lưu collect.mjs. Giữ nguyên bản gốc CLI Thư mục hồ sơ và tiểu dụng.
node collect.mjsMong đợi tập tin
| Tập tin | Dùng nó đi |
|---|---|
| enriched.ndjson | Một hàng trên bản ghi chép gốc, bao gồm bản sao và thất bại. Nhập khẩu bởi record_id. |
| metadata.ndjson | Một hàng mỗi yêu cầu duy nhất, với công việc IDhồ sơ kết quả và tín dụng thực tế. Đếm số lần sử dụng ở đây. |
| results/*.json | Những câu trả lời thành công thô cộng với việc lưu input/receipt ràng buộc. |
| failed-videos.json | Xác nhận failed/cancelled làm việc để xem xét. Việc chờ đợi và đọc những công việc khủng bố được giữ ngoài danh sách này. |
5. Dùng hồ sơ phong phú trong ứng dụng của bạn
Lưu inspect.mjs trong phim- làm giàu, bên cạnh enriched.ndjsonSau đó chạy nó từ tập tài liệu đó. Hy vọng có 3 bản gốc và 2 bản thảo độc đáo. Trang đã hoàn tất hàng bằng record_idHãy giữ các trường nhập và hiển thị các hàng chưa hoàn chỉnh trong báo cáo nhập khẩu. Đọc tựa đề và khoảng thời gian từ siêu dữ liệu; sử dụng video_id yêu cầu liên kết hoặc sau này phụ đề.
// inspect.mjs — inspect your dataset without another API call.
import { readFile } from 'node:fs/promises';
const rows = (await readFile('enriched.ndjson', 'utf8')).trim().split('\n').map(JSON.parse);
console.table(rows.map(row => ({ record_id: row.record_id, video_id: row.video_id,
collection: row.input.collection, status: row.status, title: row.metadata?.title ?? null })));
console.log('Original records:', rows.length);
console.log('Unique queries:', new Set(rows.map(row => row.query_index)).size);node inspect.mjsTiếp tục từ những thứ cần chú ý
Để dành kết quả trong tay họ 24- thời gian lưu trữ hàng giờ. Để cập nhật lại, hãy dùng thư mục chụp ảnh mới rồi chạy ID Chỉ khi bạn có ý định để trả tiền cho dữ liệu mới; giữ record_id ổn định trong đơn của anh.
| Kết quả | Hành động tiếp theo |
|---|---|
| pending / processing | Sau đó hãy chạy bộ sưu tập lại. Giữ cùng một công việc IDs. |
| failed / cancelled | Xem lại failed-videos.json và lỗi công việc. Hãy sửa chữa nguyên nhân trước khi yêu cầu video đó một lần nữa. |
| read_error | Sửa truy cập, kết nối nhập hoặc kết quả đã lưu. Giữ tập tin thô để kiểm tra; lỗi đọc không có nghĩa là cần một yêu cầu mới. |
| Không rõ kết quả phục tùng | Kiểm tra chạy cục bộ và History trước khi nộp lại. |
flashdata runs show video-metadata-001 --json