|
| 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 | +} |
0 commit comments