Created
March 30, 2022 09:23
-
-
Save dannyhw/120917afe3e390679485b85ffefe84ff to your computer and use it in GitHub Desktop.
if you put your integration token in a .env.json you can automatically post a markdown file to medium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const yargs = require("yargs/yargs"); | |
const { hideBin } = require("yargs/helpers"); | |
const { token } = require("./.env.json"); | |
const { URL, URLSearchParams } = require("url"); | |
const fetch = require("node-fetch"); | |
class HTTPResponseError extends Error { | |
constructor(response, customMessage, ...args) { | |
super( | |
`${customMessage}: ${response.status} ${response.statusText}`, | |
...args | |
); | |
this.response = response; | |
} | |
} | |
const headers = { | |
Accept: "application/json", | |
"Accept-Encoding": "gzip, deflate, br", | |
"Accept-Language": "en-US,en;q=0.5", | |
Connection: "keep-alive", | |
"Accept-Charset": "utf-8", | |
Host: "api.medium.com", | |
"Content-Type": "application/json", | |
Authorization: `Bearer ${token}`, | |
"Upgrade-Insecure-Requests": "1", | |
"User-Agent": | |
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0", | |
}; | |
async function getAuthorId() { | |
const url = new URL("https://api.medium.com/v1/me"); | |
url.search = new URLSearchParams({ Authorization: `Bearer ${token}` }); | |
const result = await fetch(url, { headers, method: "GET" }); | |
if (result.status >= 400) { | |
throw new HTTPResponseError(result, "Error getting author id"); | |
} | |
const jsonResponse = await result.json(); | |
return jsonResponse.data.id; | |
} | |
async function publish(data) { | |
const authorId = await getAuthorId(); | |
const url = new URL(`https://api.medium.com/v1/users/${authorId}/posts`); | |
const result = await fetch(url, { | |
method: "POST", | |
headers, | |
body: JSON.stringify(data), | |
}); | |
if (result.status >= 400) { | |
throw new HTTPResponseError(result, "Error publishing post"); | |
} | |
const jsonResponse = await result.json(); | |
return jsonResponse.data.url; | |
} | |
async function main({ filePath, title }) { | |
const markdownFile = require("fs").readFileSync(filePath, { | |
encoding: "utf8", | |
}); | |
const data = { | |
title: title, | |
contentFormat: "markdown", | |
content: markdownFile.toString(), | |
publishStatus: "draft", | |
}; | |
const url = await publish(data); | |
console.log("posted to medium:", url); | |
} | |
const argv = yargs(hideBin(process.argv)) | |
.usage( | |
'./publish-to-medium.js --title="Hello World" --filePath="./path/to/file"' | |
) | |
.demandOption(["title", "filePath"]).argv; | |
main(argv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment