Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: force market as request option to search for shows #258

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions countries.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ const (
CountryCanada = "CA"
CountryChile = "CL"
CountryChina = "CN"
CountryColombia = "CO"
CountryFinland = "FI"
CountryFrance = "FR"
CountryGermany = "DE"
CountryHongKong = "HK"
CountryIreland = "IE"
CountryIndia = "IN"
CountryItaly = "IT"
CountryJapan = "JP"
CountrySpain = "ES"
CountryFinland = "FI"
CountryFrance = "FR"
CountryMexico = "MX"
CountryNewZealand = "NZ"
CountryRussia = "RU"
CountrySpain = "ES"
CountrySwitzerland = "CH"
CountryUnitedArabEmirates = "AE"
CountryUnitedKingdom = "GB"
Expand Down
19 changes: 18 additions & 1 deletion examples/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package main
import (
"context"
"fmt"
spotifyauth "github.com/zmb3/spotify/v2/auth"
"log"
"os"

spotifyauth "github.com/zmb3/spotify/v2/auth"

"golang.org/x/oauth2/clientcredentials"

"github.com/zmb3/spotify/v2"
Expand Down Expand Up @@ -46,4 +47,20 @@ func main() {
fmt.Println(" ", item.Name)
}
}

// search for shows using market query parameter
options := []spotify.RequestOption{
spotify.Market("CO"),
}

results, err = client.Search(ctx, "cyclast", spotify.SearchTypeShow, options...)
if err != nil {
log.Fatal(err)
}
// handle show results
if results.Shows != nil {
for _, item := range results.Shows.Shows {
fmt.Println(" ", item.Name)
}
}
}
13 changes: 9 additions & 4 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package spotify

import (
"context"
"fmt"
"strings"
)

