seance/functions.js

85 lines
1.8 KiB
JavaScript
Raw Normal View History

const r2 = require('r2')
const getPost = require('mediumexporter').getPost
2019-10-06 09:57:08 -04:00
/**
* function [fetchFromMedium]
* @returns [string] status
*/
const fetchFromMedium = async (mediumUrl) => {
console.debug('Fetching: ' + mediumUrl);
return await getPost(mediumUrl, { returnObject: true })
2019-10-06 09:57:08 -04:00
};
/**
* function [pushToGhost]
* @returns [string] status
*/
const pushToGhost = (postSlug) => {
console.info('Pushing: ' + postSlug);
};
/**
* function [mediumToGhost]
* @returns [string] status
*/
const mediumToGhost = (mediumUrl) => {
console.info('Copying: ' + mediumUrl);
};
async function fetchMediumJSON(mediumUrl) {
console.debug(`Fetching: ${mediumUrl}`)
const response = await fetch(mediumUrl)
const text = await response.text()
const json = await JSON.parse(text.substr(text.indexOf('{')))
return json;
}
2019-10-06 09:57:08 -04:00
/**
* function [createUser]
* @returns [string] status
*/
const generateUserData = async (mediumUsername, email) => {
console.debug('Creating: @' + mediumUsername + '(email: ' + email + ')');
const mediumUrl = `https://medium.com/@${mediumUsername}/?format=json`;
const json = await fetchMediumJSON(mediumUrl);
if (!json.success) {
console.error(`Error: ${json.error}`)
return false
}
console.debug(`Name: ${json.payload.user.name}`)
console.debug(`Bio: ${json.payload.user.bio}`)
// Generate Ghost JSON
const ghostData = {
data: {
users: [
{
id: 1,
slug: json.payload.user.username,
bio: json.payload.user.bio,
email: email,
name: json.payload.user.name,
}
]
},
meta: {
exported_on: new Date,
version: '2.14.0'
}
}
return(JSON.stringify(ghostData))
2019-10-06 09:57:08 -04:00
};
module.exports = {
fetchFromMedium,
pushToGhost,
mediumToGhost,
generateUserData,
2019-10-06 09:57:08 -04:00
}