2019-10-09 06:47:37 -04:00
|
|
|
const r2 = require('r2')
|
2019-10-11 06:55:52 -04:00
|
|
|
const getPost = require('mediumexporter').getPost
|
2019-10-09 06:47:37 -04:00
|
|
|
|
2019-10-06 09:57:08 -04:00
|
|
|
/**
|
|
|
|
* function [fetchFromMedium]
|
|
|
|
* @returns [string] status
|
|
|
|
*/
|
2019-10-11 06:55:52 -04:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2019-10-09 06:47:37 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2019-10-09 06:47:37 -04:00
|
|
|
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,
|
2019-10-09 06:47:37 -04:00
|
|
|
generateUserData,
|
2019-10-06 09:57:08 -04:00
|
|
|
}
|