Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit dc82682

Browse files
committed
Search through youtube api if logged in
Signed-off-by: Sahil Gupte <[email protected]>
1 parent c5e2fff commit dc82682

File tree

5 files changed

+88
-16
lines changed

5 files changed

+88
-16
lines changed

src/mainWindow/pages/search/index.vue

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
/>
3535
</RecycleScroller>
3636
<b-container v-else class="mt-5 mb-3">
37-
<b-row align-v="center">
38-
<b-col class="nothing-found"> Nothing found... </b-col>
37+
<b-row align-v="center" align-h="center">
38+
<b-col cols="auto" v-if="i.loading"><b-spinner class="spinner" label="Loading..."></b-spinner></b-col>
39+
<b-col v-else class="nothing-found"> Nothing found... </b-col>
3940
</b-row>
4041
</b-container>
4142
<b-button class="load-more" v-if="getLoadMore(tab)" @click="handleLoadMore(tab)"
@@ -70,8 +71,8 @@ export default class SearchPage extends mixins(RouterPushes, ContextMenuMixin, I
7071
{ tab: 'Artists', count: 0, key: 'Artists-0' },
7172
{ tab: 'Genres', count: 0, key: 'Genres-0' },
7273
{ tab: 'Playlists', count: 0, key: 'Playlists-0' },
73-
{ tab: 'Youtube', count: 0, key: 'Youtube-0' },
74-
{ tab: 'Spotify', count: 0, key: 'Spotify-0' }
74+
{ tab: 'Youtube', count: 0, key: 'Youtube-0', loading: false },
75+
{ tab: 'Spotify', count: 0, key: 'Spotify-0', loading: false }
7576
]
7677
7778
private loadedMore: { [key: string]: boolean } = { artists: false }
@@ -81,15 +82,43 @@ export default class SearchPage extends mixins(RouterPushes, ContextMenuMixin, I
8182
}
8283
8384
private async fetchData() {
84-
this.result = await window.SearchUtils.searchAll(`%${this.term}%`)
85-
this.refreshLocal()
85+
window.SearchUtils.searchAll(`%${this.term}%`).then((data) => {
86+
this.result = {
87+
...this.result,
88+
...data
89+
}
90+
this.refreshLocal()
91+
})
92+
93+
const youtubeItem = this.items.find((val) => val.tab === 'Youtube')
94+
if (youtubeItem) {
95+
youtubeItem.loading = true
8696
87-
this.result.youtube = await window.SearchUtils.searchYT(this.term, undefined, false, false, true)
88-
this.refreshYoutube()
97+
if (vxm.providers.loggedInYoutube) {
98+
vxm.providers.youtubeProvider.searchSongs(this.term).then((data) => {
99+
this.result.youtube = data
100+
this.refreshYoutube()
101+
youtubeItem.loading = false
102+
})
103+
} else {
104+
window.SearchUtils.searchYT(this.term, undefined, false, false, true).then((data) => {
105+
this.result.youtube = data
106+
this.refreshYoutube()
107+
youtubeItem.loading = false
108+
})
109+
}
110+
}
89111
90-
if (vxm.providers.loggedInSpotify) {
91-
this.result.spotify = await vxm.providers.spotifyProvider.searchSongs(this.term)
92-
this.refreshSpotify()
112+
const spotifyItem = this.items.find((val) => val.tab === 'Spotify')
113+
if (spotifyItem) {
114+
if (vxm.providers.loggedInSpotify) {
115+
spotifyItem.loading = true
116+
vxm.providers.spotifyProvider.searchSongs(this.term).then((data) => {
117+
this.result.spotify = data
118+
this.refreshSpotify()
119+
spotifyItem.loading = false
120+
})
121+
}
93122
}
94123
}
95124
@@ -367,4 +396,6 @@ export default class SearchPage extends mixins(RouterPushes, ContextMenuMixin, I
367396
.load-more
368397
background-color: var(--accent)
369398
color: var(--textInverse)
399+
400+
.spinner
370401
</style>

src/types/declarations/youtubeResponses.d.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ declare namespace YoutubeResponses {
1919

2020
type SearchRequest = {
2121
params: {
22+
part: ['id', 'snippet'?]
2223
relatedToVideoId?: string
23-
videoCategoryId: 10
24-
type: 'video'
25-
videoDuration: 'short'
24+
videoCategoryId?: 10
25+
type?: 'video'
26+
videoDuration?: 'short'
2627
maxResults?: number
27-
videoEmbeddable: true
28-
order: 'date'
28+
videoEmbeddable?: true
29+
order?: 'date' | 'relevance'
30+
q?: string
2931
}
3032
}
3133

@@ -277,6 +279,7 @@ declare namespace YoutubeResponses {
277279
}
278280
kind: string
279281
etag: string
282+
snippet: VideoDetails.Snippet
280283
}
281284
interface SearchDetails {
282285
items: Item[]

src/utils/ui/providers/generics/genericProvider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,6 @@ export abstract class GenericProvider {
8383
* @param artist_id ID of artists whose tracks are to be fetched
8484
*/
8585
public abstract getArtistSongs(artist_id: string): AsyncGenerator<Song[]>
86+
87+
public abstract searchSongs(term: string): Promise<Song[]>
8688
}

src/utils/ui/providers/invidious.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,17 @@ export class InvidiousProvider extends GenericAuth implements GenericProvider, G
217217
public async *getArtistSongs(): AsyncGenerator<Song[]> {
218218
yield []
219219
}
220+
221+
public async searchSongs(term: string): Promise<Song[]> {
222+
const resp = await this.populateRequest(InvidiousApiResources.SEARCH, {
223+
params: {
224+
q: term,
225+
type: 'video',
226+
sort_by: 'relevance'
227+
}
228+
})
229+
230+
if (resp) return this.parsePlaylistItems([resp])
231+
return []
232+
}
220233
}

src/utils/ui/providers/youtube.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,28 @@ export class YoutubeProvider extends GenericAuth implements GenericProvider, Gen
331331
}
332332
}
333333

334+
public async searchSongs(term: string): Promise<Song[]> {
335+
const songList: Song[] = []
336+
const resp = await this.populateRequest(ApiResources.SEARCH, {
337+
params: {
338+
part: ['id', 'snippet'],
339+
q: term,
340+
type: 'video',
341+
maxResults: 30,
342+
order: 'relevance',
343+
videoEmbeddable: true
344+
}
345+
})
346+
347+
const finalIDs: string[] = []
348+
if (resp.items) {
349+
resp.items.forEach((val) => finalIDs.push(val.id.videoId))
350+
songList.push(...(await this.getSongDetailsFromID(false, ...finalIDs)))
351+
}
352+
353+
return songList
354+
}
355+
334356
public async getSongDetails(url: string, invalidateCache = false): Promise<Song | undefined> {
335357
if (
336358
url.match(
@@ -379,6 +401,7 @@ export class YoutubeProvider extends GenericAuth implements GenericProvider, Gen
379401
;(
380402
await this.populateRequest(ApiResources.SEARCH, {
381403
params: {
404+
part: ['id', 'snippet'],
382405
type: 'video',
383406
videoCategoryId: 10,
384407
videoDuration: 'short',

0 commit comments

Comments
 (0)