Skip to content

Commit e454a4c

Browse files
committed
Switch clan downloading to official API
1 parent f773eea commit e454a4c

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

docs/README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,11 @@ npm run download-clan-stats
122122

123123
If you wish to only flatten your downloaded clan stats into a single once-a-day JSON file, you can use the `--only-flatten` argument.
124124

125-
***
126-
127-
#### API
125+
#### Official API
128126

129-
An API for this information was [recently added](https://github.com/codewars/codewars.com/discussions/2710), containing the same information but with 100 users listed per page - a great improvement over the current 15.
127+
An API for this information was [added](https://github.com/codewars/codewars.com/discussions/2710), containing the same information but with 100 users listed per page - a great improvement over the current 15.
130128

131-
Unfortunately as of writing, I haven't been able to use it for half of the pages, therefore it's only usable via the `--use-api` argument, when it becomes stable I will make it default.
129+
For those that still wish to use the previous method, the `--use-allies` argument will do so.
132130

133131
### Estimate Honor & Score
134132

src/download-clan-stats.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ async function useAPI(){
5757

5858
const rows: Record<string, Entry> = {}
5959

60+
let failures: Record<number, number> = {}
6061
let totalPages = 1;
6162
for (let page = 0; page <= totalPages; page++){
6263
await delay(2500);
@@ -74,6 +75,14 @@ async function useAPI(){
7475
} = await response.json();
7576
if ('success' in payload){
7677
console.error('API Error:', payload.reason);
78+
79+
if (!(page in failures)) failures[page] = 0;
80+
failures[page]++;
81+
if (failures[page] > 5){
82+
console.error('Failed on this page five times, giving up');
83+
return false;
84+
}
85+
7786
page--;
7887
await delay(10000);
7988
continue
@@ -91,8 +100,8 @@ async function useAPI(){
91100
return true;
92101
}
93102

94-
async function useBrowser() {
95-
console.log('Attempting to download Clan stats using browser...');
103+
async function useAllies() {
104+
console.log('Attempting to download Clan stats using allies page...');
96105
if (!USER_NAME) return console.error('USER_NAME environment variable not set');
97106
if (!USER_AGENT) return console.error('USER_AGENT environment variable not set');
98107
if (!REMEMBER_USER_TOKEN) return console.error('REMEMBER_USER_TOKEN environment variable not set');
@@ -177,8 +186,8 @@ async function flattenClanStats() {
177186
if (!process.argv.includes('--only-flatten')) {
178187
const useDirective = process.argv.find(arg => arg.startsWith('--use'))
179188

180-
const chosen = useDirective && useDirective.endsWith('api') ? useAPI : useBrowser;
181-
if (!(await chosen())) await (chosen === useAPI ? useBrowser : useAPI)();
189+
const chosen = useDirective && useDirective.endsWith('allies') ? useAllies : useAPI;
190+
if (!(await chosen())) await (chosen === useAPI ? useAllies : useAPI)();
182191
}
183192
await flattenClanStats();
184193
})().catch(console.error);

0 commit comments

Comments
 (0)