-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt2spotify.php
43 lines (37 loc) · 1.23 KB
/
txt2spotify.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
<?php
$spotify_lookup_uri = "http://ws.spotify.com/search/1/track.json?q=";
function get_json_api($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
if(strlen(curl_error($ch) > 0))
return Array('status'=>'error', 'error'=>curl_error($ch));
// als response te json_decoden is, gedecodeerd teruggeven, anders status error
if($d = json_decode($response, true))
return $d;
else
return Array('status'=>'error', 'error'=>'http status ' . $info['http_code']);
}
$username = (isset($argv[1])) ? $argv[1] : 'quarkness';
$kind = (isset($argv[2])) ? $argv[2] : 'weeklytrackchart';
$txt_filename = "{$kind}.{$username}.txt";
$spotify_filename = "{$kind}.{$username}.spotify";
$h_txt = fopen($txt_filename, 'r');
$h_spotify = fopen($spotify_filename, 'w');
//$line = 'smurfen dierentuin';
while (($line = fgets($h_txt)) !== false)
{
$terms = urlencode(trim(str_replace(' - ', ' ', $line)));
if(strlen($terms) > 0)
{
$url = $spotify_lookup_uri . $terms;
$spotify_response = get_json_api($url);
if(isset($spotify_response['tracks'][0]))
{
fputs($h_spotify, "{$spotify_response['tracks'][0]['href']}\n");
}
}
}