From bafc64fb5be6363766689b961012ddc37ce37e62 Mon Sep 17 00:00:00 2001 From: Hippo Date: Wed, 9 Oct 2019 16:17:37 +0530 Subject: [PATCH] Implement basic "create-user" functions (outputs JSON) --- functions.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- index.js | 7 ++++--- package.json | 3 ++- 3 files changed, 53 insertions(+), 7 deletions(-) mode change 100644 => 100755 index.js diff --git a/functions.js b/functions.js index 163b6b2..b245493 100644 --- a/functions.js +++ b/functions.js @@ -1,3 +1,5 @@ +const r2 = require('r2') + /** * function [fetchFromMedium] * @returns [string] status @@ -22,17 +24,59 @@ 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 createUser = (mediumUsername, email) => { - console.info('Creating: @' + mediumUsername + '(email: ' + email + ')'); +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, - createUser + generateUserData, } diff --git a/index.js b/index.js old mode 100644 new mode 100755 index e2afa76..096bc29 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ const { fetchFromMedium, pushToGhost, mediumToGhost, - createUser + generateUserData, } = require ('./functions'); program @@ -36,8 +36,9 @@ program.command('medium-to-ghost ') program.command('create-user ') .description('create ghost-import.json to import Medium user to Ghost') - .action((username, email) => { - createUser(username, email); + .action(async (username, email) => { + const jsonOut = await generateUserData(username, email); + console.log(await jsonOut); }); program.parse(process.argv) diff --git a/package.json b/package.json index fb9bd39..39c0ecb 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ }, "dependencies": { "commander": "^3.0.2", - "mediumexporter": "github:xdamman/mediumexporter#v0.2.0" + "mediumexporter": "github:xdamman/mediumexporter#v0.2.0", + "r2": "^2.0.1" } }