Expand Down Expand Up @@ -68,14 +69,14 @@ type SearchResult struct {
// search types. For example, `Search(query, SearchTypeArtist|SearchTypeAlbum)`
// will search for artists or albums matching the specified keywords.
//
// Matching
// # Matching
Santiago-Balcero marked this conversation as resolved.
Show resolved Hide resolved
//
// Matching of search keywords is NOT case sensitive. Keywords are matched in
// any order unless surrounded by double quotes. Searching for playlists will
// return results where the query keyword(s) match any part of the playlist's
// name or description. Only popular public playlists are returned.
//
// Operators
// # Operators
//
// The operator NOT can be used to exclude results. For example,
// query = "roadhouse NOT blues" returns items that match "roadhouse" but excludes
Expand All @@ -85,14 +86,14 @@ type SearchResult struct {
//
// Operators should be specified in uppercase.
//
// Wildcards
// # Wildcards
//
// The asterisk (*) character can, with some limitations, be used as a wildcard
// (maximum of 2 per query). It will match a variable number of non-white-space
// characters. It cannot be used in a quoted phrase, in a field filter, or as
// the first character of a keyword string.
//
// Field filters
// # Field filters
//
// By default, results are returned when a match is found in any field of the
// target object type. Searches can be made more specific by specifying an album,
Expand Down Expand Up @@ -124,6 +125,10 @@ func (c *Client) Search(ctx context.Context, query string, t SearchType, opts ..
v.Set("q", query)
v.Set("type", t.encode())

if t == SearchTypeShow && v.Get("market") == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is specific to searching Shows - but I'm also not sure that returning an Error here is the right thing to do. Spotify could adjust their API behaviour in future. Perhaps we just need to better document that when using Client ID/Secret you need to specify the market as there's no default market.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, better documentation is probably the way to go as we don't control the scope of these params or if/when this issue really will get fixed. we can't decode the bearer token to get user info for us to implement the fallback method can we?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't decode the bearer token to get user info for us to implement the fallback method can we?

I think we'd probably want to avoid that - even if the bearer token was decodable it'd probably be a bad idea for us to couple with that.

return nil, fmt.Errorf("searching for shows requires market parameter")
Santiago-Balcero marked this conversation as resolved.
Show resolved Hide resolved
}

spotifyURL := c.baseURL + "search?" + v.Encode()

var result SearchResult
Expand Down
29 changes: 29 additions & 0 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ func TestSearchPlaylistTrack(t *testing.T) {
}
}

func TestSearchShow(t *testing.T) {
client, server := testClientFile(http.StatusOK, "test_data/search_show.txt")
defer server.Close()

options := []RequestOption{
Market("CO"),
}

result, err := client.Search(context.Background(), "go time", SearchTypeShow, options...)
if err != nil {
t.Error(err)
}
if result.Albums != nil {
t.Error("Searched for shows but received album results")
}
if result.Playlists != nil {
t.Error("Searched for shows but received playlist results")
}
if result.Tracks != nil {
t.Error("Searched for shows but received track results")
}
if result.Shows == nil || len(result.Shows.Shows) == 0 {
t.Error("Didn't receive show results")
}
if result.Shows.Shows[0].Name != "Go Time: Golang, Software Engineering" {
t.Error("Got wrong show name")
}
}

func TestPrevNextSearchPageErrors(t *testing.T) {
client, server := testClientString(0, "")
defer server.Close()
Expand Down
49 changes: 49 additions & 0 deletions test_data/search_show.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"shows": {
"href": "https://api.spotify.com/v1/search?query=go+time&type=show&locale=es-CO%2Ces%3Bq%3D0.9%2Cen-US%3Bq%3D0.8%2Cen%3Bq%3D0.7%2Ces-419%3Bq%3D0.6&offset=0&limit=1",
"limit": 20,
"next": "https://api.spotify.com/v1/search?query=go+time&type=show&locale=es-CO%2Ces%3Bq%3D0.9%2Cen-US%3Bq%3D0.8%2Cen%3Bq%3D0.7%2Ces-419%3Bq%3D0.6&offset=1&limit=1",
"offset": 0,
"previous": null,
"total": 1,
"items": [
{
"available_markets": ["AD", "AE", "AG", "AL", "AM", "AR", "AT", "AU", "BA", "BB", "BE", "BF", "BG", "BH", "BJ", "BO", "BR", "BS", "BT", "BW", "BZ", "CA", "CH", "CL", "CO", "CR", "CV", "CW", "CY", "CZ", "DE", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FJ", "FM", "FR", "GB", "GD", "GE", "GH", "GM", "GR", "GT", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JM", "JO", "JP", "KE", "KI", "KN", "KR", "KW", "LB", "LC", "LI", "LR", "LS", "LT", "LU", "LV", "MA", "MC", "ME", "MG", "MH", "MK", "ML", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NE", "NG", "NI", "NL", "NO", "NR", "NZ", "OM", "PA", "PE", "PG", "PH", "PL", "PR", "PS", "PT", "PW", "PY", "QA", "RO", "RS", "SA", "SB", "SC", "SE", "SG", "SI", "SK", "SL", "SM", "SN", "SR", "ST", "SV", "TH", "TL", "TN", "TO", "TR", "TT", "TV", "TW", "UA", "US", "UY", "VC", "VN", "VU", "WS", "XK", "ZA"],
"copyrights": [],
"description": "Your source for diverse discussions from around the Go community. This show records LIVE every Tuesday at 3pm US Eastern. Join the Golang community and chat with us during the show in the #gotimefm channel of Gophers slack. Panelists include Mat Ryer, Jon Calhoun, Natalie Pistunovich, Johnny Boursiquot, Angelica Hill, Kris Brandow, and Ian Lopshire. We discuss cloud infrastructure, distributed systems, microservices, Kubernetes, Docker… oh and also Go! Some people search for GoTime or GoTimeFM and can’t find the show, so now the strings GoTime and GoTimeFM are in our description too.",
"html_description": "Your source for diverse discussions from around the Go community. This show records LIVE every Tuesday at 3pm US Eastern. Join the Golang community and chat with us during the show in the #gotimefm channel of Gophers slack. Panelists include Mat Ryer, Jon Calhoun, Natalie Pistunovich, Johnny Boursiquot, Angelica Hill, Kris Brandow, and Ian Lopshire. We discuss cloud infrastructure, distributed systems, microservices, Kubernetes, Docker… oh and also Go! Some people search for GoTime or GoTimeFM and can’t find the show, so now the strings GoTime and GoTimeFM are in our description too.",
"explicit": false,
"external_urls": {
"spotify": "https://open.spotify.com/show/2cKdcxETn7jDp7uJCwqmSE"
},
"href": "https://api.spotify.com/v1/shows/2cKdcxETn7jDp7uJCwqmSE",
"id": "2cKdcxETn7jDp7uJCwqmSE",
"images": [
{
"url": "https://i.scdn.co/image/18d9cacf366a7173b10204eb983fe5d374b728dd",
"height": 640,
"width": 640
},
{
"url": "https://i.scdn.co/image/3f9b1dd55b3353a0d70dfcca6a44c01775cfbfcc",
"height": 300,
"width": 300
},
{
"url": "https://i.scdn.co/image/aa9e23de8a5a64bedaf526bb963123413984e53f",
"height": 64,
"width": 64
}
],
"is_externally_hosted": false,
"languages": ["en-US"],
"media_type": "audio",
"name": "Go Time: Golang, Software Engineering",
"publisher": "Changelog Media",
"type": "show",
"uri": "spotify:show:2cKdcxETn7jDp7uJCwqmSE",
"total_episodes": 316
}
]
}
}