Last active
January 17, 2020 11:46
-
-
Save cmbuckley/0c92ae591eb231607dc62069ae960738 to your computer and use it in GitHub Desktop.
Get all the comments from the change.org petition for Game of Thrones season 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$base = 'https://www.change.org/api-proxy/-/comments'; | |
$options = ['commentable_type' => 'Event', 'commentable_id' => 15409786, 'role' => 'comment']; | |
$complete = false; | |
// API blocks PHP user-agent | |
$context = stream_context_create(['http' => ['header' => "User-Agent: parser/1.0.0\r\n"]]); | |
$fh = fopen('comments.csv', 'w'); | |
set_time_limit(0); | |
fputcsv($fh, ['ID', 'Date', 'Likes', 'User Language', 'Comment']); | |
$maxPages = 100; // how many pages to read (there are thousands) | |
while ($maxPages-- > 0) { | |
$query = http_build_query($options); | |
$response = json_decode(file_get_contents("$base?$query", false, $context)); | |
foreach ($response->items as $comment) { | |
fputcsv($fh, [ | |
$comment->id, | |
$comment->created_at, | |
$comment->likes, | |
$comment->user->locale, | |
$comment->comment | |
]); | |
} | |
// next request to start before our earliest comment | |
$options['before_datetime'] = array_pop($response->items)->created_at; | |
if ($response->last_page) { $maxPages = 0; } // we're done | |
usleep(200000); // 0.2s, just to avoid hammering the API | |
} | |
fclose($fh); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment