@@ -38,44 +38,32 @@ export class APIService {
3838 return data ;
3939 }
4040
41- async getGithubUserRegistration ( username : string ) : Promise < UserReg | null > {
42- const cache = await this . cacheManager . get < string > ( `user_reg:${ username } ` ) ;
41+
42+ async getGithubStreak ( username : string ) : Promise < { streak : number ; longest : number ; total_contributions : number ; } | null > {
43+ const cache = await this . cacheManager . get < string > ( `streak:${ username } ` ) ;
4344
4445 if ( cache ) return JSON . parse ( cache ) ;
4546
46- const response = await axios . post (
47- 'https://api.github.com/graphql' ,
48- {
49- query : "query GetUserCreatedAt($username: String!) { user(login: $username) { createdAt } }" ,
50- variables : {
51- username : username
52- }
53- } ,
54- {
55- headers : { Authorization : `Bearer ${ process . env . GITHUB } ` } ,
56- validateStatus : ( ) => true
57- }
58- ) ;
47+ const date = new Date ( ) ;
48+ const timezoneOffset = date . getTimezoneOffset ( ) ;
49+ const now = date . getTime ( ) - timezoneOffset * 60000 ;
5950
60- if ( response . status !== 200 ) return null ;
51+ const nowISO = new Date ( now ) . toISOString ( ) ;
6152
62- const data : UserReg = response . data ;
53+ const from = new Date ( now ) ;
54+ from . setFullYear ( from . getFullYear ( ) - 1 ) ;
55+ const fromISO = from . toISOString ( ) ;
6356
64- await this . cacheManager . set ( `user_reg:${ username } ` , JSON . stringify ( data ) , 1000 * 60 * 60 * 1000 ) ;
65- return data ;
66- }
67-
68- async getGithubStreakYear ( username : string , from : string , to : string ) {
6957 const response = await axios . post (
7058 'https://api.github.com/graphql' ,
7159 {
72- query : ` query GetUserContributions($username: String!, $from: DateTime!${ ! ! to ? " , $to: DateTime!" : "" } )` +
73- ` { user(login: $username) { contributionsCollection(from: $from${ ! ! to ? " , to: $to" : "" } )` +
60+ query : " query GetUserContributions($username: String!, $from: DateTime!, $to: DateTime!)" +
61+ " { user(login: $username) { contributionsCollection(from: $from, to: $to)" +
7462 "{ contributionCalendar { weeks { contributionDays { date contributionCount } } } } } }" ,
7563 variables : {
7664 username : username ,
77- from : from ,
78- to : to
65+ from : fromISO ,
66+ to : nowISO
7967 }
8068 } ,
8169 {
@@ -84,40 +72,11 @@ export class APIService {
8472 }
8573 ) ;
8674 if ( response . status !== 200 || ! response . data . data . user ) return null ;
87- const data : GithubStreak = response . data ;
8875
89- return data . data . user . contributionsCollection . contributionCalendar . weeks . flatMap ( week =>
76+ const data = response . data as GithubStreak ;
77+ const days = data . data . user . contributionsCollection . contributionCalendar . weeks . flatMap ( week =>
9078 week . contributionDays . map ( val => val . contributionCount )
9179 ) ;
92- }
93-
94-
95- async getGithubStreak ( username : string ) : Promise < { streak : number ; longest : number ; total_contributions : number ; } | null > {
96- const cache = await this . cacheManager . get < string > ( `streak:${ username } ` ) ;
97-
98- if ( cache ) return JSON . parse ( cache ) ;
99-
100- const date = new Date ( ) ;
101- const timezoneOffset = date . getTimezoneOffset ( ) ;
102- const now = date . getTime ( ) - timezoneOffset * 60000 ;
103- const nowISO = new Date ( now ) . toISOString ( ) ;
104-
105- const currentYearStart = `${ date . getFullYear ( ) } -01-01T00:00:00Z` ;
106-
107- let days = await this . getGithubStreakYear ( username , currentYearStart , nowISO ) ;
108- if ( ! days ) return null ;
109-
110- const regTime = new Date ( ( await this . getGithubUserRegistration ( username ) ) . data . user . createdAt ) ;
111-
112- for ( let year = date . getFullYear ( ) - 1 ; year >= regTime . getFullYear ( ) ; year -- ) {
113- try {
114- const days_last = await this . getGithubStreakYear ( username , `${ year } -01-01T00:00:00Z` , undefined ) ;
115- days = [ ...days_last , ...days ] ;
116- } catch ( e ) {
117- console . error ( e ) ;
118- break ;
119- }
120- }
12180
12281 let streak_start = - 1 ;
12382 let streak_end = - 1 ;
@@ -149,7 +108,7 @@ export class APIService {
149108 total_contributions
150109 }
151110
152- await this . cacheManager . set ( `streak:${ username } ` , JSON . stringify ( result ) , 1000 * 60 * 60 * 3 ) ;
111+ await this . cacheManager . set ( `streak:${ username } ` , JSON . stringify ( result ) , 1000 * 60 * 60 ) ;
153112 return result ;
154113 }
155114
0 commit comments