|
| 1 | +import * as functions from 'firebase-functions'; |
| 2 | +import { sendTopic } from '../utilities/googleapis'; |
| 3 | +import { |
| 4 | + queryPurrfectStreamDevTo, |
| 5 | + patchPurrfectPage, |
| 6 | +} from '../utilities/notion.server'; |
| 7 | +import { addArticle } from '../utilities/devto'; |
| 8 | + |
| 9 | +const topicId = 'devtoCreateFromNotion'; |
| 10 | + |
| 11 | +export const scheduledNotionToDevto = functions.pubsub |
| 12 | + .schedule('every 5 minutes') |
| 13 | + .onRun(async () => { |
| 14 | + // Check to see if ther are scheduled pods |
| 15 | + console.log('Checking for scheduled pods'); |
| 16 | + const scheduledRes = await queryPurrfectStreamDevTo(1); |
| 17 | + console.log('Scheduled Result:', JSON.stringify(scheduledRes)); |
| 18 | + |
| 19 | + if (scheduledRes?.results) { |
| 20 | + const needCloudinaryPods = scheduledRes?.results; |
| 21 | + console.log('Pods to add to pub/sub', JSON.stringify(needCloudinaryPods)); |
| 22 | + |
| 23 | + for (const pod of needCloudinaryPods) { |
| 24 | + await sendTopic(topicId, pod); |
| 25 | + } |
| 26 | + } |
| 27 | + return true; |
| 28 | + }); |
| 29 | + |
| 30 | +export const devtoToNotionPubSub = functions.pubsub |
| 31 | + .topic(topicId) |
| 32 | + .onPublish(async (message, context) => { |
| 33 | + console.log('The function was triggered at ', context.timestamp); |
| 34 | + console.log('The unique ID for the event is', context.eventId); |
| 35 | + const page = JSON.parse(JSON.stringify(message.json)); |
| 36 | + console.log('page', page); |
| 37 | + |
| 38 | + let data; |
| 39 | + switch (page._type) { |
| 40 | + case 'podcast': |
| 41 | + data = { |
| 42 | + article: { |
| 43 | + title: page.title, |
| 44 | + published: true, |
| 45 | + tags: ['podcast', 'webdev', 'javascript', 'beginners'], |
| 46 | + series: `codingcatdev_podcast_${page.properties.Season.number}`, |
| 47 | + main_image: `https://media.codingcat.dev/image/upload/b_rgb:5e1186,c_pad,w_1000,h_420/${page?.coverPhoto?.public_id}`, |
| 48 | + canonical_url: `https://codingcat.dev/${page._type}/${page.slug}`, |
| 49 | + description: page.excerpt, |
| 50 | + organization_id: '1009', |
| 51 | + body_markdown: `Original: https://codingcat.dev/${page._type}/${ |
| 52 | + page.slug |
| 53 | + } |
| 54 | + {% youtube ${page.properties.youtube.url} %} |
| 55 | + {% spotify spotify:episode:${page.properties.spotify.url |
| 56 | + .split('/') |
| 57 | + .at(-1) |
| 58 | + .split('?') |
| 59 | + .at(0)} %} |
| 60 | + |
| 61 | + `, |
| 62 | + }, |
| 63 | + }; |
| 64 | + break; |
| 65 | + default: |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + if (data) { |
| 70 | + const response = await addArticle(data); |
| 71 | + console.log('addArticle result:', response); |
| 72 | + |
| 73 | + const devto = response?.data?.url; |
| 74 | + |
| 75 | + if (!devto) { |
| 76 | + console.log('devto url missing'); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const update = { |
| 81 | + page_id: page.id, |
| 82 | + properties: { |
| 83 | + devto: { |
| 84 | + id: 'remote', |
| 85 | + type: 'url', |
| 86 | + url: devto, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }; |
| 90 | + console.log('Updating page with: ', JSON.stringify(update)); |
| 91 | + const purrfectPagePatchRes = await patchPurrfectPage(update); |
| 92 | + console.log('Page update result:', JSON.stringify(purrfectPagePatchRes)); |
| 93 | + |
| 94 | + return purrfectPagePatchRes; |
| 95 | + } else { |
| 96 | + console.log('No Data matched for article'); |
| 97 | + return; |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | +// Used for testing don't forget to remove for production |
| 102 | +export const httpNotionToDevto = functions.https.onRequest(async (req, res) => { |
| 103 | + // Check to see if ther are scheduled pods |
| 104 | + console.log('Checking for scheduled pods'); |
| 105 | + const scheduledRes = await queryPurrfectStreamDevTo(1); |
| 106 | + console.log('Scheduled Result:', JSON.stringify(scheduledRes)); |
| 107 | + |
| 108 | + if (scheduledRes?.results) { |
| 109 | + const needCloudinaryPods = scheduledRes?.results; |
| 110 | + console.log('Pods to add to pub/sub', JSON.stringify(needCloudinaryPods)); |
| 111 | + |
| 112 | + for (const pod of needCloudinaryPods) { |
| 113 | + await sendTopic(topicId, pod); |
| 114 | + } |
| 115 | + } |
| 116 | + res.send({ msg: 'started' }); |
| 117 | +}); |
0 commit comments