-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
183 lines (162 loc) · 5.99 KB
/
server.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
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 lightningcss from 'bun-lightningcss'
if (!Bun.env.GOOGLE_CALENDAR_API_KEY) {
throw new Error("Missing Google Calendar API key")
}
if (!Bun.env.FLICKR_API_KEY) {
throw new Error("Missing Flickr API key")
}
if (!Bun.env.SPOTIFY_CLIENT_ID) {
throw new Error("Missing Spotify client ID")
}
if (!Bun.env.SPOTIFY_CLIENT_SECRET) {
throw new Error("Missing Spotify client secret")
}
if (!Bun.env.SPOTIFY_REFRESH_TOKEN) {
throw new Error("Missing Spotify refresh token")
}
if (!Bun.env.WAPPU_DECLARED) {
throw new Error("Missing Wappu declared")
}
process.env.TZ = 'Europe/Helsinki'
const builds = await Bun.build({
entrypoints: ['./src/index.tsx'],
target: "browser",
define: {
'Bun.env.GOOGLE_CALENDAR_API_KEY': JSON.stringify(Bun.env.GOOGLE_CALENDAR_API_KEY),
'Bun.env.FLICKR_API_KEY': JSON.stringify(Bun.env.FLICKR_API_KEY),
'Bun.env.SPOTIFY_CLIENT_ID': JSON.stringify(Bun.env.SPOTIFY_CLIENT_ID),
'Bun.env.SPOTIFY_CLIENT_SECRET': JSON.stringify(Bun.env.SPOTIFY_CLIENT_SECRET),
'Bun.env.SPOTIFY_REFRESH_TOKEN': JSON.stringify(Bun.env.SPOTIFY_REFRESH_TOKEN),
'Bun.env.WAPPU_DECLARED': JSON.stringify(Bun.env.WAPPU_DECLARED),
},
minify: {
identifiers: true,
syntax: true,
whitespace: true,
},
outdir: './build',
plugins: [lightningcss()],
})
const indexFile = Bun.file('public/index.html')
const server = Bun.serve({
port: 3010,
fetch: async (req, server) => {
const { pathname } = new URL(req.url)
if (pathname === "/main.js" && req.method === "GET") {
return new Response(builds.outputs[0].stream(), {
headers: {
'Content-Type': builds.outputs[0].type,
},
})
}
if (pathname === "/favicon.ico" && req.method === "GET") {
const faviconFile = Bun.file('public/favicon.ico')
return new Response(faviconFile.stream(), {
headers: {
'Content-Type': faviconFile.type,
},
})
}
if (pathname.includes("images")) {
const imageFile = Bun.file(`src${pathname}`)
return new Response(imageFile.stream(), {
headers: {
'Content-Type': imageFile.type,
},
})
}
if (pathname.includes("fonts")) {
const fontFile = Bun.file(`src${pathname}`)
return new Response(fontFile.stream(), {
headers: {
'Content-Type': fontFile.type,
},
})
}
if (pathname === "/api/update" && req.method === "GET") {
const updateFile = Bun.file('telegram-bot/update.json')
return new Response(updateFile.stream(), {
headers: {
'Content-Type': updateFile.type,
},
})
}
if (pathname === "/api/recently-listened" && req.method === "GET") {
const recentlyListed = await getRecentlyListened()
return new Response(JSON.stringify(recentlyListed), {
headers: {
'Content-Type': 'application/json',
},
})
}
if (pathname === "/api/open-restaurants" && req.method === "GET") {
const openRestaurants = await getOpenRestaurants()
return new Response(JSON.stringify(openRestaurants), {
headers: {
'Content-Type': 'application/json',
},
})
}
if (pathname === "/" && req.method === "GET") {
const indexContent = await indexFile.text()
const contentWithReactScript = indexContent.replace(
"<!-- react-script -->",
`<script type="module" src="/main.js"></script>`,
)
return new Response(contentWithReactScript, {
headers: {
'Content-Type': 'text/html',
},
})
}
return new Response('Not Found', { status: 404 })
},
})
console.log(`Listening on ${server.hostname}:${server.port}`)
async function getRecentlyListened() {
const tokenResponse = await fetch("https://accounts.spotify.com/api/token", {
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${Buffer.from(`${Bun.env.SPOTIFY_CLIENT_ID}:${Bun.env.SPOTIFY_CLIENT_SECRET}`).toString('base64')}`,
},
body: 'grant_type=refresh_token&refresh_token=' + Bun.env.SPOTIFY_REFRESH_TOKEN,
})
const { access_token } = await tokenResponse.json()
const data = await fetch("https://api.spotify.com/v1/me/player/recently-played?limit=5", {
headers: {
'Authorization': `Bearer ${access_token}`,
},
})
const recentlyListened = await data.json()
return recentlyListened.items.map((item: {track: {artists: {name: string}[], name: string, album: {images: {url: string}[]}}, played_at: string}) => ({
artist: item.track.artists[0].name,
title: item.track.name,
timestamp: new Date(item.played_at).getTime(),
thumbnail: item.track.album.images[0].url,
}))
}
async function getOpenRestaurants() {
const timeNow = new Date()
const todayString = timeNow.toISOString().substring(0,10)
const restaurantIDs = [1, 5, 3, 45, 50, 51, 52]
let currentDay = timeNow.getDay()
currentDay = currentDay === 0 ? 6 : currentDay - 1
const data = await fetch(`https://kitchen.kanttiinit.fi/restaurants?lang=fi&ids=${restaurantIDs.join()}&priceCategories=student,studentPremium`)
const restaurants = await data.json() as {openingHours: string[], id: string}[]
const openRestaurants = restaurants.filter(restaurant => {
const todayOpenings = restaurant.openingHours[currentDay]
if(!todayOpenings) return false
const [open, close] = todayOpenings.split("-")
const [openHour, openMinute] = open.split(":")
const [closeHour, closeMinute] = close.split(":")
const openTime = new Date(timeNow.getTime())
openTime.setHours(Number(openHour), Number(openMinute), 0, 0)
const closeTime = new Date(timeNow.getTime())
closeTime.setHours(Number(closeHour), Number(closeMinute), 0, 0)
return openTime < timeNow && timeNow < closeTime
}).map(restaurant => restaurant.id)
if(openRestaurants.length === 0) return []
const menus = await fetch(`https://kitchen.kanttiinit.fi/menus?lang=fi&restaurants=${openRestaurants}&days=${todayString}`)
return Object.entries(await menus.json())
}