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
This commit is contained in:
Badri Sunderarajan 2020-05-04 18:06:01 +05:30
parent 0d9ecfd8f5
commit 198f576123

View file

@ -31,13 +31,19 @@ class Seance {
*/ */
} }
async fetchFromMedium (mediumUrl) { async fetchFromMedium (mediumUrl, options = {
json: null,
}) {
console.info(`Fetching: ${mediumUrl}`); console.info(`Fetching: ${mediumUrl}`);
var output = path.join(process.env.PWD, 'content') var output = path.join(process.env.PWD, 'content')
var json 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 // use mediumexporter's getPost function to fetch a Medium post
const post = await getPost(mediumUrl, { const post = await getPost(mediumUrl, {
@ -264,8 +270,10 @@ class Seance {
mediumUrl= `${mediumUrl}?format=json` mediumUrl= `${mediumUrl}?format=json`
const response = await fetch(mediumUrl) const response = await fetch(mediumUrl)
text = await response.text() text = await response.text()
} else if (fs.existsSync(mediumUrl)) {
text = fs.readFileSync(mediumUrl).toString()
} else { } 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('{'))) json = await JSON.parse(text.substr(text.indexOf('{')))