@@ -10,24 +10,25 @@ import { createPublicationStory } from '../utilities/hashnode';
1010
1111const topicId = 'hashnodeCreateFromNotion' ;
1212
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 ) ) ;
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 ) ;
13+ export const scheduledNotionToHashnode = functions . pubsub
14+ . schedule ( 'every 5 minutes' )
15+ . onRun ( async ( ) => {
16+ // Check to see if ther are scheduled pods
17+ console . log ( 'Checking for scheduled pods' ) ;
18+ const scheduledRes = await queryPurrfectStreamHashnode ( 1 ) ;
19+ console . log ( 'Scheduled Result:' , JSON . stringify ( scheduledRes ) ) ;
20+
21+ if ( scheduledRes ?. results ) {
22+ const needCloudinaryPods = scheduledRes ?. results ;
23+ console . log ( 'Pods to add to pub/sub' , JSON . stringify ( needCloudinaryPods ) ) ;
24+
25+ for ( const pod of needCloudinaryPods ) {
26+ await sendTopic ( topicId , pod ) ;
27+ }
2528 }
26- }
2729
28- for ( const _type of [ 'post' , 'tutorial' ] ) {
2930 console . log ( 'Checking for hashnode missing' ) ;
30- const posts = await queryByHashnode ( _type , 1 ) ;
31+ const posts = await queryByHashnode ( 'post' , 1 ) ;
3132 console . log ( 'Posts:' , JSON . stringify ( posts ) ) ;
3233
3334 if ( posts ?. results ) {
@@ -38,13 +39,7 @@ const scheduleCheck = async () => {
3839 await sendTopic ( topicId , p ) ;
3940 }
4041 }
41- }
42- } ;
4342
44- export const scheduledNotionToHashnode = functions . pubsub
45- . schedule ( 'every 5 minutes' )
46- . onRun ( async ( ) => {
47- await scheduleCheck ( ) ;
4843 return true ;
4944 } ) ;
5045
@@ -57,73 +52,18 @@ export const hashnodeToNotionPubSub = functions.pubsub
5752 console . log ( 'page' , page ) ;
5853
5954 let input ;
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 }
66-
67- %[${ page . properties . youtube . url } ]
68-
69- %[${ page . properties . spotify . url } ]
70- ` ,
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- if ( post && post ?. content ) {
110- if ( page ?. properties ?. spotify ?. url ) {
111- post . content = `
112- %[${ page . properties . spotify . url } ]
113-
114- ${ post . content } `;
115- }
116- if ( page ?. properties ?. youtube ?. url ) {
117- post . content = `
118- %[${ page . properties . youtube . url } ]
119-
120- ${ post . content } `;
55+ switch ( page . _type ) {
56+ case 'post' :
57+ const post = await getNotionPageMarkdown ( {
58+ _type : page . _type ,
59+ slug : page ?. properties ?. slug ?. url ,
60+ preview : false ,
61+ } ) ;
62+
63+ if ( ! post || ! post . content ) {
64+ break ;
12165 }
122- post . content = `
123- Original: https://codingcat.dev/${ page . _type } /${ page . slug }
12466
125- ${ post . content }
126- ` ;
12767 input = {
12868 title : page . title ,
12969 subtitle : page . excerpt ,
@@ -133,6 +73,40 @@ ${post.content}
13373 isRepublished : {
13474 originalArticleURL : `https://codingcat.dev/${ page . _type } /${ page . slug } ` ,
13575 } ,
76+ tags : [
77+ {
78+ _id : '56744721958ef13879b94cad' ,
79+ name : 'JavaScript' ,
80+ slug : 'javascript' ,
81+ } ,
82+ {
83+ _id : '56744722958ef13879b94f1b' ,
84+ name : 'Web Development' ,
85+ slug : 'web-development' ,
86+ } ,
87+ {
88+ _id : '56744723958ef13879b955a9' ,
89+ name : 'Beginner Developers' ,
90+ slug : 'beginners' ,
91+ } ,
92+ ] ,
93+ } ;
94+ break ;
95+ case 'podcast' :
96+ input = {
97+ title : page . title ,
98+ subtitle : page . excerpt ,
99+ slug : `${ page . _type } -${ page . slug } ` ,
100+ contentMarkdown : `Original: https://codingcat.dev/${ page . _type } /${ page . slug }
101+
102+ %[${ page . properties . youtube . url } ]
103+
104+ %[${ page . properties . spotify . url } ]
105+ ` ,
106+ coverImageURL : `https://media.codingcat.dev/image/upload/f_auto,c_limit,w_1920,q_auto/${ page ?. coverPhoto ?. public_id } ` ,
107+ isRepublished : {
108+ originalArticleURL : `https://codingcat.dev/${ page . _type } /${ page . slug } ` ,
109+ } ,
136110 tags : [
137111 {
138112 _id : '56744722958ef13879b950d3' ,
@@ -156,7 +130,9 @@ ${post.content}
156130 } ,
157131 ] ,
158132 } ;
159- }
133+ break ;
134+ default :
135+ break ;
160136 }
161137
162138 if ( input ) {
@@ -191,15 +167,26 @@ ${post.content}
191167 return purrfectPagePatchRes ;
192168 } else {
193169 console . log ( 'No Data matched for article' ) ;
170+ return ;
194171 }
195- return ;
196172 } ) ;
197173
198174// Used for testing don't forget to remove for production
199175export const httpNotionToHashnode = functions . https . onRequest (
200176 async ( req , res ) => {
201- await scheduleCheck ( ) ;
177+ // Check to see if ther are scheduled pods
178+ console . log ( 'Checking for scheduled pods' ) ;
179+ const scheduledRes = await queryPurrfectStreamHashnode ( 1 ) ;
180+ console . log ( 'Scheduled Result:' , JSON . stringify ( scheduledRes ) ) ;
181+
182+ if ( scheduledRes ?. results ) {
183+ const needCloudinaryPods = scheduledRes ?. results ;
184+ console . log ( 'Pods to add to pub/sub' , JSON . stringify ( needCloudinaryPods ) ) ;
202185
186+ for ( const pod of needCloudinaryPods ) {
187+ await sendTopic ( topicId , pod ) ;
188+ }
189+ }
203190 res . send ( { msg : 'started' } ) ;
204191 }
205192) ;
0 commit comments