Skip to content

Instantly share code, notes, and snippets.

@CodeBrauer
Created September 30, 2015 16:28
Show Gist options
  • Select an option

  • Save CodeBrauer/5bbc8d68481d015e8706 to your computer and use it in GitHub Desktop.

Select an option

Save CodeBrauer/5bbc8d68481d015e8706 to your computer and use it in GitHub Desktop.
youtube playlist to spotify uri
{
"require": {
"google/apiclient": "^1.1",
"jwilsson/spotify-web-api-php": "^0.10.0"
}
}
<?php
date_default_timezone_set('Europe/Berlin');
require 'vendor/autoload.php';
include '../ref/ref.php';
$spotify_api = new SpotifyWebAPI\SpotifyWebAPI();
// var_dump($spotify_api->search('song 2', 'track')->tracks->items[0]->uri);die();
$client = new Google_Client();
$client->setDeveloperKey('AIzaSyDK9nsZda4XRoPq-mJeh97Xu2MRO1f27xs');
$youtube = new Google_Service_YouTube($client);
// r($youtube);
if (isset($_POST['yt-playlist'])) {
$nextPageToken = NULL;
$result = [];
// crawl youtube to get IDs + Track Names
do {
$playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
'playlistId' => rawurlencode($_POST['yt-playlist']),
'maxResults' => 50,
'pageToken' => $nextPageToken));
foreach ($playlistItemsResponse['items'] as $playlistItem) {
$spotify_uri = $spotify_api->search($playlistItem['snippet']['title'], 'track')->tracks->items[0]->uri;
if (is_string($spotify_uri)) {
$result[] = $spotify_uri;
}
}
$nextPageToken = $playlistItemsResponse['nextPageToken'];
} while ($nextPageToken != NULL);
$result_str = implode("\n", $result);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>YT2SPOTIFY</title>
<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.5/paper/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>YT2Spotify</h1>
<div class="row">
<div class="col-md-3">
<p>
<strong>Example ID:</strong><br><code>PLhzswTGE9_z92Pm5HF-_A-YYR1dOuuUL-</code>
</p>
</div>
<div class="col md-6">
<form class="form-inline" method="post">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">https://www.youtube.com/playlist?list=</div>
<input type="text" class="form-control" name="yt-playlist" placeholder="Playlist ID">
</div>
</div>
<button type="submit" class="btn btn-primary">GET Spotify URIs</button>
</form>
</div>
<div class="col-md-3">
<?php if (isset($result_str)): ?>
<h3>Result:</h3>
<textarea rows="40" class="form-control"></textarea>
<?php endif ?>
</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment