Implement basic "create-user" functions (outputs JSON)

This commit is contained in:
Hippo 2019-10-09 16:17:37 +05:30
parent 1a6d7a8df3
commit bafc64fb5b
3 changed files with 53 additions and 7 deletions

View file

@ -1,3 +1,5 @@
const r2 = require('r2')
/** /**
* function [fetchFromMedium] * function [fetchFromMedium]
* @returns [string] status * @returns [string] status
@ -22,17 +24,59 @@ const mediumToGhost = (mediumUrl) => {
console.info('Copying: ' + 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] * function [createUser]
* @returns [string] status * @returns [string] status
*/ */
const createUser = (mediumUsername, email) => { const generateUserData = async (mediumUsername, email) => {
console.info('Creating: @' + mediumUsername + '(email: ' + 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 = { module.exports = {
fetchFromMedium, fetchFromMedium,
pushToGhost, pushToGhost,
mediumToGhost, mediumToGhost,
createUser generateUserData,
} }

7
index.js Normal file → Executable file
View file

@ -6,7 +6,7 @@ const {
fetchFromMedium, fetchFromMedium,
pushToGhost, pushToGhost,
mediumToGhost, mediumToGhost,
createUser generateUserData,
} = require ('./functions'); } = require ('./functions');
program program
@ -36,8 +36,9 @@ program.command('medium-to-ghost <mediumUrl>')
program.command('create-user <username> <email>') program.command('create-user <username> <email>')
.description('create ghost-import.json to import Medium user to Ghost') .description('create ghost-import.json to import Medium user to Ghost')
.action((username, email) => { .action(async (username, email) => {
createUser(username, email); const jsonOut = await generateUserData(username, email);
console.log(await jsonOut);
}); });
program.parse(process.argv) program.parse(process.argv)

View file

@ -13,6 +13,7 @@
}, },
"dependencies": { "dependencies": {
"commander": "^3.0.2", "commander": "^3.0.2",
"mediumexporter": "github:xdamman/mediumexporter#v0.2.0" "mediumexporter": "github:xdamman/mediumexporter#v0.2.0",
"r2": "^2.0.1"
} }
} }