Initial commit
This commit is contained in:
commit
02a6dabcfb
62 files changed
+15751
No files matched your search
@@ -0,0 +1,152 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"gitea.mrixs.me/Mrixs/yandex-music-api-go/internal/models"
|
||||
)
|
||||
|
||||
// Search performs a multi-type search
|
||||
func (c *Client) Search(ctx context.Context, query string, searchType string, page, pageSize int) (*models.SearchResult, error) {
|
||||
params := map[string]string{
|
||||
"text": query,
|
||||
"type": searchType,
|
||||
"page": fmt.Sprintf("%d", page),
|
||||
"page_size": fmt.Sprintf("%d", pageSize),
|
||||
}
|
||||
|
||||
var result models.SearchResult
|
||||
if err := c.request.Get(ctx, "/search", params, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Set client on all results
|
||||
if result.Tracks != nil {
|
||||
for i := range result.Tracks.Results {
|
||||
result.Tracks.Results[i].SetClient(c)
|
||||
}
|
||||
}
|
||||
if result.Albums != nil {
|
||||
for i := range result.Albums.Results {
|
||||
result.Albums.Results[i].SetClient(c)
|
||||
}
|
||||
}
|
||||
if result.Artists != nil {
|
||||
for i := range result.Artists.Results {
|
||||
result.Artists.Results[i].SetClient(c)
|
||||
}
|
||||
}
|
||||
if result.Playlists != nil {
|
||||
for i := range result.Playlists.Results {
|
||||
result.Playlists.Results[i].SetClient(c)
|
||||
}
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// SearchTracks searches for tracks
|
||||
func (c *Client) SearchTracks(ctx context.Context, query string, page, pageSize int) (*models.SearchTracksResult, error) {
|
||||
result, err := c.Search(ctx, query, "track", page, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.Tracks, nil
|
||||
}
|
||||
|
||||
// SearchAlbums searches for albums
|
||||
func (c *Client) SearchAlbums(ctx context.Context, query string, page, pageSize int) (*models.SearchAlbumsResult, error) {
|
||||
result, err := c.Search(ctx, query, "album", page, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.Albums, nil
|
||||
}
|
||||
|
||||
// SearchArtists searches for artists
|
||||
func (c *Client) SearchArtists(ctx context.Context, query string, page, pageSize int) (*models.SearchArtistsResult, error) {
|
||||
result, err := c.Search(ctx, query, "artist", page, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.Artists, nil
|
||||
}
|
||||
|
||||
// SearchPlaylists searches for playlists
|
||||
func (c *Client) SearchPlaylists(ctx context.Context, query string, page, pageSize int) (*models.SearchPlaylistsResult, error) {
|
||||
result, err := c.Search(ctx, query, "playlist", page, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.Playlists, nil
|
||||
}
|
||||
|
||||
// SearchVideos searches for videos
|
||||
func (c *Client) SearchVideos(ctx context.Context, query string, page, pageSize int) (*models.SearchVideosResult, error) {
|
||||
result, err := c.Search(ctx, query, "video", page, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.Videos, nil
|
||||
}
|
||||
|
||||
// SearchUsers searches for users
|
||||
func (c *Client) SearchUsers(ctx context.Context, query string, page, pageSize int) (*models.SearchUsersResult, error) {
|
||||
result, err := c.Search(ctx, query, "user", page, pageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.Users, nil
|
||||
}
|
||||
|
||||
// SearchSuggestions gets search suggestions
|
||||
func (c *Client) SearchSuggestions(ctx context.Context, query string) ([]string, error) {
|
||||
params := map[string]string{
|
||||
"text": query,
|
||||
"type": "suggest",
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Suggestions []string `json:"suggestions"`
|
||||
}
|
||||
if err := c.request.Get(ctx, "/search/suggest", params, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.Suggestions, nil
|
||||
}
|
||||
|
||||
// SearchBanners gets search banners
|
||||
func (c *Client) SearchBanners(ctx context.Context, query string) ([]models.SearchBanner, error) {
|
||||
params := map[string]string{
|
||||
"text": query,
|
||||
"type": "banner",
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Banners []models.SearchBanner `json:"banners"`
|
||||
}
|
||||
if err := c.request.Get(ctx, "/search/banner", params, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.Banners, nil
|
||||
}
|
||||
|
||||
// SearchTags gets search tags
|
||||
func (c *Client) SearchTags(ctx context.Context, query string) ([]models.Tag, error) {
|
||||
params := map[string]string{
|
||||
"text": query,
|
||||
"type": "tag",
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Tags []models.Tag `json:"tags"`
|
||||
}
|
||||
if err := c.request.Get(ctx, "/search/tag", params, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.Tags, nil
|
||||
}
|
||||
|
||||
// SearchResult represents search results
|
||||
// This is defined in models/search.go
|
||||
Reference in new issue
Block a user