-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add search action for spotify #18791
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
Open
bontaq
wants to merge
1
commit into
PipedreamHQ:master
Choose a base branch
from
bontaq:add-search-for-spotify
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { | ||
axios, | ||
ConfigurationError, | ||
} from "@pipedream/platform"; | ||
import get from "lodash/get.js"; | ||
import spotify from "../../spotify.app.mjs"; | ||
import { | ||
ITEM_TYPES, | ||
ITEM_TYPES_RESULT_NAME, | ||
} from "../../consts.mjs"; | ||
|
||
export default { | ||
key: "spotify-search", | ||
name: "Search", | ||
description: "Search for items on Spotify (tracks, albums, artists, playlists, shows, or episodes). [See the docs here](https://developer.spotify.com/documentation/web-api/reference/search)", | ||
version: "0.0.1", | ||
type: "action", | ||
annotations: { | ||
destructiveHint: false, | ||
openWorldHint: true, | ||
readOnlyHint: true, | ||
}, | ||
props: { | ||
spotify, | ||
query: { | ||
type: "string", | ||
label: "Search Query", | ||
description: "Search query keywords and optional field filters. [See the Spotify docs for query syntax](https://developer.spotify.com/documentation/web-api/reference/search)", | ||
}, | ||
type: { | ||
type: "string[]", | ||
label: "Search Type", | ||
description: "Type(s) of items to search for", | ||
options: [ | ||
{ | ||
label: "Album", | ||
value: ITEM_TYPES.ALBUM, | ||
}, | ||
{ | ||
label: "Artist", | ||
value: ITEM_TYPES.ARTIST, | ||
}, | ||
{ | ||
label: "Playlist", | ||
value: ITEM_TYPES.PLAYLIST, | ||
}, | ||
{ | ||
label: "Track", | ||
value: ITEM_TYPES.TRACK, | ||
}, | ||
{ | ||
label: "Show", | ||
value: ITEM_TYPES.SHOW, | ||
}, | ||
{ | ||
label: "Episode", | ||
value: ITEM_TYPES.EPISODE, | ||
}, | ||
], | ||
default: [ | ||
ITEM_TYPES.TRACK, | ||
], | ||
}, | ||
market: { | ||
propDefinition: [ | ||
spotify, | ||
"market", | ||
], | ||
optional: true, | ||
}, | ||
limit: { | ||
propDefinition: [ | ||
spotify, | ||
"limit", | ||
], | ||
description: "The maximum number of results to return per type.", | ||
default: 20, | ||
max: 50, | ||
}, | ||
offset: { | ||
propDefinition: [ | ||
spotify, | ||
"offset", | ||
], | ||
}, | ||
includeExternal: { | ||
type: "string", | ||
label: "Include External", | ||
description: "If `audio` is specified, the response will include any relevant audio content that is hosted externally.", | ||
optional: true, | ||
options: [ | ||
"audio", | ||
], | ||
}, | ||
}, | ||
async run({ $ }) { | ||
const { | ||
query, | ||
type, | ||
market, | ||
limit, | ||
offset, | ||
includeExternal, | ||
} = this; | ||
|
||
if (!query || query.trim().length === 0) { | ||
throw new ConfigurationError("Search `query` cannot be empty"); | ||
} | ||
|
||
const types = type; | ||
bontaq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!types?.length) { | ||
throw new ConfigurationError("Select at least one search type"); | ||
} | ||
|
||
const res = await axios($, this.spotify._getAxiosParams({ | ||
method: "GET", | ||
path: "/search", | ||
params: { | ||
q: query, | ||
type: types.join(","), | ||
market, | ||
limit, | ||
offset, | ||
include_external: includeExternal, | ||
}, | ||
timeout: 15000, | ||
})); | ||
|
||
// Collect all items from the response across all search types | ||
const allItems = types.flatMap( | ||
(itemType) => get(res, `${ITEM_TYPES_RESULT_NAME[itemType]}.items`, []), | ||
); | ||
|
||
const typeLabel = types.length > 1 | ||
? "items" | ||
: types[0] + (allItems.length !== 1 | ||
? "s" | ||
: ""); | ||
|
||
$.export("$summary", `Successfully found ${allItems.length} ${typeLabel} matching "${query}"`); | ||
|
||
return res; | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.