Searching Podcasts for Fun and Profit

This was originally published on the Clarify blog on 28 June 2014.

I have a confession: I’ve never been a fan of podcasts.

While I know a number of podcasters, understand the amount of work that’s involved, appreciate many of the topics, and should listen to keep up to date, I don’t. For me, there’s just a disconnect between the material covered and how I like to consume media. I want to jump to the relevant parts first and then listen to the rest at my leisure.

After a brief exchange with some of the members of the ATX Web Show about podcast production, I was struck by the simple idea:

I should use Clarify to jump directly to the parts of the podcast I want.

Luckily, since they have an RSS feed of the podcasts directly, it was trivial to grab the feed, process it, load the files, and start searching. Here are the first few steps:

<?php
require 'creds.php';
// This assumes you've used Composer to install the Clarify library.
require 'vendor/autoload.php';

$rssFeed = 'http://atxwebshow.com/feed/podcast/';
$rssString = file_get_contents($rssFeed);

$xml = simplexml_load_string($rssString);

$i = 0;
$itemArray = $xml->channel->item;
foreach ($itemArray as $item) {
    if ($i > 2) {
        break 1;
    }
    $name = $item->title;
    $description = html_entity_decode($item->description);

    $media = $item->enclosure;
    $attributes = $media->attributes();
    $mediaUrl = $attributes['url'][0];

    if (strlen($mediaUrl)) {
        $jsonData = '{"description": '.json_encode($description).'}';

        $clarify = new \Clarify\Bundle($apikey);
        $result = $clarify->create($name, $mediaUrl, $jsonData);

        if ($result['code'] == 201) {
            echo "loaded: " . $mediaUrl . "\n";
        }
    }
    $i++;
}

retrieve.php

No wait, that’s the whole thing.

Of the less than 30 lines of code, only two (lines 26 & 27) are used for the Clarify import. The rest are used to parse the RSS feed.

Next, we can search the results for various keywords. In this case, we’ll find Austin:

<?php
require 'creds.php';
// This assumes you've used Composer to install the Clarify library.
require 'vendor/autoload.php';

$audio = new \Clarify\Bundle($apikey);

$result = $audio->search('Austin');

$results = $result['item_results'];
$items = $result['_links']['items'];
foreach($items as $index => $item) {
    $bundle = $audio->load($item['href']);

    echo $bundle['_links']['self']['href'] . "\n";
    echo $bundle['name'] . "\n";

    $search_hits = $results[$index]['term_results'][0]['matches'][0]['hits'];
    foreach($search_hits as $search_hit) {
        echo $search_hit['start'] . ' -- ' . $search_hit['end'] . "\n";
    }
}

search.php

Finally, we can visualize the results using our audio player with this code below:

<?php
require 'creds.php';
// This assumes you've used Composer to install the Clarify library.
require 'vendor/autoload.php';

$audio = new \Clarify\Bundle($apikey);
$items = $audio->search($terms);

$search_terms = json_encode($items['search_terms']);
$item_results = json_encode($items['item_results']);

$audiokey = $items['_links']['items'][0]['href'];
$tracks = $audio->tracks($audiokey)['tracks'];
$mediaUrl = $tracks[0]['media_url'];

// this continues with the rest of the player code available here - https://github.com/Clarify/clarify-audio-player

audio-player.php