-
Notifications
You must be signed in to change notification settings - Fork 0
/
shazam_export.php
71 lines (53 loc) · 1.75 KB
/
shazam_export.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$dotenv = new Dotenv\Dotenv(__DIR__);
// Load from .env file
$dotenv->load();
$endpoint = 'https://amp.shazam.com/shazam/v1/en-US/BR/android/-/installation/' . getenv('SHAZAM_ID') . '/tagevents';
$apiKey = getenv('SHAZAM_KEY');
$allTags = [];
// Get tags...
echo 'Getting tags...';
do {
$tags = getTags($endpoint, $apiKey, $client);
foreach ($tags['events'] as $tag) {
array_push($allTags, $tag);
}
echo '.';
$endpoint = isset($tags['next']['url']) ? $tags['next']['url'] : null;
} while ($endpoint != null);
echo "\n";
// Encode and save to file...
saveJsonToFile('tags', $allTags);
echo count($allTags) . " tags found!\n";
// Get info for each track...
for ($i = 0; $i < count($allTags); $i++) {
$track = getTrack($allTags[$i]['tag']['key'], $client);
$allTags[$i]['track'] = $track;
echo 'Getting tracks info: ' . $i . '/' . count($allTags) . "\n";
}
saveJsonToFile('tags_tracks', $allTags);
// Get tag list from the user...
function getTags($endpoint, $apiKey, $client) {
$res = $client->request('GET', $endpoint, [
'headers' => [
'x-shazam-ampkey' => $apiKey,
]
]);
return json_decode($res->getBody(), true);
}
// Get track metadata.
function getTrack($trackId, $client) {
$res = $client->request('GET', 'https://cdn.shazam.com/discovery/v4/en-US/BR/android/-/track/' . $trackId);
return json_decode($res->getBody(), true);
}
// Save tags to json file.
function saveJsonToFile($fileName, $tags) {
$serializedTags = json_encode($tags);
$fileName = $fileName . '_' . time() . '.json';
$file = fopen($fileName, 'w');
fwrite($file, $serializedTags);
fclose($file);
}