File tree Expand file tree Collapse file tree 6 files changed +76
-40
lines changed
Expand file tree Collapse file tree 6 files changed +76
-40
lines changed Original file line number Diff line number Diff line change @@ -8,18 +8,24 @@ import fetch from 'node-fetch';
88
99import { qiitaApiUrl } from '../config' ;
1010import { QiitaArticle , RawQiitaArticle } from '../data-transfer-types' ;
11+ import { logger } from '../logger' ;
1112
1213export async function fetchArticles ( userName : string , qiitaAccessToken : string ) : Promise < QiitaArticle [ ] > {
13- const response = await fetch ( qiitaApiUrl ( userName ) , {
14- headers : {
15- 'content-type' : 'application/json' ,
16- charset : 'utf-8' ,
17- ...( qiitaAccessToken ? { Authorization : `Bearer ${ qiitaAccessToken } ` } : undefined ) ,
18- } ,
19- } ) ;
20- if ( response . status !== 200 ) {
14+ try {
15+ const response = await fetch ( qiitaApiUrl ( userName ) , {
16+ headers : {
17+ 'content-type' : 'application/json' ,
18+ charset : 'utf-8' ,
19+ ...( qiitaAccessToken ? { Authorization : `Bearer ${ qiitaAccessToken } ` } : undefined ) ,
20+ } ,
21+ } ) ;
22+ if ( response . status !== 200 ) {
23+ return [ ] ;
24+ }
25+ const result : RawQiitaArticle [ ] = await response . json ( ) ;
26+ return result . map ( article => QiitaArticle . from ( article ) ) ;
27+ } catch ( e ) {
28+ logger . error ( `Error at qiita fetching: ${ JSON . stringify ( e ) } ` ) ;
2129 return [ ] ;
2230 }
23- const result : RawQiitaArticle [ ] = await response . json ( ) ;
24- return result . map ( article => QiitaArticle . from ( article ) ) ;
2531}
Original file line number Diff line number Diff line change @@ -46,19 +46,31 @@ import fetch from 'node-fetch';
4646
4747import { zennRssUrl } from '../config' ;
4848import { RawZennArticle , ZennArticle } from '../data-transfer-types' ;
49+ import { logger } from '../logger' ;
4950
5051export async function fetchArticles ( userId : string ) : Promise < ZennArticle [ ] > {
51- const response = await fetch ( zennRssUrl ( userId ) ) ;
52- if ( response . status !== 200 ) {
53- return [ ] ;
54- }
52+ try {
53+ const response = await fetch ( zennRssUrl ( userId ) ) ;
54+ if ( response . status !== 200 ) {
55+ return [ ] ;
56+ }
57+
58+ const result = await response . text ( ) ;
59+ if ( ! parser . validate ( result ) ) {
60+ return [ ] ;
61+ }
5562
56- const result = await response . text ( ) ;
57- if ( ! parser . validate ( result ) ) {
63+ const xml = parser . parse ( result ) ;
64+
65+ if ( ! xml . rss . channel . item ) {
66+ logger . info ( `User (${ userId } ) has no zenn artiles.` ) ;
67+ return [ ] ;
68+ }
69+
70+ const items : RawZennArticle [ ] = [ xml . rss . channel . item ] . flat ( ) ;
71+ return items . map ( article => ZennArticle . from ( article ) ) ;
72+ } catch ( e ) {
73+ logger . error ( `Error at zenn fetching: ${ JSON . stringify ( e ) } ` ) ;
5874 return [ ] ;
5975 }
60-
61- const xml = parser . parse ( result ) ;
62- const items : RawZennArticle [ ] = [ xml . rss . channel . item ] . flat ( ) ;
63- return items . map ( article => ZennArticle . from ( article ) ) ;
6476}
Original file line number Diff line number Diff line change @@ -21,8 +21,8 @@ export const QiitaArticle = {
2121 from ( article : RawQiitaArticle ) : QiitaArticle {
2222 return {
2323 ...article ,
24- created_at : DateTime . fromISO ( article . created_at ) ,
25- updated_at : DateTime . fromISO ( article . updated_at ) ,
24+ created_at : DateTime . fromISO ( article . created_at ) . setZone ( 'utc' ) ,
25+ updated_at : DateTime . fromISO ( article . updated_at ) . setZone ( 'utc' ) ,
2626 } ;
2727 } ,
2828} ;
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ export const ZennArticle = {
1515 from ( article : RawZennArticle ) : ZennArticle {
1616 return {
1717 ...article ,
18- pubDate : DateTime . fromHTTP ( article . pubDate ) ,
18+ pubDate : DateTime . fromHTTP ( article . pubDate ) . setZone ( 'utc' ) ,
1919 } ;
2020 } ,
2121} ;
You can’t perform that action at this time.
0 commit comments