Skip to content

Commit 89a5e80

Browse files
feat(Symphony Radio): update domain and add feature to view user profiles (PreMiD#10412)
* Update regExp for Symphony Radio URL Signed-off-by: Keiran Chippendale <keiran.chippendale@kdnet.co.uk> * Update hostname check for Symphony Radio Signed-off-by: Keiran Chippendale <keiran.chippendale@kdnet.co.uk> * Update version number to 1.0.4 in metadata.json Signed-off-by: Keiran Chippendale <keiran.chippendale@kdnet.co.uk> * my bad typo fix is meta data Signed-off-by: Keiran Chippendale <keiran.chippendale@kdnet.co.uk> * Add user profile fetching and caching logic Signed-off-by: Keiran Chippendale <keiran.chippendale@kdnet.co.uk> * Refactor error handling and improve code readability Signed-off-by: Keiran Chippendale <keiran.chippendale@kdnet.co.uk> * Update API URL for Symphony Radio Signed-off-by: Keiran Chippendale <keiran.chippendale@kdnet.co.uk> * Update websites/S/Symphony Radio/metadata.json Co-authored-by: Daniel Lau <32113157+theusaf@users.noreply.github.com> Signed-off-by: Keiran Chippendale <keiran.chippendale@kdnet.co.uk> * Fix URL in Symphony Radio metadata Signed-off-by: Keiran Chippendale <keiran.chippendale@kdnet.co.uk> --------- Signed-off-by: Keiran Chippendale <keiran.chippendale@kdnet.co.uk> Co-authored-by: Daniel Lau <32113157+theusaf@users.noreply.github.com>
1 parent fad689f commit 89a5e80

File tree

2 files changed

+90
-10
lines changed

2 files changed

+90
-10
lines changed

websites/S/Symphony Radio/metadata.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"description": {
1010
"en": "Listen live to Symphony Radio — bringing you great music, live DJs, and feel-good vibes all day long."
1111
},
12-
"url": "symphonyradio.co.uk",
13-
"regExp": "^https?[:][/][/]([a-z0-9-]+[.])*symphonyradio[.]co[.]uk[/]",
14-
"version": "1.0.1",
12+
"url": "symphonyrad.io",
13+
"regExp": "^https?[:][/][/]([a-z0-9-]+[.])*symphonyrad[.]io[/]",
14+
"version": "1.0.2",
1515
"logo": "https://cdn.rcd.gg/PreMiD/websites/S/Symphony%20Radio/assets/logo.png",
1616
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/S/Symphony%20Radio/assets/thumbnail.png",
1717
"color": "#7C3AED",

websites/S/Symphony Radio/presence.ts

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const presence = new Presence({
44
clientId: '1449503984996974745',
55
})
66

7-
const API_URL = 'https://panel.symphradio.live/api/stats'
7+
const API_URL = 'https://staff.symphonyrad.io/api/stats'
88
const LOGO_512 = 'https://cdn.rcd.gg/PreMiD/websites/S/Symphony%20Radio/assets/logo.png'
99

1010
let lastTrackId: string | null = null
@@ -38,6 +38,15 @@ interface ApiResponse {
3838
}
3939
}
4040

41+
interface UserApiResponse {
42+
success: boolean
43+
data?: {
44+
id: number
45+
username: string
46+
avatar?: string
47+
}
48+
}
49+
4150
const statsCache: {
4251
data: ApiResponse | null
4352
fetchedAt: number
@@ -48,6 +57,14 @@ const statsCache: {
4857
promise: null,
4958
}
5059

60+
const profileCache: {
61+
data: Record<string, UserApiResponse['data'] | null>
62+
promise: Record<string, Promise<UserApiResponse['data'] | null>>
63+
} = {
64+
data: {},
65+
promise: {},
66+
}
67+
5168
const CACHE_TTL = 72_000
5269

5370
async function fetchStats(): Promise<ApiResponse | null> {
@@ -87,10 +104,69 @@ async function fetchStats(): Promise<ApiResponse | null> {
87104
return statsCache.promise
88105
}
89106

107+
async function fetchProfile(username: string): Promise<UserApiResponse['data'] | null> {
108+
if (profileCache.data[username]) {
109+
return profileCache.data[username]
110+
}
111+
112+
if (profileCache.promise[username]) {
113+
return profileCache.promise[username]
114+
}
115+
116+
profileCache.promise[username] = (async () => {
117+
try {
118+
const res = await fetch(
119+
`https://staff.symphonyrad.io/api/user?username=${encodeURIComponent(username)}`,
120+
)
121+
122+
if (!res.ok) {
123+
return null
124+
}
125+
126+
const json = (await res.json()) as UserApiResponse
127+
128+
if (!json.success || !json.data) {
129+
return null
130+
}
131+
132+
profileCache.data[username] = json.data
133+
134+
return json.data
135+
}
136+
catch {
137+
return null
138+
}
139+
finally {
140+
delete profileCache.promise[username]
141+
}
142+
})()
143+
144+
return profileCache.promise[username]
145+
}
146+
90147
presence.on('UpdateData', async () => {
91148
const browsing = await presence.getSetting<boolean>('browsing')
149+
const { hostname, pathname } = document.location
150+
151+
if (hostname.includes('symphonyrad.io') && pathname.startsWith('/profile/')) {
152+
const username = pathname.split('/profile/')[1]?.trim()
153+
154+
if (username) {
155+
const profile = await fetchProfile(username)
92156

93-
if (!document.location.hostname.includes('symphonyradio.co.uk')) {
157+
presence.setActivity({
158+
type: ActivityType.Watching,
159+
details: 'Viewing profile',
160+
state: profile?.username ?? username,
161+
largeImageKey: profile?.avatar || LOGO_512,
162+
largeImageText: profile?.username || 'Symphony Radio',
163+
})
164+
165+
return
166+
}
167+
}
168+
169+
if (!hostname.includes('symphonyrad.io')) {
94170
if (browsing) {
95171
presence.setActivity({
96172
type: ActivityType.Listening,
@@ -112,10 +188,14 @@ presence.on('UpdateData', async () => {
112188
return
113189
}
114190

115-
const getArtwork = (data: ApiResponse, fallback: string): string =>
116-
data.nowPlaying?.track?.artwork?.url || fallback
117-
const getDjart = (data: ApiResponse, fallback: string): string =>
118-
data.onAir?.presenter?.avatar || fallback
191+
const getArtwork = (api: ApiResponse, fallback: string): string => {
192+
return api.nowPlaying?.track?.artwork?.url || fallback
193+
}
194+
195+
const getDjart = (api: ApiResponse, fallback: string): string => {
196+
return api.onAir?.presenter?.avatar || fallback
197+
}
198+
119199
const track = data.song?.track ?? 'Live Radio'
120200
const artist = data.song?.artist ?? 'Symphony Radio'
121201

@@ -141,7 +221,7 @@ presence.on('UpdateData', async () => {
141221
largeImageKey: getArtwork(data, LOGO_512),
142222
largeImageText: artist,
143223
smallImageKey: getDjart(data, LOGO_512),
144-
smallImageText: `${isLive ? `DJ ${djName}` : 'DJ Symphony'}`,
224+
smallImageText: isLive ? `DJ ${djName}` : 'DJ Symphony',
145225
startTimestamp: lastStart || undefined,
146226
endTimestamp: end || undefined,
147227
})

0 commit comments

Comments
 (0)