how much do I need to clean the search query?

I'm trying to search Spotify for metadata for tracks. I'm trying to see if Spotify has a song from a YouTube video. This is the URL template I use to make the search:

https://api.spotify.com/v1/search?q="MY SEARCH QUERY"&type=artist,track.

Since it's hard to determine what part of the search query is the artist or the track, I add those 2 as types for my search query.

However, when I get a YouTube video with a title like this: "Katy Perry - This How We Do (Official Video) [Letra Español - Lyrics English]" and use it as my search term I get 0 results.

https://api.spotify.com/v1/search?q=Katy%20Perry%20-%20This%20How%20We%20Do%20(Official%20Video)%20[Letra%20Espa%C3%B1ol%20-%20Lyrics%20English]&type=artist,track

If I manually "clean" it and my search query is instead: "Katy Perry - This How We Do", I get the result I'm looking for.

https://api.spotify.com/v1/search?q=Katy%20Perry%20-%20This%20How%20We%20Do&type=artist,track

Given this example is there any way in which the first query (as is without any cleaning) will yield results from the Spotify API? If not, what do I need to do to my search terms before sending them to the Spotify search API so that it has a greater chance of yielding results?


You might be able to make use of the NOT operator to mitigate anything that will potentially give you a bad result.

However, you'd have to check the search string to add the NOT operator in the correct place anyway, so you might as well take out anything that isn't a "normal" part of a track.

could use a regex for that (JS):

// evil search string
var search = "Katy Perry - This How We Do (Official Video) [Letra Español - Lyrics English]";

// good search string --> Katy Perry - This How We Do
var cleaned = search.replace(/(.*?)|[.*?]/g, "");

I think the easiest way is to obtain a clean artist name and track name from Youtube, rather than trying to find out rules about how tracks are named on Spotify.

This information is shown in music videos on Youtube, in a panel below the video (see <div id="action-panel-details"></div> ).

This information seems to be accessible using the Youtube API. Suppose we are searching on Spotify information about the track in the video https://www.youtube.com/watch?v=VhUrCrR4yiw :

1) First, fetch the information about the video using its id (in this case VhUrCrR4yiw ) and asking for its topicDetails . You can try this out going to https://developers.google.com/youtube/v3/docs/videos/list . Input the previous id in the id field, and topicDetails in the part field.

2) Read the item's topicDetails.topicIds :

...
"topicDetails": {
    "topicIds": [
        "/m/03y82t6",
        "/m/0y87l_h"
    ],
...

3) Use them to query Freebase database to get information about the topics. For a quick test, you can use their website. The topics returned from the Youtube API point to http://www.freebase.com/m/03y82t6 (Katy Perry) and http://www.freebase.com/m/0y87l_h (This Is How We Do). In fact, the track itself contains information about the artist.


Here is a suggestion that hopefully helps.

Most music videos on YouTube have the following naming convention: ARTIST_NAME - SONG_NAME

Examples:
Katy Perry - Dark Horse (Official) ft. Juicy J
Iggy Azalea - Fancy (Explicit) ft. Charli XCX

Spotify search, on the other hand, uses the following convention (see reference):

q=SONG_NAME+artist:ARTIST_NAME&type=track

The "cleaning process" depends on what language you are using to process your data, and simply involves "parsing" the YouTube title so you can better build the proper Spotify search query by rearranging the information. For example in PHP, it would roughly look something like this:

$youtube_title = "Katy Perry - Dark Horse (Official) ft. Juicy J";
$arr = explode (" - ",$youtube_title); // split title and artist
$spotify_search = "https://api.spotify.com/v1/search?q="+$arr[1]." artist:".$arr[0]."&type=track";

That's a rough representation of how to get this done in PHP (I left out quotation marks and encoding for simplicity sake). If you share the language you are using, someone can probably provide a example specific to your programming language.

You can check out the Spotify Web API Search Reference.

链接地址: http://www.djcxy.com/p/55650.html

上一篇: Spotify API相当于Echonest顶级艺术家的流派

下一篇: 我需要多少钱来清理搜索查询?