Skip to content

Commit

Permalink
Add post to medium to tools
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjberg committed Feb 16, 2024
1 parent adde0c3 commit 59ac2eb
Show file tree
Hide file tree
Showing 10 changed files with 986 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/post-to-medium/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions tools/post-to-medium/hello-world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
async function run() {
const userId = "153c9e8a4fdeb3740507f08ecb43235618c83250367c37b99460885d02a51c614";
const token = "2fcb4f6bb4adbaf767eae7394a6b4a8823d167e202c6e99140c3b31747291c37e";
const res = await fetch(`https://api.medium.com/v1/users/${userId}/posts`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
body: JSON.stringify({
title: "Hello World",
contentFormat: "markdown",
content: "# Hello World\nTis just a draft",
publishStatus: "draft"
})
});

console.log(await res.json())
}

run();
43 changes: 43 additions & 0 deletions tools/post-to-medium/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fs from "fs";
import fm from "front-matter";

const filepath = process.argv[2];

async function run() {
const postFileData = fs.readFileSync(filepath);

const content = fm(postFileData.toString());

const {
title,
permalink
} = content.attributes as any;

const token =
"2fcb4f6bb4adbaf767eae7394a6b4a8823d167e202c6e99140c3b31747291c37e";

const canonicalUrl = `https://devtails.xyz${permalink}`;
const originallyPostedText = `*Originally published at [https://devtails.xyz](${canonicalUrl}).*`

const res = await fetch(
"https://api.medium.com/v1/publications/8c66334049e3/posts",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
title,
contentFormat: "markdown",
content: `# ${title}\n${originallyPostedText}\n* * *\n${content.body}`,
canonicalUrl,
publishStatus: "draft",
}),
}
);

console.log(await res.json());
}

run();
12 changes: 12 additions & 0 deletions tools/post-to-medium/me.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
async function run() {
const token = "2fcb4f6bb4adbaf767eae7394a6b4a8823d167e202c6e99140c3b31747291c37e";
const res = await fetch("https://api.medium.com/v1/me", {
headers: {
"Authorization": `Bearer ${token}`
}
});

console.log(await res.json())
}

run();
Loading

0 comments on commit 59ac2eb

Please sign in to comment.