Skip to content

Instantly share code, notes, and snippets.

@corypratt
Last active March 14, 2019 22:06
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 corypratt/6a6ce38c3d945640d38f2b40f8569cd2 to your computer and use it in GitHub Desktop.
Save corypratt/6a6ce38c3d945640d38f2b40f8569cd2 to your computer and use it in GitHub Desktop.
Wordpress function to get episode stats from Simplecast
<?php
/*
* This function can be used to pull total episode stats from Simplecast
*
*/
function cp_get_episode_stats( $podcast_id ) {
if ( ! $podcast_id) {
return "-";
}
$podcast = $podcast_id;
$url = 'https://api.simplecast.com/v1/podcasts/' . $podcast . '/statistics.json';
$api_key = 'REPLACE WITH YOUR API KEY';
$headers = array( 'X-API-KEY' => $api_key );
$request = wp_remote_get( $url, array( 'headers' => $headers ) );
if (is_wp_error($request)) {
return "-";
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
$listens = $data->total_listens;
if ($listens > 999 && $listens <= 9999) {
$result = floor($listens / 1000) . 'K';
} elseif ($listens > 9999 && $listens <= 999999) {
$result = round($listens/1000, 1) . 'K';
} elseif ($listens > 999999) {
$result = floor($listens / 1000000) . 'M';
} else {
$result = $listens;
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment