-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72e1784
commit a77200d
Showing
4 changed files
with
241 additions
and
153 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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); | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
a77200d
There was a problem hiding this comment.
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