Created
August 30, 2023 07:43
-
-
Save Eventyret/dadd7b219df9d96cddee72d980008713 to your computer and use it in GitHub Desktop.
Transfer data from two API's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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