2024-09-18 07:28:57 +05:30
|
|
|
import { Router } from 'express';
|
2024-09-18 18:27:38 +05:30
|
|
|
import { google } from "googleapis";
|
2024-09-17 20:39:42 +05:30
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
2024-09-18 18:27:38 +05:30
|
|
|
import logger from "../logger";
|
2024-09-17 20:38:32 +05:30
|
|
|
|
2024-09-18 18:27:38 +05:30
|
|
|
export const router = Router();
|
2024-09-17 20:38:32 +05:30
|
|
|
|
2024-09-18 18:27:38 +05:30
|
|
|
router.post('/upload-credentials', async (req, res) => {
|
|
|
|
|
const { credentials, spreadsheetId, range } = req.body;
|
2024-09-17 20:58:30 +05:30
|
|
|
|
2024-09-18 18:27:38 +05:30
|
|
|
if (!credentials || !spreadsheetId || !range) {
|
|
|
|
|
return res.status(400).json({ message: 'Credentials, Spreadsheet ID, and Range are required.' });
|
2024-09-17 20:39:42 +05:30
|
|
|
}
|
2024-09-18 18:27:38 +05:30
|
|
|
|
2024-09-17 20:39:42 +05:30
|
|
|
// Todo: Store the credentials in a secure place (for test, we store them locally)
|
2024-09-17 20:58:30 +05:30
|
|
|
const storedCredentialsPath = path.join(__dirname, 'service_account_credentials.json');
|
2024-09-18 18:27:38 +05:30
|
|
|
|
2024-09-17 20:47:36 +05:30
|
|
|
try {
|
2024-09-18 18:27:38 +05:30
|
|
|
fs.writeFileSync(storedCredentialsPath, JSON.stringify(credentials));
|
|
|
|
|
logger.log('info', 'Service account credentials saved successfully.');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.log('error', `Error saving credentials: ${error.message}`);
|
|
|
|
|
return res.status(500).json({ message: 'Failed to save credentials.', error: error.message });
|
|
|
|
|
}
|
2024-09-17 20:58:30 +05:30
|
|
|
|
2024-09-18 18:27:38 +05:30
|
|
|
let storedCredentials;
|
|
|
|
|
try {
|
|
|
|
|
storedCredentials = JSON.parse(fs.readFileSync(storedCredentialsPath, 'utf-8'));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.log('error', `Error reading credentials: ${error.message}`);
|
|
|
|
|
return res.status(500).json({ message: 'Failed to read credentials.', error: error.message });
|
|
|
|
|
}
|
2024-09-17 20:58:30 +05:30
|
|
|
|
2024-09-18 18:27:38 +05:30
|
|
|
let authToken;
|
|
|
|
|
try {
|
2024-09-17 20:58:30 +05:30
|
|
|
const auth = new google.auth.GoogleAuth({
|
2024-09-17 23:50:57 +05:30
|
|
|
credentials: {
|
2024-09-18 18:27:38 +05:30
|
|
|
client_email: storedCredentials.client_email,
|
|
|
|
|
private_key: storedCredentials.private_key,
|
2024-09-17 23:50:57 +05:30
|
|
|
},
|
2024-09-17 20:58:30 +05:30
|
|
|
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
|
|
|
|
|
});
|
|
|
|
|
|
2024-09-18 18:27:38 +05:30
|
|
|
authToken = await auth.getClient();
|
|
|
|
|
logger.log('info', 'Authenticated with Google Sheets API successfully.');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.log('error', `Google Sheets API Authentication failed: ${error.message}`);
|
|
|
|
|
return res.status(500).json({ message: 'Authentication with Google failed.', error: error.message });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sheets = google.sheets({ version: 'v4', auth: authToken });
|
|
|
|
|
|
|
|
|
|
// Data to be written to the sheet
|
|
|
|
|
const values = [
|
|
|
|
|
['Scraped Data 1', 'More Data', 'IDKKKKKKKKK'],
|
|
|
|
|
];
|
2024-09-17 20:58:30 +05:30
|
|
|
|
2024-09-18 18:27:38 +05:30
|
|
|
const resource = {
|
|
|
|
|
values,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
2024-09-17 20:58:30 +05:30
|
|
|
await sheets.spreadsheets.values.append({
|
|
|
|
|
spreadsheetId,
|
|
|
|
|
range,
|
|
|
|
|
valueInputOption: 'USER_ENTERED',
|
2024-09-17 23:50:57 +05:30
|
|
|
requestBody: resource,
|
2024-09-18 18:27:38 +05:30
|
|
|
});
|
|
|
|
|
logger.log('info', `Data written to Google Sheet: ${spreadsheetId}, Range: ${range}`);
|
|
|
|
|
return res.status(200).json({ message: 'Data written to Google Sheet successfully.' });
|
2024-09-17 20:47:36 +05:30
|
|
|
} catch (error) {
|
2024-09-18 18:27:38 +05:30
|
|
|
logger.log('error', `Failed to write to Google Sheet: ${error.message}`);
|
|
|
|
|
return res.status(500).json({ message: 'Failed to write to Google Sheet.', error: error.message });
|
2024-09-17 20:47:36 +05:30
|
|
|
}
|
2024-09-17 20:58:30 +05:30
|
|
|
});
|
2024-09-18 18:27:38 +05:30
|
|
|
|