-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_comments.php
67 lines (50 loc) · 1.84 KB
/
get_comments.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
<?php
$client_id = "51295b2530f1b1654966c0b4b64eaeca";
$track_id = 87274383;
//get the number of comments
$url = "https://api.soundcloud.com/tracks/".$track_id.".json?client_id=".$client_id;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($curl);
curl_close($curl);
$data = json_decode($result, TRUE);
//Most results from our API are returned as a collection.
//The number of items in the collection returned is limited to 50 by default.
//Most endpoints support limit and offset parameters that allow your app to page through collections.
//When you receive 0 items in a response, you can assume that you've reached the end of the collection.
//The maximum value is 200 for limit and 8000 for offset.
$comment_count = min($data["comment_count"], 8000);
$comment_total = 0;
$limit = 200;
$offset = 0;
$call_limit = ceil($comment_count / $limit);
$sleep_time = 0.5;
$file = fopen("comments.txt","w");
for($i=0; $i<$call_limit; $i++)
{
$url = "https://api.soundcloud.com/tracks/".$track_id."/comments.json?client_id=".$client_id."&offset=".$offset."&limit=".$limit;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($curl);
curl_close($curl);
$data = json_decode($result, TRUE);
foreach($data as $item)
{
$json = json_encode($item) . "\n";
fwrite($file, $json);
$comment_total++;
if($comment_total >= $comment_count)
{
printf("done!\n");
break 2;
}
}
echo("pulled $limit comments from offset $offset... total comments: $comment_total / $comment_count \n");
$offset += $limit;
//just so that soundcloud doesn't hate me for calling their api over and over
sleep($sleep_time);
}
fclose($file);
?>