可以接受保存 Spotify 搜索 API 的结果吗?

问题描述 投票:0回答:2

阅读 Spotify Web API 的 TOS,不允许开发人员在创建数据库时聚合来自 API 的数据。我不知道我想要完成的事情是否算作“聚合”。

我有一个网站,允许用户对婚礼上播放的歌曲提出建议。我让他们输入歌曲名称、艺术家和专辑名称,以便 DJ 可以轻松找到音乐。所有这些都是用户提供的。然后这些歌曲会得到新娘/新郎的认可,并由其他客人投票生成播放列表,以便 DJ 知道活动中会流行哪些音乐。

我想提供的是一种让用户使用该信息的方式,并且能够搜索 Spotify 上的前几个搜索结果,选择正确的曲目,并将 Spotify 曲目与他们的建议相关联。这可以让其他客人在不熟悉的情况下听到他们建议的歌曲,并允许管理员根据新娘/新郎的品味允许或禁止该歌曲。

为了避免 API 调用超出速率限制,我希望能够存储从搜索结果返回的 Spotify URI 以及用户提供的歌曲信息,以便我可以在网站上为建议的歌曲生成播放按钮。

这是否算作聚合,或者根据网络搜索 API 的当前 TOS 是否允许这样做?

api spotify
2个回答
7
投票

你所做的听起来不错。

您所询问的 TOS 部分是为了防止人们制作无需用户交互即可抓取 Spotify 目录的自动化工具。如果您正在编写一个“普通”应用程序,并在用户实际执行搜索、浏览等操作时从 Spotify API 缓存数据,那么您就没有问题。

来源:我在 Spotify 工作。


-1
投票

我用的是这个:

<?php
namespace App\Services;

use DB;
use Exception;
use App\Genre;
use App\Album;
use App\Artist;
use Illuminate\Support\Str;

class ArtistSaver {

    /**
     * Save artist to database and return it.
     * 
     * @param  array $data
     * @return Artist
     */
    public function save($data)
    {
        $artist = Artist::whereName($data['mainInfo']['name'])->first();

        if ( ! $artist) {
            $artist = Artist::create($data['mainInfo']);
        } else {
            $artist->fill($data['mainInfo'])->save();
        }

        $this->saveAlbums($data, $artist);

        if (isset($data['albums'])) {
            $this->saveTracks($data['albums'], $artist);
        }

        if (isset($data['similar'])) {
            $this->saveSimilar($data['similar'], $artist);
        }

        if (isset($data['genres']) && ! empty($data['genres'])) {
            $this->saveGenres($data['genres'], $artist);
        }

        return $artist;
    }

    /**
     * Save and attach artist genres.
     *
     * @param array $genres
     * @param Artist $artist
     */
    public function saveGenres($genres, $artist) {

        $existing = Genre::whereIn('name', $genres)->get();
        $ids = [];

        foreach($genres as $genre) {
            $dbGenre = $existing->filter(function($item) use($genre) { return $item->name === $genre; })->first();

            //genre doesn't exist in db yet, so we need to insert it
            if ( ! $dbGenre) {
                try {
                    $dbGenre = Genre::create(['name' => $genre]);
                } catch(Exception $e) {
                    continue;
                }
            }

            $ids[] = $dbGenre->id;
        }

        //attach genres to artist
        $artist->genres()->sync($ids, false);
    }

    /**
     * Save artists similar artists to database.
     *
     * @param $similar
     * @param $artist
     * @return void
     */
    public function saveSimilar($similar, $artist)
    {
        $names = array_map(function($item) { return $item['name']; }, $similar);

        //insert similar artists that don't exist in db yet
        $this->saveOrUpdate($similar, array_flatten($similar), 'artists');

        //get ids in database for artist we just inserted
        $ids = Artist::whereIn('name', $names)->lists('id');

        //attach ids to given artist
        $artist->similar()->sync($ids);
    }

