@@ -3,27 +3,48 @@ import { sendTopic } from '../utilities/googleapis';
33import {
44 queryPurrfectStreamHashnode ,
55 patchPurrfectPage ,
6+ queryByHashnode ,
7+ getNotionPageMarkdown ,
68} from '../utilities/notion.server' ;
79import { createPublicationStory } from '../utilities/hashnode' ;
810
911const 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
120183export 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) ;
0 commit comments