Implement "fetch-medium" function
No processing yet, but at least we get the post and metadata in a standard format
This commit is contained in:
parent
0b45ee2799
commit
770dbb0850
2 changed files with 57 additions and 5 deletions
53
functions.js
53
functions.js
|
@ -1,13 +1,62 @@
|
||||||
const r2 = require('r2')
|
const r2 = require('r2')
|
||||||
|
const path = require('path')
|
||||||
|
const fs = require('fs')
|
||||||
const getPost = require('mediumexporter').getPost
|
const getPost = require('mediumexporter').getPost
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* function [fetchFromMedium]
|
* function [fetchFromMedium]
|
||||||
* @returns [string] status
|
* @returns [string] status
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const fetchFromMedium = async (mediumUrl) => {
|
const fetchFromMedium = async (mediumUrl) => {
|
||||||
console.debug('Fetching: ' + mediumUrl);
|
console.info(`Fetching: ${mediumUrl}`);
|
||||||
return await getPost(mediumUrl, { returnObject: true })
|
output = path.join(process.env.PWD, 'content')
|
||||||
|
|
||||||
|
// use mediumexporter's getPost function to fetch a Medium post
|
||||||
|
const post = await getPost(mediumUrl, {
|
||||||
|
returnObject: true,
|
||||||
|
output: output,
|
||||||
|
}).catch((err) => {
|
||||||
|
return {
|
||||||
|
error: err,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// set output folder path
|
||||||
|
// this is based on what mediumexporter chooses as the output folder
|
||||||
|
outputFolder = path.join(output, post.slug)
|
||||||
|
|
||||||
|
console.info(`Saving to: ${outputFolder}`)
|
||||||
|
|
||||||
|
if (!fs.existsSync(path.join(outputFolder, post.slug))) {
|
||||||
|
fs.mkdirSync(outputFolder, { recursive: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
// mediumexporter writes a plain .md file if the post has no media
|
||||||
|
// if that is the case, we should create the subfolder manually
|
||||||
|
// and copy the data there.
|
||||||
|
if (fs.existsSync(path.join(output, post.slug + '.md'))) {
|
||||||
|
fs.renameSync(
|
||||||
|
path.join(output, post.slug + '.md'),
|
||||||
|
path.join(outputFolder, 'index.md')
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate metadata
|
||||||
|
const metadata = JSON.stringify({
|
||||||
|
title: post.title,
|
||||||
|
subtitle: post.subtitle,
|
||||||
|
date: new Date(post.date),
|
||||||
|
tags: post.tags,
|
||||||
|
url: post.url,
|
||||||
|
slug: post.slug,
|
||||||
|
images: post.images,
|
||||||
|
featuredImage: post.featuredImage,
|
||||||
|
})
|
||||||
|
|
||||||
|
// write metadata to output folder
|
||||||
|
fs.writeFileSync(path.join(outputFolder, 'metadata.json'))
|
||||||
|
return post
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
9
index.js
9
index.js
|
@ -19,8 +19,7 @@ program.command('fetch-medium <post_url>')
|
||||||
.action((post_url) => {
|
.action((post_url) => {
|
||||||
fetchFromMedium(post_url)
|
fetchFromMedium(post_url)
|
||||||
.then((post) => {
|
.then((post) => {
|
||||||
console.info(`"${post.title}" fetched successfully`)
|
console.info(`"${post.title}" fetched successfully.`)
|
||||||
console.debug(post)
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -41,7 +40,11 @@ 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(async (username, email) => {
|
.action(async (username, email) => {
|
||||||
const jsonOut = await generateUserData(username, email);
|
const jsonOut = await generateUserData(username, email)
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(`Error: ${err.error}`)
|
||||||
|
return
|
||||||
|
})
|
||||||
console.log(await jsonOut);
|
console.log(await jsonOut);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue