From 11d4b28626aec90f2867d2122e727ec055bea11e Mon Sep 17 00:00:00 2001 From: GeneZC Date: Tue, 6 Jun 2023 11:00:48 +0800 Subject: [PATCH] Add spotify --- bmtools/tools/__init__.py | 1 + bmtools/tools/spotify/__init__.py | 7 +++++ bmtools/tools/spotify/api.py | 47 +++++++++++++++++++++++++++++++ bmtools/tools/spotify/readme.md | 5 ++++ bmtools/tools/spotify/test.py | 11 ++++++++ host_local_tools.py | 1 + 6 files changed, 72 insertions(+) create mode 100644 bmtools/tools/spotify/__init__.py create mode 100644 bmtools/tools/spotify/api.py create mode 100644 bmtools/tools/spotify/readme.md create mode 100644 bmtools/tools/spotify/test.py diff --git a/bmtools/tools/__init__.py b/bmtools/tools/__init__.py index ac7f21b..28d0f87 100644 --- a/bmtools/tools/__init__.py +++ b/bmtools/tools/__init__.py @@ -30,6 +30,7 @@ from . import gradio_tools from . import travel from . import walmart +from . import spotify from .tool import Tool diff --git a/bmtools/tools/spotify/__init__.py b/bmtools/tools/spotify/__init__.py new file mode 100644 index 0000000..53d435b --- /dev/null +++ b/bmtools/tools/spotify/__init__.py @@ -0,0 +1,7 @@ + +from ..registry import register + +@register("spotify") +def spotify(): + from .api import build_tool + return build_tool diff --git a/bmtools/tools/spotify/api.py b/bmtools/tools/spotify/api.py new file mode 100644 index 0000000..7a2f6be --- /dev/null +++ b/bmtools/tools/spotify/api.py @@ -0,0 +1,47 @@ +import requests +import json +from datetime import date, datetime, timedelta +import os +from ..tool import Tool + +from typing import Optional, Dict, Union, List + + +def build_tool(config) -> Tool: + tool = Tool( + "Spotify", + "Search over millions of songs & podcasts, artists, albums, playlists and more.", + name_for_model="Spotify", + description_for_model="Plugin for looking up songs & podcasts, artists, albums, playlists and more.", + logo_url="https://your-app-url.com/.well-known/logo.png", + contact_email="hello@contact.com", + legal_info_url="hello@legal.com" + ) + + BASE_URL = "https://spotify23.p.rapidapi.com" + KEY = config["subscription_key"] + HEADERS = { + "X-RapidAPI-Key": KEY, + "X-RapidAPI-Host": "spotify23.p.rapidapi.com" + } + + @tool.get("/spotify_search") + def spotify_search(query: str, query_type: str) -> dict: + """ + Search for songs & podcasts, artists, albums, playlists and more on Spotify. + + :param query: Free-form spotify search query. + :param query_type: Type of the query, `albums`, `artists`, etc., best with `multi`. + :return: A dictionary with the response from the API. + """ + + query_params = { + "q": query, + "type": query_type, + } + + response = requests.get(BASE_URL + "/search/", headers=HEADERS, params=query_params) + + return response.json() + + return tool diff --git a/bmtools/tools/spotify/readme.md b/bmtools/tools/spotify/readme.md new file mode 100644 index 0000000..914f7d1 --- /dev/null +++ b/bmtools/tools/spotify/readme.md @@ -0,0 +1,5 @@ +# Spotify + +Contributor: [Chen Zhang](https://github.com/GeneZC) + +You can get your RAIPID key here: https://rapidapi.com/hub diff --git a/bmtools/tools/spotify/test.py b/bmtools/tools/spotify/test.py new file mode 100644 index 0000000..01838aa --- /dev/null +++ b/bmtools/tools/spotify/test.py @@ -0,0 +1,11 @@ +from bmtools.agent.singletool import load_single_tools, STQuestionAnswerer + +tool_name, tool_url = 'Spotify', "http://127.0.0.1:8079/tools/spotify/" +tools_name, tools_config = load_single_tools(tool_name, tool_url) +print(tools_name, tools_config) + +qa = STQuestionAnswerer() + +agent = qa.load_tools(tools_name, tools_config) + +agent("List famous songs of coldplay.") \ No newline at end of file diff --git a/host_local_tools.py b/host_local_tools.py index 07a6080..bfdf08b 100644 --- a/host_local_tools.py +++ b/host_local_tools.py @@ -70,6 +70,7 @@ def load_rapidapi_tool(): server.load_tool("zillow", {"subscription_key": RAPIDAPI_KEY}) server.load_tool("airbnb", {"subscription_key": RAPIDAPI_KEY}) server.load_tool("job_search", {"subscription_key": RAPIDAPI_KEY}) + server.load_tool("spotify", {"subscription_key": RAPIDAPI_KEY}) # def load_nllb_translation_tool(): # server.load_tool("nllb-translation")