Skip to content

Commit a77200d

Browse files
committed
add podcasts
1 parent 72e1784 commit a77200d

File tree

4 files changed

+241
-153
lines changed

4 files changed

+241
-153
lines changed

.github/workflows/syndicate.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ jobs:
3535
- name: Install dependencies
3636
working-directory: ./apps/codingcatdev
3737
run: pnpm i
38+
- name: syndicate:post-dev-to
39+
working-directory: ./apps/codingcatdev/scripts
40+
run: node post-dev-to.js
41+
env:
42+
PRIVATE_DEVTO: ${{ secrets.PRIVATE_DEVTO }}
3843
- name: syndicate:dev-to
3944
working-directory: ./apps/codingcatdev/scripts
40-
run: node dev-to.js
45+
run: node podcast-dev-to.js
4146
env:
4247
PRIVATE_DEVTO: ${{ secrets.PRIVATE_DEVTO }}
4348
- uses: stefanzweifel/git-auto-commit-action@v4
@@ -46,4 +51,4 @@ jobs:
4651
- uses: stefanzweifel/git-auto-commit-action@v4
4752
with:
4853
commit_message: dev-devto updates
49-
branch:
54+
branch: dev
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* You can test this using act
3+
* run act -s PRIVATE_DEVTO=yourapikey
4+
*/
5+
6+
import { Glob } from 'glob';
7+
import matter from 'gray-matter';
8+
import fs from 'fs';
9+
10+
const TYPE = 'podcast';
11+
const BASE = `../src/routes/(content-single)/(non-course)/${TYPE}/`;
12+
const g = new Glob(`${BASE}**/*.md`, {});
13+
14+
const delay = async (ms) => new Promise((res) => setTimeout(res, ms));
15+
const addArticle = async (data) => {
16+
return fetch('https://dev.to/api/articles/', {
17+
method: 'POST',
18+
headers: {
19+
'api-key': process.env.PRIVATE_DEVTO,
20+
'Content-Type': 'application/json'
21+
},
22+
body: JSON.stringify(data)
23+
});
24+
};
25+
26+
for await (const file of g) {
27+
const mdFile = fs.readFileSync(file, { encoding: 'utf8', flag: 'r' });
28+
const { data, content } = await matter(mdFile); // data has frontmatter, code is html
29+
const fm = data;
30+
if (!fm) continue;
31+
// TODO: We might need to add a check on canonical if this page is already in dev.to
32+
if (
33+
fm?.slug &&
34+
fm?.title &&
35+
fm?.cover &&
36+
fm?.published === 'published' &&
37+
new Date(fm?.start) < new Date() &&
38+
!fm?.devto
39+
) {
40+
console.log('Adding', { slug: fm?.slug, devto: fm?.devto });
41+
42+
try {
43+
console.log('addArticle to devto');
44+
const response = await addArticle({
45+
article: {
46+
title: fm.title,
47+
published: true,
48+
tags: ['podcast', 'webdev', 'javascript', 'beginners'],
49+
series: `codingcatdev_podcast_${fm?.season || 4}`,
50+
main_image: fm.cover.replace('upload/', 'upload/b_rgb:5e1186,c_pad,w_1000,h_420/'),
51+
canonical_url: `https://codingcat.dev/${TYPE}/${fm.slug}`,
52+
description: fm?.excerpt || '',
53+
organization_id: '1009',
54+
body_markdown: `Original: https://codingcat.dev/${TYPE}/${fm.slug}
55+
{% youtube ${fm?.youtube} %}
56+
{% spotify spotify:episode:${fm?.spotify.split('/').at(-1).split('?').at(0)} %}
57+
${content}`
58+
}
59+
});
60+
console.log('addArticle result:', response.status);
61+
62+
// Get new devto url and update
63+
if (response.status === 201) {
64+
const json = await response.json();
65+
if (json?.url) {
66+
console.log('Updating', file, { devto: json.url });
67+
const newMdFile = matter.stringify(content, {
68+
...data,
69+
devto: json.url
70+
});
71+
fs.writeFileSync(file, newMdFile, { encoding: 'utf8' });
72+
}
73+
}
74+
// Avoid 429
75+
delay(Integer(process.env.SYNDICATE_DELAY) || 10000);
76+
} catch (error) {
77+
console.error(error);
78+
}
79+
}
80+
}

apps/codingcatdev/scripts/dev-to.js renamed to apps/codingcatdev/scripts/post-dev-to.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const TYPE = 'post';
1111
const BASE = `../src/routes/(content-single)/(non-course)/${TYPE}/`;
1212
const g = new Glob(`${BASE}**/*.md`, {});
1313

14+
const delay = async (ms) => new Promise((res) => setTimeout(res, ms));
1415
const addArticle = async (data) => {
1516
return fetch('https://dev.to/api/articles/', {
1617
method: 'POST',
@@ -66,6 +67,8 @@ for await (const file of g) {
6667
fs.writeFileSync(file, newMdFile, { encoding: 'utf8' });
6768
}
6869
}
70+
// Avoid 429
71+
delay(Integer(process.env.SYNDICATE_DELAY) || 10000);
6972
} catch (error) {
7073
console.error(error);
7174
}

0 commit comments

Comments
 (0)