Incluye utilidades de ops para CRON_SECRET, login customer Google Ads, sync de seguimiento BPS y probes de conexiones. Co-authored-by: Cursor <cursoragent@cursor.com>
64 lines
2.2 KiB
TypeScript
64 lines
2.2 KiB
TypeScript
import { config } from 'dotenv';
|
|
import { resolve } from 'path';
|
|
import { writeFileSync, mkdirSync } from 'fs';
|
|
config({ path: resolve(process.cwd(), '.env.local') });
|
|
import { getValidAccessToken } from '../src/lib/google-drive';
|
|
|
|
const IDS = [
|
|
'1YPcHBjioiHHQy22Q2xevkvUqA5jMq87v6IviV7VTL2s',
|
|
'1-Zss1suBLDxAgqUuP9DrJYWHiicyin8aQqMOTIIekAg',
|
|
];
|
|
|
|
async function main() {
|
|
const token = await getValidAccessToken();
|
|
mkdirSync('/tmp/bps-cal', { recursive: true });
|
|
for (const id of IDS) {
|
|
const metaR = await fetch(
|
|
`https://www.googleapis.com/drive/v3/files/${id}?fields=id,name,mimeType&supportsAllDrives=true`,
|
|
{ headers: { Authorization: `Bearer ${token}` } }
|
|
);
|
|
const meta = (await metaR.json()) as {
|
|
id: string;
|
|
name: string;
|
|
mimeType: string;
|
|
};
|
|
console.log('META', meta);
|
|
const mime = meta.mimeType || '';
|
|
let buf: Buffer;
|
|
if (mime.includes('spreadsheet')) {
|
|
const r = await fetch(
|
|
`https://www.googleapis.com/drive/v3/files/${id}/export?mimeType=${encodeURIComponent('text/csv')}`,
|
|
{ headers: { Authorization: `Bearer ${token}` } }
|
|
);
|
|
if (!r.ok) throw new Error(await r.text());
|
|
buf = Buffer.from(await r.arrayBuffer());
|
|
writeFileSync(`/tmp/bps-cal/${id}.csv`, buf);
|
|
} else if (mime.includes('document')) {
|
|
const r = await fetch(
|
|
`https://www.googleapis.com/drive/v3/files/${id}/export?mimeType=${encodeURIComponent('text/plain')}`,
|
|
{ headers: { Authorization: `Bearer ${token}` } }
|
|
);
|
|
if (!r.ok) throw new Error(await r.text());
|
|
buf = Buffer.from(await r.arrayBuffer());
|
|
writeFileSync(`/tmp/bps-cal/${id}.txt`, buf);
|
|
} else {
|
|
const r = await fetch(
|
|
`https://www.googleapis.com/drive/v3/files/${id}?alt=media&supportsAllDrives=true`,
|
|
{ headers: { Authorization: `Bearer ${token}` } }
|
|
);
|
|
if (!r.ok) throw new Error(await r.text());
|
|
buf = Buffer.from(await r.arrayBuffer());
|
|
writeFileSync(`/tmp/bps-cal/${id}.bin`, buf);
|
|
}
|
|
const text = buf.toString('utf8');
|
|
console.log('size', buf.length);
|
|
console.log(text.slice(0, 3000));
|
|
console.log('----\n');
|
|
}
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|