是否可以保存Spotify搜索API的结果?

时间:2013-08-15 16:59:37

标签: spotify

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

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

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

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

这是否算作聚合,或者在网络搜索API的当前TOS下是否允许?

2 个答案:

答案 0 :(得分:6)

你在做什么听起来很不错。

您要问的服务条款部分是为了防止人们制作自动化工具,在没有用户交互的情况下刮掉Spotify目录。如果您正在编写“普通”应用程序并且由于用户实际执行搜索,浏览等操作而从Spotify API缓存数据,那么您就没有问题。

资料来源:我在Spotify工作。

答案 1 :(得分:-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);
    }
}