-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
106 lines (82 loc) · 3.19 KB
/
index.js
File metadata and controls
106 lines (82 loc) · 3.19 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require('express');
const app = express();
const Airtable = require('airtable');
const fs = require('fs');
const axios = require('axios');
const _ = require('lodash');
const algoliasearch = require('algoliasearch');
require('dotenv').config();
app.get('/import', (req, res) => {
console.log('Starting import');
// Initialize Airtable client.
const airTableBase = new Airtable({ apiKey: process.env.AIRTABLE_API_KEY }).base(process.env.AIRTABLE_BASE_ID);
// Initialize the Algolia client.
const client = algoliasearch(process.env.ALGOLIA_APP_ID, process.env.ALGOLIA_ADMIN_API_KEY);
const AlgoliaIndex = client.initIndex(process.env.ALGOLIA_INDEX_NAME);
const downloadImage = async (url, filePath) => {
try {
const response = await axios({
url,
method: 'GET',
responseType: 'stream',
});
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
} catch (error) {
console.error('Error downloading the image:', error);
}
};
// Initialize Airtable
const data = [];
// Fetch records from the specified table
airTableBase(process.env.AIRTABLE_TABLE_NAME).select(
{}
).eachPage(async (records, fetchNextPage) => {
var p = []
var ap = []
// Process the records
records.forEach((record) => {
record.fields = _.mapKeys(record.fields, (v, k) => _.camelCase(k));
record.fields.objectID = record.id;
if (record.fields.logo && record.fields.logo.length > 0) {
let url = record.fields.logo[0].url
let ext = record.fields.logo[0].type.split('/')[1];
let savePath = `./logos/${record.fields.objectID}.${ext}`;
record.fields.logo = `${record.fields.objectID}.${ext}`;
}
ap.push(record.fields)
data.push(record.fields);
});
// Force the index to run.
await AlgoliaIndex.saveObjects(ap).then(({ objectIDs }) => {
console.log('Data pushed to Algolia:', objectIDs);
}).catch(err => {
console.error('Error pushing data to Algolia:', err);
});
// Force the save to run.
await Promise.all(p);
// Fetch the next page of records, if any.
fetchNextPage();
}, (err) => {
if (err) {
console.error('Error fetching records:', err);
}
console.log(`All done. Processed ${data.length} records.`);
res.send(`All done. Processed ${data.length} records.`);
// fs.writeFile('./output.json', JSON.stringify(data,), (err) => {
// if (err) {
// console.error('Error writing file:', err);
// } else {
// console.log('JSON data saved to', data.length);
// }
// });
});
});
const port = process.env.PORT || 10000;
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});