@@ -14,7 +14,7 @@ export default async function({login, q, imports, data, computed, graphql, queri
14
14
15
15
//Initialization
16
16
const list = [ ]
17
- await total ( { imports} )
17
+ await total ( { imports, graphql , queries } )
18
18
await compute [ account ] ( { list, login, data, computed, imports, graphql, queries, rest, rank, leaderboard} )
19
19
20
20
//Results
@@ -72,31 +72,50 @@ function leaderboard({user, type, requirement}) {
72
72
}
73
73
74
74
/**Total extracter */
75
- async function total ( { imports} ) {
75
+ async function total ( { imports, graphql , queries } ) {
76
76
if ( ! total . promise ) {
77
77
total . promise = new Promise ( async ( solve , reject ) => {
78
- //Setup browser
79
- console . debug ( "metrics/compute/plugins > achievements > filling total from github.com/search" )
80
- const browser = await imports . puppeteer . launch ( )
81
- console . debug ( `metrics/compute/plugins > achievements > started ${ await browser . version ( ) } ` )
82
- //Extracting total from github.com/search
83
- for ( let i = 0 ; ( i < 100 ) && ( ( ! total . users ) || ( ! total . repositories ) ) ; i ++ ) {
84
- const page = await browser . newPage ( )
85
- await page . goto ( "https://github.com/search?q=created%3A%3E%3D1970" )
86
- const results = await page . evaluate ( ( ) => [ ...[ ...document . querySelectorAll ( "h2" ) ] . filter ( node => / F i l t e r b y / . test ( node . innerText ) ) . shift ( ) ?. nextSibling ?. innerText . trim ( ) . matchAll ( / (?< type > R e p o s i t o r i e s | U s e r s | I s s u e s ) \n (?< count > .* ?) M / g) ?? [ ] ] ) ?? null
87
- for ( const result of results ) {
88
- const type = result [ 1 ] ?. toLowerCase ( )
89
- console . debug ( `metrics/compute/plugins > achievements > setup found ${ type ?? "(?)" } ` )
90
- const count = result [ 2 ] ?? ""
91
- if ( ( count !== "" ) && ( ! total [ type ] ) ) {
92
- total [ type ] = Number ( count ) * 10e5
93
- console . debug ( `metrics/compute/plugins > achievements > set total.${ type } to ${ total [ type ] } ` )
78
+ for ( const method of [ "graphql" , "browser" ] ) {
79
+ console . debug ( `metrics/compute/plugins > achievements > setup using ${ method } ` )
80
+ try {
81
+ //Setup using GraphQL
82
+ if ( method === "graphql" ) {
83
+ const queried = await graphql ( queries . achievements . total ( ) )
84
+ Object . assign ( total , Object . fromEntries ( Object . entries ( queried ) . map ( ( [ key , { count :value } ] ) => [ key , value ] ) ) )
94
85
}
86
+ //Setup using browser
87
+ if ( method === "browser" ) {
88
+ //Setup browser
89
+ console . debug ( "metrics/compute/plugins > achievements > filling total from github.com/search" )
90
+ const browser = await imports . puppeteer . launch ( )
91
+ console . debug ( `metrics/compute/plugins > achievements > started ${ await browser . version ( ) } ` )
92
+ //Extracting total from github.com/search
93
+ for ( let i = 0 ; ( i < 4 ) && ( ( ! total . users ) || ( ! total . repositories ) ) ; i ++ ) {
94
+ const page = await browser . newPage ( )
95
+ await page . goto ( "https://github.com/search?q=created%3A%3E%3D1970" )
96
+ const results = await page . evaluate ( ( ) => [ ...[ ...document . querySelectorAll ( "h2" ) ] . filter ( node => / F i l t e r b y / . test ( node . innerText ) ) . shift ( ) ?. nextSibling ?. innerText . trim ( ) . matchAll ( / (?< type > R e p o s i t o r i e s | U s e r s | I s s u e s ) \n .* ?(?< count > \d + ) M / g) ?? [ ] ] ) ?? null
97
+ for ( const result of results ) {
98
+ const type = result [ 1 ] ?. toLowerCase ( )
99
+ console . debug ( `metrics/compute/plugins > achievements > setup found ${ type ?? "(?)" } ` )
100
+ const count = result [ 2 ] ?? ""
101
+ if ( ( count !== "" ) && ( ! total [ type ] ) ) {
102
+ total [ type ] = Number ( count ) * 10e5
103
+ console . debug ( `metrics/compute/plugins > achievements > set total.${ type } to ${ total [ type ] } ` )
104
+ }
105
+ }
106
+ await page . close ( )
107
+ await imports . wait ( 10 * Math . random ( ) )
108
+ }
109
+ }
110
+ //Check setup state
111
+ if ( ( ! total . users ) || ( ! total . repositories ) )
112
+ throw new Error ( "Uncomplete setup" )
113
+ }
114
+ catch ( error ) {
115
+ console . debug ( `metrics/compute/plugins > achievements > setup error > ${ error } ` )
116
+ continue
95
117
}
96
- await page . close ( )
97
- await imports . wait ( 10 * Math . random ( ) )
98
118
}
99
- //Check setup state
100
119
if ( ( ! total . users ) || ( ! total . repositories ) )
101
120
return reject ( "Failed to initiate total for achievement plugin" )
102
121
console . debug ( "metrics/compute/plugins > achievements > total setup complete" )
0 commit comments