Skip to content

Commit 773b426

Browse files
committed
add hasnode post/tutorials
1 parent 983f24e commit 773b426

4 files changed

Lines changed: 140 additions & 37 deletions

File tree

‎backend/firebase/functions/src/devto/scheduledNotionToDevto.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ export const devtoToNotionPubSub = functions.pubsub
145145
}
146146
} else {
147147
console.error('No Data matched for article');
148-
return;
149148
}
149+
return;
150150
});
151151

152152
// Used for testing don't forget to remove for production

‎backend/firebase/functions/src/hashnode/scheduledNotionToHashNode.ts‎

Lines changed: 87 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,48 @@ import { sendTopic } from '../utilities/googleapis';
33
import {
44
queryPurrfectStreamHashnode,
55
patchPurrfectPage,
6+
queryByHashnode,
7+
getNotionPageMarkdown,
68
} from '../utilities/notion.server';
79
import { createPublicationStory } from '../utilities/hashnode';
810

911
const topicId = 'hashnodeCreateFromNotion';
1012

11-
export const scheduledNotionToHashnode = 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 queryPurrfectStreamHashnode(1);
17-
console.log('Scheduled Result:', JSON.stringify(scheduledRes));
13+
const scheduleCheck = async () => {
14+
// Check to see if ther are scheduled pods
15+
console.log('Checking for scheduled pods');
16+
const scheduledRes = await queryPurrfectStreamHashnode(1);
17+
console.log('Scheduled Result:', JSON.stringify(scheduledRes));
1818

19-
if (scheduledRes?.results) {
20-
const needCloudinaryPods = scheduledRes?.results;
21-
console.log('Pods to add to pub/sub', JSON.stringify(needCloudinaryPods));
19+
if (scheduledRes?.results) {
20+
const needCloudinaryPods = scheduledRes?.results;
21+
console.log('Pods to add to pub/sub', JSON.stringify(needCloudinaryPods));
2222

23-
for (const pod of needCloudinaryPods) {
24-
await sendTopic(topicId, pod);
23+
for (const pod of needCloudinaryPods) {
24+
await sendTopic(topicId, pod);
25+
}
26+
}
27+
28+
for (const _type of ['post', 'tutorial']) {
29+
console.log('Checking for hashnode missing');
30+
const posts = await queryByHashnode(_type, 1);
31+
console.log('Posts:', JSON.stringify(posts));
32+
33+
if (posts?.results) {
34+
const needposts = posts?.results;
35+
console.log('Posts to add to pub/sub', JSON.stringify(needposts));
36+
37+
for (const p of needposts) {
38+
await sendTopic(topicId, p);
2539
}
2640
}
41+
}
42+
};
43+
44+
export const scheduledNotionToHashnode = functions.pubsub
45+
.schedule('every 5 minutes')
46+
.onRun(async () => {
47+
await scheduleCheck();
2748
return true;
2849
});
2950

@@ -36,18 +57,62 @@ export const hashnodeToNotionPubSub = functions.pubsub
3657
console.log('page', page);
3758

3859
let input;
39-
switch (page._type) {
40-
case 'podcast':
41-
input = {
42-
title: page.title,
43-
subtitle: page.excerpt,
44-
slug: `${page._type}-${page.slug}`,
45-
contentMarkdown: `Original: https://codingcat.dev/${page._type}/${page.slug}
60+
if (page._type === 'podcast') {
61+
input = {
62+
title: page.title,
63+
subtitle: page.excerpt,
64+
slug: `${page._type}-${page.slug}`,
65+
contentMarkdown: `Original: https://codingcat.dev/${page._type}/${page.slug}
4666
4767
%[${page.properties.youtube.url}]
4868
4969
%[${page.properties.spotify.url}]
5070
`,
71+
coverImageURL: `https://media.codingcat.dev/image/upload/f_auto,c_limit,w_1920,q_auto/${page?.coverPhoto?.public_id}`,
72+
isRepublished: {
73+
originalArticleURL: `https://codingcat.dev/${page._type}/${page.slug}`,
74+
},
75+
tags: [
76+
{
77+
_id: '56744722958ef13879b950d3',
78+
name: 'podcast',
79+
slug: 'podcast',
80+
},
81+
{
82+
_id: '56744721958ef13879b94cad',
83+
name: 'JavaScript',
84+
slug: 'javascript',
85+
},
86+
{
87+
_id: '56744722958ef13879b94f1b',
88+
name: 'Web Development',
89+
slug: 'web-development',
90+
},
91+
{
92+
_id: '56744723958ef13879b955a9',
93+
name: 'Beginner Developers',
94+
slug: 'beginners',
95+
},
96+
],
97+
};
98+
} else {
99+
console.log(
100+
`Getting ${page._type}: ${page.id} markdown, with slug ${page?.properties?.slug?.url}`
101+
);
102+
const post = await getNotionPageMarkdown({
103+
_type: page._type,
104+
slug: page?.properties?.slug?.url,
105+
preview: false,
106+
});
107+
108+
console.log('Block Result', post);
109+
110+
if (post && post?.content) {
111+
input = {
112+
title: page.title,
113+
subtitle: page.excerpt,
114+
slug: `${page._type}-${page.slug}`,
115+
contentMarkdown: post.content,
51116
coverImageURL: `https://media.codingcat.dev/image/upload/f_auto,c_limit,w_1920,q_auto/${page?.coverPhoto?.public_id}`,
52117
isRepublished: {
53118
originalArticleURL: `https://codingcat.dev/${page._type}/${page.slug}`,
@@ -75,9 +140,7 @@ export const hashnodeToNotionPubSub = functions.pubsub
75140
},
76141
],
77142
};
78-
break;
79-
default:
80-
break;
143+
}
81144
}
82145

83146
if (input) {
@@ -112,26 +175,15 @@ export const hashnodeToNotionPubSub = functions.pubsub
112175
return purrfectPagePatchRes;
113176
} else {
114177
console.log('No Data matched for article');
115-
return;
116178
}
179+
return;
117180
});
118181

