Skip to content

Commit

Permalink
Abstract away more common batchexecute parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
baltpeter committed Jun 20, 2022
1 parent 3521a57 commit 2a3bddf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
10 changes: 9 additions & 1 deletion src/common/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@ export const batchExecute = async (requests: RequestPayload[], queryParams?: Que
const messages: any[] = JSON.parse(res.split('\n')[2]!);
assert(() => messages.length === requests.length + 2, 'Has response payload for each request.');

return messages.slice(0, requests.length).sort((a, b) => a[a.length - 1] - b[b.length - 1]);
const payloads = messages.slice(0, requests.length).sort((a, b) => a[a.length - 1] - b[b.length - 1]);

return payloads.map((payload, idx) => {
assert(() => payload[0] === 'wrb.fr' && payload[1] === requests[idx]?.[0], 'Correct header.');

const data = JSON.parse(payload[2]);
assert(() => data, 'Has inner data.');
return data;
});
};
14 changes: 5 additions & 9 deletions src/top-charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,10 @@ export const parseTopChartEntry = (entry: any, idx: number): TopChartsEntry => (
cover_image_url: entry[22][3]?.[2] as string | undefined,
});

export const parseTopChartPayload = (payload: any): TopChartsEntry[] | undefined => {
assert(() => payload[0] === 'wrb.fr' && payload[1] === 'vyAe2', 'Correct header.');

const data = JSON.parse(payload[2]);
assert(() => data, 'Has inner data.');

export const parseTopChartPayload = (data: any): TopChartsEntry[] | undefined => {
assert(() => data.length === 1, 'One top-level array entry.');
if (data[0][1] === null) return undefined;

assert(
() =>
data[0][1][0].length === 29 &&
Expand All @@ -118,7 +114,7 @@ export const parseTopChartPayload = (payload: any): TopChartsEntry[] | undefined
assert(() => meta.length === 23, 'Meta length.');
assert(() => meta[8][8][0] === 'CAE=', 'Weird buy param.');
assert(() => e[1].length === 1 && e[1][0].length === 3, 'Expected weird second meta object structure.');
const empty_meta = e[1][0].flat(100);
const empty_meta = e[1][0].flat(Infinity);
assert(
() =>
empty_meta.filter((i: unknown) => i === null).length === empty_meta.length - 1 &&
Expand Down Expand Up @@ -160,10 +156,10 @@ export async function fetchTopCharts(
): Promise<(TopChartsResult | undefined)[]>;
export async function fetchTopCharts(requests: TopChartsRequest | TopChartsRequest[], options: TopChartsOptions) {
const _requests = Array.isArray(requests) ? requests : [requests];
const payloads = await batchExecute(
const data = await batchExecute(
_requests.map((r) => topChartsRequestPayload(r)),
{ hl: options.language, gl: options.country }
);
const res = payloads.map((p) => parseTopChartPayload(p));
const res = data.map(parseTopChartPayload);
return _requests.length === 1 ? res[0] : res;
}

0 comments on commit 2a3bddf

Please sign in to comment.