Spaces:
Running
Running
Download server.js from aakgur/google-data-sync-wizard: direct link, hf CLI and curl.
- Browser
- Download file 3.17 kB
-
https://huggingface.co/spaces/aakgur/google-data-sync-wizard/resolve/main/server.js
- Command line
-
hf download hf://spaces/aakgur/google-data-sync-wizard/server.js
-
curl -L -o server.js https://huggingface.co/spaces/aakgur/google-data-sync-wizard/resolve/main/server.js
3.17 kB
| require('dotenv').config(); | |
| const express = require('express'); | |
| const cors = require('cors'); | |
| const { google } = require('googleapis'); | |
| const session = require('express-session'); | |
| const path = require('path'); | |
| const app = express(); | |
| const PORT = process.env.PORT || 3000; | |
| // Middleware | |
| app.use(cors()); | |
| app.use(express.json()); | |
| app.use(express.urlencoded({ extended: true })); | |
| app.use(session({ | |
| secret: process.env.SESSION_SECRET || 'your-secret-key', | |
| resave: false, | |
| saveUninitialized: true, | |
| cookie: { secure: process.env.NODE_ENV === 'production' } | |
| })); | |
| // Serve static files from the public directory | |
| app.use(express.static(path.join(__dirname, 'public'))); | |
| // Google OAuth2 Client | |
| const oauth2Client = new google.auth.OAuth2( | |
| process.env.GOOGLE_CLIENT_ID, | |
| process.env.GOOGLE_CLIENT_SECRET, | |
| process.env.GOOGLE_REDIRECT_URI || 'http://localhost:3000/auth/google/callback' | |
| ); | |
| // Routes | |
| app.get('/auth/google', (req, res) => { | |
| const url = oauth2Client.generateAuthUrl({ | |
| access_type: 'offline', | |
| scope: [ | |
| 'https://www.googleapis.com/auth/webmasters', | |
| 'https://www.googleapis.com/auth/analytics' | |
| ], | |
| prompt: 'consent' | |
| }); | |
| res.redirect(url); | |
| }); | |
| app.get('/auth/google/callback', async (req, res) => { | |
| const { code } = req.query; | |
| try { | |
| const { tokens } = await oauth2Client.getToken(code); | |
| oauth2Client.setCredentials(tokens); | |
| req.session.tokens = tokens; | |
| res.redirect('/'); | |
| } catch (error) { | |
| console.error('Error during Google callback:', error); | |
| res.status(500).send('Authentication failed'); | |
| } | |
| }); | |
| // Search Console API Endpoints | |
| app.post('/api/search-console/submit', async (req, res) => { | |
| try { | |
| const { urls } = req.body; | |
| const searchconsole = google.searchconsole({ | |
| version: 'v1', | |
| auth: oauth2Client | |
| }); | |
| const batchRequests = urls.map(url => ({ | |
| url, | |
| type: 'URL_UPDATED' | |
| })); | |
| const response = await searchconsole.urlInspection.index.inspect({ | |
| requestBody: { | |
| inspectionUrl: batchRequests[0].url, | |
| siteUrl: req.session.siteUrl | |
| } | |
| }); | |
| res.json({ | |
| success: true, | |
| data: response.data | |
| }); | |
| } catch (error) { | |
| console.error('Search Console error:', error); | |
| res.status(500).json({ error: 'Failed to submit URLs' }); | |
| } | |
| }); | |
| // Analytics API Endpoints | |
| app.post('/api/analytics/submit', async (req, res) => { | |
| try { | |
| const { events, trackingId } = req.body; | |
| const analytics = google.analytics('v3'); | |
| const batchRequests = events.map(event => ({ | |
| v: '1', | |
| tid: trackingId, | |
| cid: req.session.clientId || '555', | |
| t: event.type, | |
| ec: event.category, | |
| ea: event.action, | |
| el: event.label, | |
| ev: event.value | |
| })); | |
| // In a real app, you would send these to Google Analytics | |
| // This is a simulation | |
| res.json({ | |
| success: true, | |
| submitted: batchRequests.length | |
| }); | |
| } catch (error) { | |
| console.error('Analytics error:', error); | |
| res.status(500).json({ error: 'Failed to submit events' }); | |
| } | |
| }); | |
| // Start server | |
| app.listen(PORT, () => { | |
| console.log(`Server running on http://localhost:${PORT}`); | |
| }); |