可以保存Spotify搜索API的结果吗?
阅读Spotify Web API的TOS,开发人员不允许在创建数据库时从API中汇总数据。 我不知道我试图完成的是否是“聚合”。
我有一个网站,允许用户在婚礼上播放歌曲。 我让他们输入歌曲名称,艺术家和专辑名称,以便DJ能够轻松找到音乐。 所有这些都是用户提供的。 这些歌曲然后由新娘/新郎批准并由其他客人投票生成播放列表,以便DJ知道什么音乐将在该事件中受欢迎。
我想提供的是一种让用户使用这些信息的方法,并且能够搜索Spotify上的前几个搜索结果,选择正确的轨道,并将Spotify跟踪与他们的建议相关联。 这让其他客人听到他们建议的歌曲,如果他们不熟悉它,并允许管理员根据新娘/新郎的口味允许或禁止歌曲。
为了避免API调用超出限制,我希望能够将搜索结果返回的Spotify URI与用户提供的歌曲信息一起存储,以便我可以在网站上为所建议的歌曲生成一个播放按钮。
这是否算作聚合,还是在网络搜索API的当前TOS下允许?
你在做什么听起来很好。
您询问的TOS部分是为了防止人们制作自动化工具,无需用户交互即可刮取Spotify目录。 如果您正在编写“正常”应用程序并从Spotify API中缓存数据,而这些数据是由用户实际执行搜索,浏览等操作的结果,则不存在任何问题。
来源:我在Spotify工作。
我使用这个:
<?php
namespace AppServices;
use DB;
use Exception;
use AppGenre;
use AppAlbum;
use AppArtist;
use IlluminateSupportStr;
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);
}
}
链接地址: http://www.djcxy.com/p/9465.html