@@ -18,6 +18,48 @@ const ERR = {
18
18
"The request to Medium didn't succeed. Check if Medium username in your .env file is correct."
19
19
} ;
20
20
21
+ async function fetchWithRetry ( options , data , retries = 5 , delay = 5000 ) {
22
+ for ( let i = 0 ; i < retries ; i ++ ) {
23
+ try {
24
+ return await new Promise ( ( resolve , reject ) => {
25
+ const req = https . request ( options , res => {
26
+ let responseData = "" ;
27
+
28
+ res . on ( "data" , d => {
29
+ responseData += d ;
30
+ } ) ;
31
+
32
+ res . on ( "end" , ( ) => {
33
+ if ( res . statusCode === 200 ) {
34
+ resolve ( responseData ) ;
35
+ } else if ( res . statusCode === 429 && i < retries - 1 ) {
36
+ console . log ( `Rate limited. Retrying in ${ delay } ms...` ) ;
37
+ setTimeout ( ( ) => resolve ( fetchWithRetry ( options , data , retries - 1 , delay ) ) , delay ) ;
38
+ } else {
39
+ reject ( new Error ( `${ options . path } request failed with status code: ${ res . statusCode } ` ) ) ;
40
+ }
41
+ } ) ;
42
+ } ) ;
43
+
44
+ req . on ( "error" , error => {
45
+ reject ( error ) ;
46
+ } ) ;
47
+
48
+ if ( data ) {
49
+ req . write ( data ) ;
50
+ }
51
+ req . end ( ) ;
52
+ } ) ;
53
+ } catch ( error ) {
54
+ if ( i === retries - 1 ) {
55
+ throw error ;
56
+ }
57
+ console . log ( `Error fetching data. Retrying in ${ delay } ms...` ) ;
58
+ await new Promise ( resolve => setTimeout ( resolve , delay ) ) ;
59
+ }
60
+ }
61
+ }
62
+
21
63
if ( USE_GITHUB_DATA === "true" ) {
22
64
if ( GITHUB_USERNAME === undefined ) {
23
65
throw new Error ( ERR . noUserName ) ;
@@ -69,31 +111,16 @@ if (USE_GITHUB_DATA === "true") {
69
111
}
70
112
} ;
71
113
72
- const req = https . request ( default_options , res => {
73
- let data = "" ;
74
-
75
- console . log ( `statusCode: ${ res . statusCode } ` ) ;
76
- if ( res . statusCode !== 200 ) {
77
- throw new Error ( ERR . requestFailed ) ;
78
- }
79
-
80
- res . on ( "data" , d => {
81
- data += d ;
82
- } ) ;
83
- res . on ( "end" , ( ) => {
84
- fs . writeFile ( "./public/profile.json" , data , function ( err ) {
114
+ fetchWithRetry ( default_options , data )
115
+ . then ( responseData => {
116
+ fs . writeFile ( "./public/profile.json" , responseData , function ( err ) {
85
117
if ( err ) return console . log ( err ) ;
86
118
console . log ( "saved file to public/profile.json" ) ;
87
119
} ) ;
120
+ } )
121
+ . catch ( error => {
122
+ console . error ( "Error:" , error ) ;
88
123
} ) ;
89
- } ) ;
90
-
91
- req . on ( "error" , error => {
92
- throw error ;
93
- } ) ;
94
-
95
- req . write ( data ) ;
96
- req . end ( ) ;
97
124
}
98
125
99
126
if ( MEDIUM_USERNAME !== undefined ) {
@@ -106,29 +133,22 @@ if (MEDIUM_USERNAME !== undefined) {
106
133
method : "GET"
107
134
} ;
108
135
109
- const req = https . request ( options , res => {
110
- console . log ( `statusCode: ${ res . statusCode } ` ) ;
111
- if ( res . statusCode !== 200 ) {
112
- throw new Error ( ERR . requestMediumFailed ) ;
113
- }
114
-
115
- feed2json . fromStream ( res , url , { } , ( err , json ) => {
116
- if ( err ) {
117
- console . error ( "Error converting feed to JSON:" , err ) ;
118
- return ;
119
- }
136
+ fetchWithRetry ( options )
137
+ . then ( responseData => {
138
+ feed2json . fromString ( responseData , url , { } , ( err , json ) => {
139
+ if ( err ) {
140
+ console . error ( "Error converting feed to JSON:" , err ) ;
141
+ return ;
142
+ }
120
143
121
- const mediumData = JSON . stringify ( json , null , 2 ) ;
122
- fs . writeFile ( "./public/blogs.json" , mediumData , function ( err ) {
123
- if ( err ) return console . error ( err ) ;
124
- console . log ( "Saved file to public/blogs.json" ) ;
144
+ const mediumData = JSON . stringify ( json , null , 2 ) ;
145
+ fs . writeFile ( "./public/blogs.json" , mediumData , function ( err ) {
146
+ if ( err ) return console . error ( err ) ;
147
+ console . log ( "Saved file to public/blogs.json" ) ;
148
+ } ) ;
125
149
} ) ;
150
+ } )
151
+ . catch ( error => {
152
+ console . error ( "Error:" , error ) ;
126
153
} ) ;
127
- } ) ;
128
-
129
- req . on ( "error" , error => {
130
- throw error ;
131
- } ) ;
132
-
133
- req . end ( ) ;
134
- }
154
+ }
0 commit comments