    /**
     * Save artist albums to database.
     * 
     * @param  array $data  
     * @param  Artist|null $artist
     * $param  int|null
     * @return void      
     */
    public function saveAlbums($data, $artist = null, $albumId = null)
    {
        if (isset($data['albums']) && count($data['albums'])) {
            $b = $this->prepareAlbumBindings($data['albums'], $artist, $albumId);
            $this->saveOrUpdate($b['values'], $b['bindings'], 'albums');
        }
    }

    /**
     * Save albums tracks to database.
     * 
     * @param  array $albums
     * @param  Artist|null $artist
     * @param  Album|null $trackAlbum
     * @return void
     */
    public function saveTracks($albums, $artist, $tracksAlbum = null)
    {
        if ( ! $albums || ! count($albums)) return;

        $tracks = [];

        foreach($albums as $album) {
            if ( ! isset($album['tracks']) || empty($album['tracks'])) continue;

            if ($tracksAlbum) {
                $id = $tracksAlbum['id'];
            } else {
                $id = $this->getIdFromAlbumsArray($album['name'], $artist['albums']);
            }

            foreach($album['tracks'] as $track) {
                $track['album_id'] = $id;
                $tracks[] = $track;
            }
        }

        if ( ! empty($tracks)) {
            $this->saveOrUpdate($tracks, array_flatten($tracks), 'tracks');
        }
    }

    private function getIdFromAlbumsArray($name, $albums) {
        $id = false;

        foreach($albums as $album) {
            if ($name === $album['name']) {
                $id = $album['id']; break;
            }
        }

        if ( ! $id) {
            foreach($albums as $album) {
                if (Str::slug($name) == Str::slug($album['name'])) {
                    $id = $album['id']; break;
                }
            }
        }

        return $id;
    }

    /**
     * Unset tracks key from album arrays and flatten them into single array.
     * 
     * @param  array $albums
     * @param  Artist|null $artist
     * @param  int|null $albumId
     * @return array       
     */
    private function prepareAlbumBindings($albums, $artist = null, $albumId = null)
    {
        $flat = [];

        foreach($albums as $k => $album) {
            if (isset($albums[$k]['tracks'])) unset($albums[$k]['tracks']);

            if ( ! isset($albums[$k]['artist_id']) || ! $albums[$k]['artist_id']) {
                $albums[$k]['artist_id'] = $artist ? $artist->id : 0;
            }

            //can't insert null into auto incrementing id because
            //mysql will increment the id instead of keeping the old one
            if ($albumId) {
                $albums[$k]['id'] = $albumId;
            }

            foreach($albums[$k] as $name => $data) {
                if ($name !== 'tracks') {
                    $flat[] = $data;
                }
            }
        }


        return ['values' => $albums, 'bindings' => $flat];
    }

    /**
     * Compiles insert on duplicate update query for multiple inserts.
     *
     * @param array  $values
     * @param array  $bindings
     * @param string $table
     *
     * @return void
     */
    public function saveOrUpdate(array $values, $bindings, $table)
    {
        if (empty($values)) return;

        $first = head($values);

        //count how many inserts we need to make
        $amount = count($values);

        //count in how many columns we're inserting
        $columns = array_fill(0, count($first), '?');

        $columns = '(' . implode(', ', $columns) . ') ';

        //make placeholders for the amount of inserts we're doing
        $placeholders = array_fill(0, $amount, $columns);
        $placeholders = implode(',', $placeholders);

        $updates = [];

        //construct update part of the query if we're trying to insert duplicates
        foreach ($first as $column => $value) {
            $updates[] = "$column = COALESCE(values($column), $column)";
        }

        $prefixed = DB::getTablePrefix() ? DB::getTablePrefix().$table : $table;

        $query = "INSERT INTO {$prefixed} " . '(' . implode(',' , array_keys($first)) . ')' . ' VALUES ' . $placeholders .
            'ON DUPLICATE KEY UPDATE ' . implode(', ', $updates);

        DB::statement($query, $bindings);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.