-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
FanItemsBaseParser.ts
98 lines (84 loc) · 3.14 KB
/
FanItemsBaseParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { load as cheerioLoad } from 'cheerio';
import { decode } from 'html-entities';
import { FanItemsContinuation } from '../types/Fan.js';
import { ImageFormat } from '../types/Image.js';
type PageDataKey = 'collection' | 'wishlist' | 'following_genres' | 'following_bands';
type ContinuationDataKey = 'items' | 'followeers';
interface FanItemsParseParams<T> extends FanItemParseOptions {
dataKey: PageDataKey | ContinuationDataKey;
parseItemFn: (data: any, opts: FanItemParseOptions, tracklists: any) => T;
}
export interface FanPageItemsResult<T> {
items: T[];
total: number;
continuation: FanItemsContinuation | null;
}
export interface FanContinuationItemsResult<T> {
items: T[];
continuation: FanItemsContinuation | null;
}
export interface FanItemParseOptions {
imageBaseUrl: string;
imageFormat: ImageFormat | null;
}
export default abstract class FanItemsBaseParser {
protected static parsePageItems<T>(html: string, params: FanItemsParseParams<T>): FanPageItemsResult<NonNullable<T>> {
const _getSequenceOrPending = (o: any) => {
return Array.isArray(o.sequence) && o.sequence.length > 0 ? o.sequence :
Array.isArray(o.pending_sequence) && o.pending_sequence.length > 0 ? o.pending_sequence : [];
};
const $ = cheerioLoad(html);
const blob = decode($('#pagedata[data-blob]').attr('data-blob'));
const parsed = JSON.parse(blob);
const result: FanPageItemsResult<NonNullable<T>> = {
items: [],
total: 0,
continuation: null
};
const itemListData = parsed[`${params.dataKey}_data`];
const itemCache = parsed.item_cache?.[params.dataKey];
if (itemListData && itemCache) {
const tracklists = parsed.tracklists?.[params.dataKey];
const sequence = _getSequenceOrPending(itemListData);
const parseFn = params.parseItemFn;
sequence.forEach((itemKey: string) => {
const parsedItem = parseFn(itemCache[itemKey], params, tracklists);
if (parsedItem) {
result.items.push(parsedItem);
}
});
result.total = itemListData.item_count;
const fanId = parsed.fan_data && parsed.fan_data.fan_id ? parsed.fan_data.fan_id : null;
if (itemListData.item_count > sequence.length && itemListData.last_token && fanId) {
result.continuation = {
fanId,
token: itemListData.last_token
};
}
}
return result;
}
protected static parseContinuationItems<T>(json: any, continuation: FanItemsContinuation,
params: FanItemsParseParams<T>): FanContinuationItemsResult<NonNullable<T>> {
const items = json[params.dataKey] || [];
const tracklists = json.tracklists || null;
const parseFn = params.parseItemFn;
const result: FanContinuationItemsResult<NonNullable<T>> = {
items: [],
continuation: null
};
items.forEach((data: any) => {
const parsedItem = parseFn(data, params, tracklists);
if (parsedItem) {
result.items.push(parsedItem);
}
});
if (json.more_available && json.last_token) {
result.continuation = {
fanId: continuation.fanId,
token: json.last_token
};
}
return result;
}
}