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
| const processConversations = async (dateObj) => { const username = '你的帐户名'; const password = '密码';
const year = dateObj.format('YYYY'); const month = dateObj.format('MM'); const day = dateObj.format('DD'); const hour = dateObj.format('HH'); const dateStr = `${year}-${month}-${day}-${hour}`; const fileName = `路径后半部份`; const url = `https://api/${fileName}`; const outputPath = `/tmp/conversations`; const tableName = 'test';
try { const response = await axios.get(url, { responseType: 'stream', headers: { Authorization: `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}` } });
const writer = fs.createWriteStream(outputPath); response.data.pipe(writer); await new Promise((resolve, reject) => { writer.on('finish', resolve); writer.on('error', reject); });
console.log(' CSV 文件下载成功:', fileName);
const rows = [];
await new Promise((resolve, reject) => { fs.createReadStream(outputPath) .pipe(csv()) .on('data', (row) => { rows.push([ row['DATE_TIME'] ? dayjs(row['DATE_TIME']).format('YYYY-MM-DD HH:mm:ss') : null, row['CLID'] || null, row['CLID_SOURCE'] || null ]); }) .on('end', resolve) .on('error', reject); });
if (rows.length === 0) { console.warn(`CSV 中没有数据: ${fileName}`); return; }
const insertSQL = ` INSERT INTO \`${tableName}\` ( date_time, clid, clid_source ) VALUES ? `;
await query(insertSQL, [rows]); console.log(`成功插入 ${rows.length} 条记录: ${fileName}`); } catch (error) { console.error(`报错 (${fileName}):`, error.message); } };
|