Skip to content

Instantly share code, notes, and snippets.

@Eventyret
Created August 30, 2023 07:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eventyret/dadd7b219df9d96cddee72d980008713 to your computer and use it in GitHub Desktop.
Save Eventyret/dadd7b219df9d96cddee72d980008713 to your computer and use it in GitHub Desktop.
Transfer data from two API's
const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const ghostEndpoint = 'https://ghostcms.com/content/posts/';
const strapiEndpoint = 'https://strapiinstance.com/api/blogs';
async function fetchData() {
try {
console.log('๐ŸŒ Fetching data from Ghost CMS...');
const response = await fetch(ghostEndpoint);
const data = await response.json();
const dataFilePath = path.join(__dirname, 'fetched_data.json');
let filename = 'fetched_data.json';
// Check if the file already exists
if (fs.existsSync(dataFilePath)) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('โš ๏ธ The file "fetched_data.json" already exists. Do you want to use a different filename? (yes/no): ', (answer) => {
rl.close();
answer = answer.trim().toLowerCase(); // Trim and convert to lowercase
if (answer === 'yes' || answer === 'y') {
console.log('โ„น๏ธ Enter the new filename (e.g., new_data.json):');
const newFilename = readline.createInterface({
input: process.stdin,
output: process.stdout
});
newFilename.question('', (newName) => {
newFilename.close();
filename = newName.trim() + '.json';
saveDataToFile(data, filename);
});
} else {
saveDataToFile(data, filename);
}
});
} else {
saveDataToFile(data, filename);
}
} catch (error) {
console.error('โŒ Error fetching data:', error.message);
}
}
async function saveDataToFile(data, filename) {
const dataFilePath = path.join(__dirname, filename);
try {
fs.writeFileSync(dataFilePath, JSON.stringify(data, null, 2));
console.log('โœ… Data fetched and saved to ' + filename);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('๐Ÿš€ Are you ready to transfer the data to strapiinstance.com? (yes/no): ', async (answer) => {
rl.close();
answer = answer.trim().toLowerCase(); // Trim and convert to lowercase
if (answer === 'yes' || answer === 'y') {
await transferDataToStrapi(data);
} else {
console.log('๐Ÿ›‘ Data transfer aborted.');
}
});
} catch (error) {
console.error('โŒ Error saving data:', error.message);
}
}
async function transferDataToStrapi(data) {
try {
console.log('๐Ÿ“ก Transferring data to strapiinstance.com...');
const response = await fetch(strapiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (response.ok) {
console.log('โœ… Data transferred successfully to strapiinstance.com.');
} else {
console.error('โŒ Data transfer failed with status:', response.status);
}
} catch (error) {
console.error('โŒ Error transferring data:', error.message);
}
}
fetchData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment