-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzhsClient.js
More file actions
184 lines (151 loc) · 6.42 KB
/
zhsClient.js
File metadata and controls
184 lines (151 loc) · 6.42 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { courses } from "./courses.js"
import { generateCookieString, loadJsonFile } from "./helper.js"
export class ZHSClient {
static fromCookieFile(filePath){
const client = new ZHSClient()
const file = loadJsonFile(filePath)
client.cookieObject = file
client.cookieString = generateCookieString(file.cookie)
return client
}
static fromCookieObject(cookieObject){
const client = new ZHSClient()
client.cookieObject = cookieObject
client.cookieString = generateCookieString(cookieObject.cookie)
return client
}
static fromCookieString(cookieString){
const client = new ZHSClient()
client.cookieString = cookieString
return client
}
constructor() {
this.cookieObject = null
this.cookieString = ""
this.courses = courses
this.baseUrl = 'https://kurse.zhs-muenchen.de'
this.apiEndpoint = '/api/query'
this.defaultHeaders = {
"accept": "application/json, text/plain, */*",
"accept-language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
"content-type": "application/json",
"priority": "u=1, i",
"sec-ch-ua": "\"Chromium\";v=\"142\", \"Google Chrome\";v=\"142\", \"Not_A Brand\";v=\"99\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-uninow-language": "de_DE"
}
}
/**
* Fügt den Kurs in den Warenkorb. Bei wöchentlichen Kursen wird zum nächstmöglichen Wochentag gebucht. Für zukünftige Wochen wird ein offset gesetzt
* @param {keyof typeof courses} courseName
* @param offset Gibt das offset für die jeweilige Woche an. Z.B offset=2 beschreibt den zukünftigen Buchungstag plus 14 Tage. Default 0
*/
async book(courseName, offset = 0){
const course = this.courses[courseName] || this.findCourseNameById(courseName)
const requestOptions = {
method: "POST",
headers: {
...this.defaultHeaders,
'cookie': this.cookieString,
'origin': this.baseUrl,
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
},
body: course.queryString(offset)
}
const result = await fetch(`${this.baseUrl}${this.apiEndpoint}`, requestOptions)
console.log(result.status)
if(!result.ok){
return {
status: "error",
response: result,
responseBody: null,
}
}
const body = await result.json()
console.log(body)
if(!body.data) {
return {
status: "not_booked",
response: result,
responseBody: body
}
}
return {
status: "booked",
response: result,
responseBody: body
}
}
/**
* @typedef {Object} BookingSlot
* @property {string} start - ISO timestamp when the slot begins.
* @property {string} end - ISO timestamp when the slot ends.
* @property {string} booking_period_start - ISO timestamp when booking becomes available.
* @property {string} booking_period_end - ISO timestamp when booking closes.
* @property {number} availability - Number of free spots remaining.
* @property {number} already_booked - Whether the user has already booked this slot (0 or 1).
* @property {number} already_in_cart - Whether the slot is already in the user's cart (0 or 1).
* @property {number} already_on_waiting_list - Whether the user is on the waiting list (0 or 1).
*/
/**
* @typedef {Object} BookingSlotsData
* @property {BookingSlot[]} booking_slots - List of booking slots.
*/
/**
* @typedef {Object} BookingSlotsResponse
* @property {BookingSlotsData} data - Response wrapper.
*/
/**
* Fragt einen Kurs Slot ab, ob dieser Zeitraum valide ist und noch Plätze frei sind. Der Aufruf funktioniert nur bei bestimmten Kursen, welche ein Slot Format haben und wochenspezifisch sind
* @param {keyof typeof courses} courseName
* @param offset Gibt das offset für die jeweilige Woche an. Z.B offset=2 beschreibt den zukünftigen Buchungstag plus 14 Tage. Default 0
* @returns {Promise<BookingSlotsResponse>}
*/
async getCourseSlotDetails(courseName, offset){
const course = this.courses[courseName] || this.findCourseNameById(courseName)
const [startDate] = course.nextCourseStart(offset).split('T')
const [endDate] = course.nextCourseEnd(offset).split('T')
const requestOptions = {
method: "POST",
headers: {
...this.defaultHeaders,
'cookie': this.cookieString,
'origin': this.baseUrl,
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
},
body: JSON.stringify({
"query": "\nquery List_product_slots($productID: UUID!, $input: BookingSlotsInput!) {\n booking_slots(product_id: $productID, input: $input) {\n start\n end\n booking_period_start\n booking_period_end\n availability\n already_booked\n already_in_cart\n already_on_waiting_list\n }\n}",
"variables": {
"productID": course.id,
"input": {
// "start": course.nextCourseStart(offset),
// "end": course.nextCourseEnd(offset)
"start": `${startDate}T00:00:00Z`,
"end": `${endDate}T23:59:59Z`
}
}
})
}
const result = await fetch(`${this.baseUrl}${this.apiEndpoint}`, requestOptions)
return await result.json()
}
/**
*/
// async completeCheckout(){
// /**
// * pi intent fetchen
// * GET request mit pi intent für username und daten
// *
// */
// }
findCourseNameById(id){
for(const [key,val] of Object.entries(this.courses)){
if(val.id == id)
return key
}
}
}