119182
// Used for testing don't forget to remove for production
120183
export const httpNotionToHashnode = functions.https.onRequest(
121184
async (req, res) => {
122-
// Check to see if ther are scheduled pods
123-
console.log('Checking for scheduled pods');
124-
const scheduledRes = await queryPurrfectStreamHashnode(1);
125-
console.log('Scheduled Result:', JSON.stringify(scheduledRes));
126-
127-
if (scheduledRes?.results) {
128-
const needCloudinaryPods = scheduledRes?.results;
129-
console.log('Pods to add to pub/sub', JSON.stringify(needCloudinaryPods));
185+
await scheduleCheck();
130186

131-
for (const pod of needCloudinaryPods) {
132-
await sendTopic(topicId, pod);
133-
}
134-
}
135187
res.send({ msg: 'started' });
136188
}
137189
);

‎backend/firebase/functions/src/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ export {
3535
httpNotionToDevto,
3636
} from './devto/scheduledNotionToDevto';
3737

38-
// Notion to Devto
38+
// Notion to Hasnode
3939
export {
4040
scheduledNotionToHashnode,
4141
hashnodeToNotionPubSub,
42+
httpNotionToHashnode,
4243
} from './hashnode/scheduledNotionToHashNode';

‎backend/firebase/functions/src/utilities/notion.server.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,56 @@ export const queryByDevto = async (
473473
return await formatPosts(raw, _type, false, true);
474474
};
475475

476+
export const queryByHashnode = async (
477+
_type: string,
478+
page_size?: number,
479+
start_cursor?: string | null
480+
) => {
481+
const sorts: any = [
482+
{
483+
property: 'start',
484+
direction: 'ascending',
485+
},
486+
];
487+
const filter: any = {
488+
and: [
489+
{
490+
property: 'slug',
491+
url: {
492+
is_not_empty: true,
493+
},
494+
},
495+
{
496+
property: 'published',
497+
select: {
498+
equals: 'published',
499+
},
500+
},
501+
{
502+
property: 'hashnode',
503+
url: {
504+
is_empty: true,
505+
},
506+
},
507+
{
508+
property: 'start',
509+
date: {
510+
is_not_empty: true,
511+
},
512+
},
513+
],
514+
};
515+
516+
const raw = await notionClient.databases.query({
517+
database_id: getNotionDbByType(_type),
518+
start_cursor: start_cursor ? start_cursor : undefined,
519+
page_size,
520+
filter,
521+
sorts,
522+
});
523+
return await formatPosts(raw, _type, false, true);
524+
};
525+
476526
export const queryAll = async (
477527
_type: string,
478528
page_size?: number,

0 commit comments

Comments
 (0)