-
Notifications
You must be signed in to change notification settings - Fork 0
/
request-builder.js
154 lines (125 loc) · 4.15 KB
/
request-builder.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { URL, URLSearchParams } from 'url';
export function buildFetchRequest({
sortBy,
countryCode, geoHash, distance,
thisWeekendDate, dateFrom, dateTo, includeTBA, includeTBD,
},
classifications, page = 0, scrapedItems = 0) {
const url = new URL(`https://www.ticketmaster.com/api/next/graphql?`);
const variables = buildRequestVariables({
sortBy,
countryCode,
geoHash,
distance,
thisWeekendDate,
dateFrom,
dateTo,
includeTBA,
includeTBD,
}, classifications, page);
const extensions = {
persistedQuery: {
version: 1,
sha256Hash: '5664b981ff921ec078e3df377fd4623faaa6cd0aa2178e8bdfcba9b41303848b',
},
};
const queryParams = {
operationName: 'CategorySearch',
variables: JSON.stringify(variables),
extensions: JSON.stringify(extensions),
};
url.search = new URLSearchParams(queryParams);
const request = {
url: url.toString(),
userData: { page, classifications, scrapedItems },
};
return request;
}
function buildRequestVariables({
sortBy,
countryCode, geoHash, distance,
thisWeekendDate, dateFrom, dateTo, includeTBA, includeTBD,
}, classifications, page) {
const { sort, asc } = getSortOptions(sortBy);
const sortOrder = asc ? 'asc' : 'desc';
const variables = {
type: 'event',
locale: 'en-us',
localeStr: 'en-us',
page,
size: 200,
sort: `${sort},${sortOrder}`,
classificationId: classifications,
lineupImages: true,
withSeoEvents: true,
geoHash,
countryCode,
radius: distance,
unit: 'miles',
includeTBA,
includeTBD,
};
addDateVariable(variables, { thisWeekendDate, dateFrom, dateTo });
return variables;
}
function getSortOptions(sortBy) {
const sortOptions = { sort: 'date', asc: true }; // sort by date in asc order by default
if (sortBy === 'date' || sortBy === 'relevance') {
sortOptions.sort = sortBy;
} else if (sortBy) {
const lowerCaseSort = sortBy.toLowerCase();
if (lowerCaseSort.includes('asc')) {
sortOptions.sort = lowerCaseSort.replace('asc', '');
} else if (lowerCaseSort.includes('desc')) {
sortOptions.sort = lowerCaseSort.replace('desc', '');
sortOptions.asc = false;
}
}
return sortOptions;
}
function addDateVariable(variables, { thisWeekendDate, dateFrom, dateTo }) {
if (thisWeekendDate) {
variables.localStartEndDateTime = getWeekendDatesString();
} else if (dateFrom && dateTo) {
variables.localStartEndDateTime = getDateRangeString(dateFrom, dateTo);
} else if (dateFrom) {
validateDateFormat(dateFrom);
variables.localStartDateTime = new Date(dateFrom);
} else if (dateTo) {
validateDateFormat(dateTo);
variables.localEndDateTime = new Date(dateTo);
}
}
function getWeekendDatesString() {
const date = new Date();
const saturday = date.getDate() - (date.getDay() - 1) + 5;
const sunday = date.getDate() - (date.getDay() - 1) + 6;
const saturdayDate = new Date(date.setDate(saturday));
const sundayDate = new Date(date.setDate(sunday));
setDateFromHours(saturdayDate);
setDateToHours(sundayDate);
return `${convertDateToISOFormat(saturdayDate)},${convertDateToISOFormat(sundayDate)}`;
}
function getDateRangeString(dateFrom, dateTo) {
validateDateFormat(dateFrom);
validateDateFormat(dateTo);
const from = new Date(dateFrom);
const to = new Date(dateTo);
setDateFromHours(from);
setDateToHours(to);
return `${convertDateToISOFormat(from)},${convertDateToISOFormat(to)}`;
}
function validateDateFormat(dateFormat) {
if (!Date.parse(dateFormat)) {
throw new Error(`Invalid date format provided. Valid format is: YYYY-MM-DD. Format from input: ${dateFormat}.`);
}
}
function setDateFromHours(date) {
date.setUTCHours(0, 0, 0);
}
function setDateToHours(date) {
date.setUTCHours(23, 59, 59);
}
function convertDateToISOFormat(date) {
return date.toISOString().split('.')[0];
}