Skip to content

Commit

Permalink
add podcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
codercatdev committed Dec 18, 2023
1 parent 72e1784 commit a77200d
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 153 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/syndicate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ jobs:
- name: Install dependencies
working-directory: ./apps/codingcatdev
run: pnpm i
- name: syndicate:post-dev-to
working-directory: ./apps/codingcatdev/scripts
run: node post-dev-to.js
env:
PRIVATE_DEVTO: ${{ secrets.PRIVATE_DEVTO }}
- name: syndicate:dev-to
working-directory: ./apps/codingcatdev/scripts
run: node dev-to.js
run: node podcast-dev-to.js
env:
PRIVATE_DEVTO: ${{ secrets.PRIVATE_DEVTO }}
- uses: stefanzweifel/git-auto-commit-action@v4
Expand All @@ -46,4 +51,4 @@ jobs:
- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: dev-devto updates
branch:
branch: dev
80 changes: 80 additions & 0 deletions apps/codingcatdev/scripts/podcast-dev-to.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* You can test this using act
* run act -s PRIVATE_DEVTO=yourapikey
*/

import { Glob } from 'glob';
import matter from 'gray-matter';
import fs from 'fs';

const TYPE = 'podcast';
const BASE = `../src/routes/(content-single)/(non-course)/${TYPE}/`;
const g = new Glob(`${BASE}**/*.md`, {});

const delay = async (ms) => new Promise((res) => setTimeout(res, ms));
const addArticle = async (data) => {
return fetch('https://dev.to/api/articles/', {
method: 'POST',
headers: {
'api-key': process.env.PRIVATE_DEVTO,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
};

for await (const file of g) {
const mdFile = fs.readFileSync(file, { encoding: 'utf8', flag: 'r' });
const { data, content } = await matter(mdFile); // data has frontmatter, code is html
const fm = data;
if (!fm) continue;
// TODO: We might need to add a check on canonical if this page is already in dev.to
if (
fm?.slug &&
fm?.title &&
fm?.cover &&
fm?.published === 'published' &&
new Date(fm?.start) < new Date() &&
!fm?.devto
) {
console.log('Adding', { slug: fm?.slug, devto: fm?.devto });

try {
console.log('addArticle to devto');
const response = await addArticle({
article: {
title: fm.title,
published: true,
tags: ['podcast', 'webdev', 'javascript', 'beginners'],
series: `codingcatdev_podcast_${fm?.season || 4}`,
main_image: fm.cover.replace('upload/', 'upload/b_rgb:5e1186,c_pad,w_1000,h_420/'),
canonical_url: `https://codingcat.dev/${TYPE}/${fm.slug}`,
description: fm?.excerpt || '',
organization_id: '1009',
body_markdown: `Original: https://codingcat.dev/${TYPE}/${fm.slug}
{% youtube ${fm?.youtube} %}
{% spotify spotify:episode:${fm?.spotify.split('/').at(-1).split('?').at(0)} %}
${content}`
}
});
console.log('addArticle result:', response.status);

// Get new devto url and update
if (response.status === 201) {
const json = await response.json();
if (json?.url) {
console.log('Updating', file, { devto: json.url });
const newMdFile = matter.stringify(content, {
...data,
devto: json.url
});
fs.writeFileSync(file, newMdFile, { encoding: 'utf8' });
}
}
// Avoid 429
delay(Integer(process.env.SYNDICATE_DELAY) || 10000);
} catch (error) {
console.error(error);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const TYPE = 'post';
const BASE = `../src/routes/(content-single)/(non-course)/${TYPE}/`;
const g = new Glob(`${BASE}**/*.md`, {});

const delay = async (ms) => new Promise((res) => setTimeout(res, ms));
const addArticle = async (data) => {
return fetch('https://dev.to/api/articles/', {
method: 'POST',
Expand Down Expand Up @@ -66,6 +67,8 @@ for await (const file of g) {
fs.writeFileSync(file, newMdFile, { encoding: 'utf8' });
}
}
// Avoid 429
delay(Integer(process.env.SYNDICATE_DELAY) || 10000);
} catch (error) {
console.error(error);
}
Expand Down
Loading

1 comment on commit a77200d

@vercel
Copy link

@vercel vercel bot commented on a77200d Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

codingcat-dev – ./apps/codingcatdev

codingcat-dev-git-main-coding-cat-dev.vercel.app
codingcat.dev
codingcat-dev-coding-cat-dev.vercel.app

Please sign in to comment.