forked from CardosoDev04/seca-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm-events-data.mjs
163 lines (131 loc) · 5.07 KB
/
tm-events-data.mjs
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
155
156
157
158
159
160
161
162
163
import * as dataMem from './seca-data-mem.mjs';
const apiKey = 'yZezrkgZrATxTMVDlicZCocjNE4Nyqq4';
/**
* This module is used to interact directly with the Ticketmaster API.
* It serves as a base module for some of the functionalities of the SECA API service module.
*/
//--------------------Helper functions--------------------//
/**
* This function is used to check if the token is valid. It uses the dataMem module to check if the user exists.
* @param token
* @returns True or False
*/
async function isTokenValid(token) {
return await dataMem.checkUserExists(token);
}
/**
* This function is used to fetch a JSON response file from the URL.
* @param url
* @returns JSON File
*/
async function fetchJSONfromURL(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error(error);
}
}
//--------------------MAIN FUNCTIONS--------------------//
/**
* This function is used to search for events by name. It's used by the searchByName function in the service module.
* @param token
* @param name
* @param apikey
* @param s
* @param p
* @returns Event List
*/
async function searchByName(token, name, apikey, s, p) {
if (!await isTokenValid(token)) {
return "invalid";
}
const url = `https://app.ticketmaster.com/discovery/v2/events.json?keyword=${name}&apikey=${apikey}&size=${s}&page=${p}`;
const out = await fetchJSONfromURL(url);
let events = out._embedded.events && out._embedded.events || [];
let objOut = {};
let outList = [];
events.forEach(it => {
if (it) {
objOut = {
"id": it["id"],
"name": it["name"],
"date": it["dates"] && it["dates"]["start"] && it["dates"]["start"]["dateTime"] || "No date available",
"sales": it["sales"] && it["sales"]["public"]["startDateTime"] || "No date available",
"images": it["images"] && it["images"][0] && it["images"][0]["url"] || null,
"segment": it["classifications"] && it["classifications"][0] && it["classifications"][0]["segment"]["name"] || "N/A",
"genre": it["classifications"] && it["classifications"][0] && it["classifications"][0]["genre"]["name"] || "N/A",
"subgenre": it["classifications"] && it["classifications"][0] && it["classifications"][0]["subGenre"]["name"] || "N/A"
};
}
outList.push(objOut);
})
return outList;
}
/**
* This function is used to search for events by popularity. It's used by the searchByPopularity function in the service module.
* @param token
* @param apikey
* @param size
* @param page
* @returns Event List
*/
async function searchByPopularity(token, apikey, size, page) {
if (!await isTokenValid(token)) {
return "invalid";
}
const url = `https://app.ticketmaster.com/discovery/v2/events.json?&apikey=${apikey}&size=${size}&page=${page}&sort=relevance,desc`;
const out = await fetchJSONfromURL(url);
let events = out._embedded.events;
let outList = [];
let objOut = {};
events.forEach(it => {
if (it) {
objOut = {
"id": it["id"],
"name": it["name"],
"date": it["dates"] && it["dates"]["start"] && it["dates"]["start"]["dateTime"] || "No date available",
"sales": it["sales"] && it["sales"]["public"]["startDateTime"] || "No date available",
"images": it["images"] && it["images"][0] && it["images"][0]["url"] || null,
"segment": it["classifications"] && it["classifications"][0] && it["classifications"][0]["segment"]["name"] || "N/A",
"genre": it["classifications"] && it["classifications"][0] && it["classifications"][0]["genre"]["name"] || "N/A",
"subgenre": it["classifications"] && it["classifications"][0] && it["classifications"][0]["subGenre"]["name"] || "N/A"
};
}
outList.push(objOut);
});
return outList;
}
/**
* This function is used to get the details of an event. It's used by the getEventDetails function in the service module.
* @param id
* @returns Event Details
*/
async function getDetails(id) {
const url = `https://app.ticketmaster.com/discovery/v2/events/${id}.json?apikey=${apiKey}`;
try {
let data = await fetchJSONfromURL(url)
return {
id: id,
name: data.name ?? null,
date: data?.dates.start.dateTime ?? null,
sales: data.sales.public.startDateTime ?? null,
images: data?.images ?? null,
segment: data?.classifications[0].segment.name ?? null,
genre: data?.classifications[0].genre.name ?? null,
subgenre: data?.classifications[0].subGenre.name ?? null
};
} catch (error) {
console.error('Error fetching details', error)
}
return null;
}
export {
searchByName,
searchByPopularity,
getDetails,
fetchJSONfromURL
}