1
- fs = require ( "fs" ) ;
1
+ const feed2json = require ( "feed2json" ) ;
2
+ const fs = require ( "fs" ) ;
2
3
const https = require ( "https" ) ;
3
- process = require ( "process" ) ;
4
+ const process = require ( "process" ) ;
4
5
require ( "dotenv" ) . config ( ) ;
5
6
6
7
const GITHUB_TOKEN = process . env . REACT_APP_GITHUB_TOKEN ;
@@ -16,6 +17,49 @@ const ERR = {
16
17
requestFailedMedium :
17
18
"The request to Medium didn't succeed. Check if Medium username in your .env file is correct."
18
19
} ;
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
+
19
63
if ( USE_GITHUB_DATA === "true" ) {
20
64
if ( GITHUB_USERNAME === undefined ) {
21
65
throw new Error ( ERR . noUserName ) ;
@@ -67,64 +111,44 @@ if (USE_GITHUB_DATA === "true") {
67
111
}
68
112
} ;
69
113
70
- const req = https . request ( default_options , res => {
71
- let data = "" ;
72
-
73
- console . log ( `statusCode: ${ res . statusCode } ` ) ;
74
- if ( res . statusCode !== 200 ) {
75
- throw new Error ( ERR . requestFailed ) ;
76
- }
77
-
78
- res . on ( "data" , d => {
79
- data += d ;
80
- } ) ;
81
- res . on ( "end" , ( ) => {
82
- 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 ) {
83
117
if ( err ) return console . log ( err ) ;
84
118
console . log ( "saved file to public/profile.json" ) ;
85
119
} ) ;
120
+ } )
121
+ . catch ( error => {
122
+ console . error ( "Error:" , error ) ;
86
123
} ) ;
87
- } ) ;
88
-
89
- req . on ( "error" , error => {
90
- throw error ;
91
- } ) ;
92
-
93
- req . write ( data ) ;
94
- req . end ( ) ;
95
124
}
96
125
97
126
if ( MEDIUM_USERNAME !== undefined ) {
98
127
console . log ( `Fetching Medium blogs data for ${ MEDIUM_USERNAME } ` ) ;
128
+ const url = `https://medium.com/feed/@${ MEDIUM_USERNAME } ` ;
99
129
const options = {
100
- hostname : "api.rss2json .com" ,
101
- path : `/v1/api.json?rss_url=https://medium.com/ feed/@${ MEDIUM_USERNAME } ` ,
130
+ hostname : "medium .com" ,
131
+ path : `/feed/@${ MEDIUM_USERNAME } ` ,
102
132
port : 443 ,
103
133
method : "GET"
104
134
} ;
105
135
106
- const req = https . request ( options , res => {
107
- let mediumData = "" ;
108
-
109
- console . log ( `statusCode: ${ res . statusCode } ` ) ;
110
- if ( res . statusCode !== 200 ) {
111
- throw new Error ( ERR . requestMediumFailed ) ;
112
- }
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
+ }
113
143
114
- res . on ( "data" , d => {
115
- mediumData += d ;
116
- } ) ;
117
- res . on ( "end" , ( ) => {
118
- fs . writeFile ( "./public/blogs.json" , mediumData , function ( err ) {
119
- if ( err ) return console . log ( err ) ;
120
- 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
+ } ) ;
121
149
} ) ;
150
+ } )
151
+ . catch ( error => {
152
+ console . error ( "Error:" , error ) ;
122
153
} ) ;
123
- } ) ;
124
-
125
- req . on ( "error" , error => {
126
- throw error ;
127
- } ) ;
128
-
129
- req . end ( ) ;
130
- }
154
+ }
0 commit comments