seance/functions.js
2019-10-11 16:25:52 +05:30

84 lines
1.8 KiB
JavaScript

const r2 = require('r2')
const getPost = require('mediumexporter').getPost
/**
* function [fetchFromMedium]
* @returns [string] status
*/
const fetchFromMedium = async (mediumUrl) => {
console.debug('Fetching: ' + mediumUrl);
return await getPost(mediumUrl, { returnObject: true })
};
/**
* 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;
}
/**
* 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))
};
module.exports = {
fetchFromMedium,
pushToGhost,
mediumToGhost,
generateUserData,
}