Skip to content

Instantly share code, notes, and snippets.

@chucknado
Last active October 12, 2015 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chucknado/6e00d1dcfc2d7f85aac1 to your computer and use it in GitHub Desktop.
Save chucknado/6e00d1dcfc2d7f85aac1 to your computer and use it in GitHub Desktop.
Completed script for for "Getting large datasets with the Zendesk API and Perl" at https://support.zendesk.com/hc/en-us/articles/212521277
use strict;
use warnings;
no warnings 'utf8';
use LWP;
use JSON;
use MIME::Base64;
use Storable;
my $topic_id = 123456;
my $zendesk = 'your_zendesk_url';
my $credentials = encode_base64('your_zendesk_email:your_zendesk_password');
my $url = $zendesk . '/api/v2/community/topics/' . $topic_id . '/posts.json?include=users';
my $ua = LWP::UserAgent->new(ssl_opts =>{ verify_hostname => 0 });
my @topic_posts;
my @user_list;
while ($url) {
my $response = $ua->get($url, 'Authorization' => "Basic $credentials");
if ($response->code() == 429){
print "Rate limited! Please wait.\n";
sleep($response->header('retry-after'));
next;
}
die 'http status: ' . $response->code . ' ' . $response->message
unless ($response->is_success);
my $data = decode_json($response->content);
push @topic_posts, @{ $data->{'posts'} };
push @user_list, @{ $data->{'users'} };
if (defined $data->{'next_page'}) {
$url = $data->{'next_page'};
} else {
$url = '';
}
}
my %topic_data = (posts => \@topic_posts, users => \@user_list );
store \%topic_data, 'my_serialized_data_file';
print "Data serialized.\n";
my $topic_hash_ref = retrieve('my_serialized_data_file');
my %topic_data = %{ $topic_hash_ref };
foreach my $post ( @{ $topic_data{'posts'} } ) {
my $author = 'anonymous';
foreach my $user ( @{ $topic_data{'users'} } ) {
if ( $user->{'id'} == $post->{'author_id'} ) {
$author = $user->{'name'};
last;
}
}
print "\"$post->{'title'}\" by $author\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment