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,7 @@ 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
+
19
21
if ( USE_GITHUB_DATA === "true" ) {
20
22
if ( GITHUB_USERNAME === undefined ) {
21
23
throw new Error ( ERR . noUserName ) ;
@@ -96,28 +98,30 @@ if (USE_GITHUB_DATA === "true") {
96
98
97
99
if ( MEDIUM_USERNAME !== undefined ) {
98
100
console . log ( `Fetching Medium blogs data for ${ MEDIUM_USERNAME } ` ) ;
101
+ const url = `https://medium.com/feed/@${ MEDIUM_USERNAME } ` ;
99
102
const options = {
100
- hostname : "api.rss2json .com" ,
101
- path : `/v1/api.json?rss_url=https://medium.com/ feed/@${ MEDIUM_USERNAME } ` ,
103
+ hostname : "medium .com" ,
104
+ path : `/feed/@${ MEDIUM_USERNAME } ` ,
102
105
port : 443 ,
103
106
method : "GET"
104
107
} ;
105
108
106
109
const req = https . request ( options , res => {
107
- let mediumData = "" ;
108
-
109
110
console . log ( `statusCode: ${ res . statusCode } ` ) ;
110
111
if ( res . statusCode !== 200 ) {
111
112
throw new Error ( ERR . requestMediumFailed ) ;
112
113
}
113
114
114
- res . on ( "data" , d => {
115
- mediumData += d ;
116
- } ) ;
117
- res . on ( "end" , ( ) => {
115
+ feed2json . fromStream ( res , url , { } , ( err , json ) => {
116
+ if ( err ) {
117
+ console . error ( "Error converting feed to JSON:" , err ) ;
118
+ return ;
119
+ }
120
+
121
+ const mediumData = JSON . stringify ( json , null , 2 ) ;
118
122
fs . writeFile ( "./public/blogs.json" , mediumData , function ( err ) {
119
- if ( err ) return console . log ( err ) ;
120
- console . log ( "saved file to public/blogs.json" ) ;
123
+ if ( err ) return console . error ( err ) ;
124
+ console . log ( "Saved file to public/blogs.json" ) ;
121
125
} ) ;
122
126
} ) ;
123
127
} ) ;
0 commit comments