@@ -38,44 +38,32 @@ export class APIService {
38
38
return data ;
39
39
}
40
40
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 } ` ) ;
43
44
44
45
if ( cache ) return JSON . parse ( cache ) ;
45
46
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 ;
59
50
60
- if ( response . status !== 200 ) return null ;
51
+ const nowISO = new Date ( now ) . toISOString ( ) ;
61
52
62
- const data : UserReg = response . data ;
53
+ const from = new Date ( now ) ;
54
+ from . setFullYear ( from . getFullYear ( ) - 1 ) ;
55
+ const fromISO = from . toISOString ( ) ;
63
56
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 ) {
69
57
const response = await axios . post (
70
58
'https://api.github.com/graphql' ,
71
59
{
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)" +
74
62
"{ contributionCalendar { weeks { contributionDays { date contributionCount } } } } } }" ,
75
63
variables : {
76
64
username : username ,
77
- from : from ,
78
- to : to
65
+ from : fromISO ,
66
+ to : nowISO
79
67
}
80
68
} ,
81
69
{
@@ -84,40 +72,11 @@ export class APIService {
84
72
}
85
73
) ;
86
74
if ( response . status !== 200 || ! response . data . data . user ) return null ;
87
- const data : GithubStreak = response . data ;
88
75
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 =>
90
78
week . contributionDays . map ( val => val . contributionCount )
91
79
) ;
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
- }
121
80
122
81
let streak_start = - 1 ;
123
82
let streak_end = - 1 ;
@@ -149,7 +108,7 @@ export class APIService {
149
108
total_contributions
150
109
}
151
110
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 ) ;
153
112
return result ;
154
113
}
155
114
0 commit comments