From 198f576123a45036334a2e98eed81fe91bc430a4 Mon Sep 17 00:00:00 2001 From: Badri Sunderarajan Date: Mon, 4 May 2020 18:06:01 +0530 Subject: [PATCH] Allow passing local JSON instead of downloading This way, if you have the JSON already there locally, you don't have to bother downloading it from online. Especially useful if the JSON is passed to you some other way, like through a browser for example --- functions.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/functions.js b/functions.js index e55233b..fa68f74 100644 --- a/functions.js +++ b/functions.js @@ -31,13 +31,19 @@ class Seance { */ } - async fetchFromMedium (mediumUrl) { + async fetchFromMedium (mediumUrl, options = { + json: null, + }) { console.info(`Fetching: ${mediumUrl}`); var output = path.join(process.env.PWD, 'content') var json - json = await this.fetchMediumJSON(mediumUrl) + if (!options.json) { + json = await this.fetchMediumJSON(mediumUrl) + } else { + json = options.json + } // use mediumexporter's getPost function to fetch a Medium post const post = await getPost(mediumUrl, { @@ -264,8 +270,10 @@ class Seance { mediumUrl= `${mediumUrl}?format=json` const response = await fetch(mediumUrl) text = await response.text() + } else if (fs.existsSync(mediumUrl)) { + text = fs.readFileSync(mediumUrl).toString() } else { - throw { error: 'URL must be a Medium URL' } + throw { error: 'URL must be a Medium URL or existing JSON file' } } json = await JSON.parse(text.substr(text.indexOf('{')))