@@ -10,25 +10,24 @@ import { addArticle } from '../utilities/devto';
1010
1111const topicId = 'devtoCreateFromNotion' ;
1212
13- export const scheduledNotionToDevto = 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 queryPurrfectStreamDevTo ( 1 ) ;
19- 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 queryPurrfectStreamDevTo ( 1 ) ;
17+ console . log ( 'Scheduled Result:' , JSON . stringify ( scheduledRes ) ) ;
2018
21- if ( scheduledRes ?. results ) {
22- const needCloudinaryPods = scheduledRes ?. results ;
23- 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 ) ) ;
2422
25- for ( const pod of needCloudinaryPods ) {
26- await sendTopic ( topicId , pod ) ;
27- }
23+ for ( const pod of needCloudinaryPods ) {
24+ await sendTopic ( topicId , pod ) ;
2825 }
26+ }
2927
28+ for ( const _type of [ 'post' , 'tutorial' ] ) {
3029 console . log ( 'Checking for devto missing' ) ;
31- const posts = await queryByDevto ( 'post' , 1 ) ;
30+ const posts = await queryByDevto ( _type , 1 ) ;
3231 console . log ( 'Posts:' , JSON . stringify ( posts ) ) ;
3332
3433 if ( posts ?. results ) {
@@ -39,7 +38,13 @@ export const scheduledNotionToDevto = functions.pubsub
3938 await sendTopic ( topicId , p ) ;
4039 }
4140 }
41+ }
42+ } ;
4243
44+ export const scheduledNotionToDevto = functions . pubsub
45+ . schedule ( 'every 5 minutes' )
46+ . onRun ( async ( ) => {
47+ await scheduleCheck ( ) ;
4348 return true ;
4449 } ) ;
4550
@@ -52,18 +57,43 @@ export const devtoToNotionPubSub = functions.pubsub
5257 console . log ( 'page' , page ) ;
5358
5459 let data ;
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 ;
65- }
66-
60+ if ( page . _type === 'podcast' ) {
61+ data = {
62+ article : {
63+ title : page . title ,
64+ published : true ,
65+ tags : [ 'podcast' , 'webdev' , 'javascript' , 'beginners' ] ,
66+ series : `codingcatdev_podcast_${ page . properties . Season . number } ` ,
67+ main_image : `https://media.codingcat.dev/image/upload/b_rgb:5e1186,c_pad,w_1000,h_420/${ page ?. coverPhoto ?. public_id } ` ,
68+ canonical_url : `https://codingcat.dev/${ page . _type } /${ page . slug } ` ,
69+ description : page . excerpt ,
70+ organization_id : '1009' ,
71+ body_markdown : `Original: https://codingcat.dev/${ page . _type } /${
72+ page . slug
73+ }
74+ {% youtube ${ page . properties . youtube . url } %}
75+ {% spotify spotify:episode:${ page . properties . spotify . url
76+ . split ( '/' )
77+ . at ( - 1 )
78+ . split ( '?' )
79+ . at ( 0 ) } %}
80+
81+ ` ,
82+ } ,
83+ } ;
84+ } else {
85+ console . log (
86+ `Getting ${ page . _type } : ${ page . id } markdown, with slug ${ page ?. properties ?. slug ?. url } `
87+ ) ;
88+ const post = await getNotionPageMarkdown ( {
89+ _type : page . _type ,
90+ slug : page ?. properties ?. slug ?. url ,
91+ preview : false ,
92+ } ) ;
93+
94+ console . log ( 'Block Result' , post ) ;
95+
96+ if ( post && post ?. content ) {
6797 data = {
6898 article : {
6999 title : page . title ,
@@ -76,62 +106,43 @@ export const devtoToNotionPubSub = functions.pubsub
76106 body_markdown : post . content ,
77107 } ,
78108 } ;
79- break ;
80- case 'podcast' :
81- data = {
82- article : {
83- title : page . title ,
84- published : true ,
85- tags : [ 'podcast' , 'webdev' , 'javascript' , 'beginners' ] ,
86- series : `codingcatdev_podcast_${ page . properties . Season . number } ` ,
87- main_image : `https://media.codingcat.dev/image/upload/b_rgb:5e1186,c_pad,w_1000,h_420/${ page ?. coverPhoto ?. public_id } ` ,
88- canonical_url : `https://codingcat.dev/${ page . _type } /${ page . slug } ` ,
89- description : page . excerpt ,
90- organization_id : '1009' ,
91- body_markdown : `Original: https://codingcat.dev/${ page . _type } /${
92- page . slug
93- }
94- {% youtube ${ page . properties . youtube . url } %}
95- {% spotify spotify:episode:${ page . properties . spotify . url
96- . split ( '/' )
97- . at ( - 1 )
98- . split ( '?' )
99- . at ( 0 ) } %}
100-
101- ` ,
102- } ,
103- } ;
104- break ;
105- default :
106- break ;
109+ }
107110 }
108111
109112 if ( data ) {
110- const response = await addArticle ( data ) ;
111- console . log ( 'addArticle result:' , response ) ;
113+ try {
114+ console . log ( 'addArticle to devto' ) ;
115+ const response = await addArticle ( data ) ;
116+ console . log ( 'addArticle result:' , response ) ;
112117
113- const devto = response ?. data ?. url ;
118+ const devto = response ?. data ?. url ;
114119
115- if ( ! devto ) {
116- console . log ( 'devto url missing' ) ;
117- return ;
118- }
120+ if ( ! devto ) {
121+ console . log ( 'devto url missing' ) ;
122+ return ;
123+ }
119124
120- const update = {
121- page_id : page . id ,
122- properties : {
123- devto : {
124- id : 'remote' ,
125- type : 'url' ,
126- url : devto ,
125+ const update = {
126+ page_id : page . id ,
127+ properties : {
128+ devto : {
129+ id : 'remote' ,
130+ type : 'url' ,
131+ url : devto ,
132+ } ,
127133 } ,
128- } ,
129- } ;
130- console . log ( 'Updating page with: ' , JSON . stringify ( update ) ) ;
131- const purrfectPagePatchRes = await patchPurrfectPage ( update ) ;
132- console . log ( 'Page update result:' , JSON . stringify ( purrfectPagePatchRes ) ) ;
133-
134- return purrfectPagePatchRes ;
134+ } ;
135+ console . log ( 'Updating page with: ' , JSON . stringify ( update ) ) ;
136+ const purrfectPagePatchRes = await patchPurrfectPage ( update ) ;
137+ console . log (
138+ 'Page update result:' ,
139+ JSON . stringify ( purrfectPagePatchRes )
140+ ) ;
141+
142+ return purrfectPagePatchRes ;
143+ } catch ( error ) {
144+ console . error ( error ) ;
145+ }
135146 } else {
136147 console . error ( 'No Data matched for article' ) ;
137148 return ;
@@ -140,31 +151,7 @@ export const devtoToNotionPubSub = functions.pubsub
140151
141152// Used for testing don't forget to remove for production
142153export const httpNotionToDevto = functions . https . onRequest ( async ( req , res ) => {
143- // Check to see if ther are scheduled pods
144- console . log ( 'Checking for scheduled pods' ) ;
145- const scheduledRes = await queryPurrfectStreamDevTo ( 1 ) ;
146- console . log ( 'Scheduled Result:' , JSON . stringify ( scheduledRes ) ) ;
147-
148- if ( scheduledRes ?. results ) {
149- const needCloudinaryPods = scheduledRes ?. results ;
150- console . log ( 'Pods to add to pub/sub' , JSON . stringify ( needCloudinaryPods ) ) ;
151-
152- for ( const pod of needCloudinaryPods ) {
153- await sendTopic ( topicId , pod ) ;
154- }
155- }
156-
157- console . log ( 'Checking for devto missing' ) ;
158- const posts = await queryByDevto ( 'post' , 1 ) ;
159- console . log ( 'Posts:' , JSON . stringify ( posts ) ) ;
154+ await scheduleCheck ( ) ;
160155
161- if ( posts ?. results ) {
162- const needposts = posts ?. results ;
163- console . log ( 'Posts to add to pub/sub' , JSON . stringify ( needposts ) ) ;
164-
165- for ( const p of needposts ) {
166- await sendTopic ( topicId , p ) ;
167- }
168- }
169156 res . send ( { msg : 'started' } ) ;
170157} ) ;
0 commit comments