Author Topic: Adding Podcasts Programmatically  (Read 4478 times)

machouinard

  • Newbie
  • Posts: 8
Adding Podcasts Programmatically
« on: May 10, 2012, 01:37:16 pm »
Hello.  I've been doing some work for a not-for-profit and they would like to start publishing their podcasts.  Looks like PowerPress will work perfectly for this.  Is there a way to programmatically add the .mp3 files to posts?  There are dozens of .mp3 files to deal with and I'd prefer to not do them manually, one at a time.  I'm rather new to WP and am investigating using the API to add posts, but was wondering how to add the data for PowerPress to the post. 

A nudge in the right direction would be terrific.  PHP, Linux CLI, anything.

Thanks,
Mark

angelo

  • CIO, RawVoice
  • Administrator
  • Hero Member
  • *****
  • Posts: 4483
Re: Adding Podcasts Programmatically
« Reply #1 on: May 11, 2012, 10:12:49 am »
Is the media URL already in the blog post content? If so, you can after the fact use the Blogger/Movable Type Import Episodes tool under PowerPress > Tools menu. Essentially blogger/movable type store the podcast as a URL link in the post content, so if it's there already then you're all set to import with one click. IF you have multiple media files in a post, the import tool will also let you map episodes to specific podcast channels, and it will populate the file size and duration information if you select the right checkbox.

machouinard

  • Newbie
  • Posts: 8
Re: Adding Podcasts Programmatically
« Reply #2 on: May 13, 2012, 04:54:19 am »
Thanks for the reply.  I guess in my haste to post I wasn't as clear as I should have been.  I'm trying to figure out a way to programmatically create the initial blog post and include the media url for the podcast episode.  The podcasts are stored on another server and everything works great when I include the media url while creating the post manually.  I can successfully use the following code in a PHP script to add a post.  What do I need to add to include the media url?  That is, I'm assuming I can add something to the $content array, I just don't know what the parameters are named. 

Code: [Select]
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8') {
    $title = htmlentities($title,ENT_NOQUOTES,$encoding);
    $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
    $content = array(
        'title'=>$title,
        'description'=>$body,
        'mt_allow_comments'=>1,
        'mt_allow_pings'=>0,
        'post_type'=>'post',
        'mt_keywords'=>$keywords,
        'categories'=>array($category)
    );
    $params = array(0,$username,$password,$content,true);
    $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
    //$request = xmlrpc_encode_request('blogger.newPost',$params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpcurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

In my minimal experience with WordPress and PowerPress I believe for a very simple podcast setup I just need to create a post for each media file, and include the media url in the post.  I'll be learning much more about both WP and PP in the very near future but for now I just need this basic functionality.

Thanks for indulging me.

angelo

  • CIO, RawVoice
  • Administrator
  • Hero Member
  • *****
  • Posts: 4483
Re: Adding Podcasts Programmatically
« Reply #3 on: May 14, 2012, 09:44:19 am »
Okay so you want to be able to push the episode via the XML-RPC protocol? Is this script going to run on the same server as the destination WordPress blog, or remotely?

machouinard

  • Newbie
  • Posts: 8
Re: Adding Podcasts Programmatically
« Reply #4 on: May 14, 2012, 10:30:21 am »
Yes, it looks like XML-RPC is the way to go.  The script can be run either locally or remotely.  I started looking into plugin creation, but Mother's Day weekend had me busy.

EDIT:  I don't believe it's material, but there are now copies of all the media files stored locally on the server as well as on a remote server.  I don't believe bandwidth will be an issue serving files locally at this point as there is currently a very limited audience.  That's probably all beside the point, but thought I'd mention it nonetheless.
« Last Edit: May 14, 2012, 12:25:46 pm by machouinard »

angelo

  • CIO, RawVoice
  • Administrator
  • Hero Member
  • *****
  • Posts: 4483
Re: Adding Podcasts Programmatically
« Reply #5 on: May 14, 2012, 08:31:23 pm »
If your using XML-RPC, you need to use the custom fields option.

The field name is called 'enclosure' and the value is 3 lines in the following specific order:
Code: [Select]
$enclosure_value = $enclosure['url'];
$enclosure_value .= "\n";
$enclosure_value .= $enclosure['filesize'];
$enclosure_value .= "\n";
$enclosure_value .= $enclosure['type'];

In the XML-RCP library I wrote, the value is appended to a custom fields array like so...
Code: [Select]
$custom_fields[] = array('key'=>'enclosure', 'value'=>$enclosure_value);
IF you can't add custom fields, then you need to put the URL into the post content, then WordPress will automatically assume that URL in the post content is supposed to be a podcast episode enclosure URL. It may not contain the size and type information though.

machouinard

  • Newbie
  • Posts: 8
Re: Adding Podcasts Programmatically
« Reply #6 on: May 15, 2012, 11:01:28 am »
Thank you!  Precisely what I was looking for.  Had tried adding enclosure to the $content array, so I guess I was on the right track. 

This is a terrific plugin and I'm grateful for the help.

Mark

angelo

  • CIO, RawVoice
  • Administrator
  • Hero Member
  • *****
  • Posts: 4483
Re: Adding Podcasts Programmatically
« Reply #7 on: May 16, 2012, 09:56:08 am »
The secret sauce is how WordPress expects enclosure data to be saved. Someone decided that it should be saved in a multi-line format, with line 1 being the URL, line 2 being the file size in bytes, and line three being the content type. PowerPress and the TGS Podcasting plugin use the 4th line to save additional episode data in the form of a serialized PHP array. So if you wanted to get really crafty you could add a 4th line with information like "duration". Check out the PowerPress source for a full list.

machouinard

  • Newbie
  • Posts: 8
Re: Adding Podcasts Programmatically
« Reply #8 on: June 01, 2012, 01:10:40 am »
First off, thanks again for the help!

We had an issue with the server and lost all the media a few days ago.  I'm attempting to repost all the podcasts, but am experiencing some difficulties.  The post shows up with the media player but nothing will play.  I noticed the Play in new window and Download links are pointing to the wrong podcasts.  Podcasts I haven't re-uploaded yet.  I suspect I may to blame for this.  After the podcasts were lost I figured I'd delete all the posts and start from scratch after they were all uploaded again.  Instead of deleting each post I truncated the wp-posts table.  I suspect there is some leftover information about the original posts that is getting in the way.

Is the podcast url and other info stored anywhere other than the post itself?  I didn't see anything in the database tables.  I deleted and reinstalled the PowerPress plugin hoping that would help but nothing changed.  I guess I can't figure out why it's attaching old URL's to the freshly uploaded podcasts.  All the info is correct before being sent to the XMLRPC function.  Now I'm babbling.

I hope you've got a simple solution.  Who am I kidding, I'll take any solution!

Thanks,
Mark

EDIT: For what it's worth, I did get one podcast to play after re-posting.  It was one that had been previously posted so that may negate my theory about the old podcast info. Didn't want to leave out any seemingly trivial facts.
« Last Edit: June 01, 2012, 01:46:55 am by machouinard »

machouinard

  • Newbie
  • Posts: 8
Re: Adding Podcasts Programmatically
« Reply #9 on: June 01, 2012, 01:27:51 am »
Quote
EDIT: Okay, that was code from an old attempt. This is the code I'm currently working with.  The posts themselves show up fine each time.  Occasionally the custom enclosure field will also show up.  I'm sure last time all I did was add the custom_fields array to the $content array using enclosure for the key and $enclosure_value as the value.  The only thing missing from the full code is where I set up the vars.  For testing purposes I'm only getting info for podcasts #15 and 150-159.  Any suggestions about what I'm doing wrong?
Quote
EDIT 2:  To reduce the amount of variables in the situation I set up a fresh blog with a fresh database.  The enclosure custom field and the media player (pointing to the old mp3 location) are no longer showing up randomly.  I suspect that had to do with the way I removed the old posts by truncating the wp-posts table, but that's just a theory at this point.

Code: [Select]
try{
$DBH = new PDO("mysql:host=$host;dbname=$db_name", $user, $pass);
}
 catch (PDOException $e){
     echo $e->getMessage();
}

$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 
$STH = $DBH->query("SELECT * FROM podcasts WHERE number LIKE '15%'");
$STH->setFetchMode(PDO::FETCH_ASSOC);
 



function wpPostXMLRPC($enclosure_value, $title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8') {
    $title = htmlentities($title,ENT_NOQUOTES,$encoding);
    $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

    $content = array(
        'title'=>$title,
        'description'=>$body,
        'mt_allow_comments'=>1,
        'mt_allow_pings'=>0,
        'post_type'=>'post',
        'mt_keywords'=>$keywords,
        'categories'=>array($category),
        'custom_fields' => array(
            'enclosure' => $enclosure_value, 
        ),
    );

    $params = array(0,$username,$password,$content,true);
    $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
    //$request = xmlrpc_encode_request('blogger.newPost',$params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpcurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}


while($row = $STH->fetch()){
    $title = $row['title'];
    $body = $row['name']."\n";
    $body .= $row['comments'];
    $rpcurl = 'http://OUR BLOG URL/xmlrpc.php';
    $category = 'Membership Meetings';

    $enclosure_value = $row['url'];
    $enclosure_value .= "\n";
    $enclosure_value .= $row['size'];
    $enclosure_value .= "\n";
    $enclosure_value .= $type;

    $result = wpPostXMLRPC($enclosure_value, $title, $body, $rpcurl, $username, $password, $category);
    echo $result.  "<br />\n";


« Last Edit: June 01, 2012, 11:11:27 pm by machouinard »

machouinard

  • Newbie
  • Posts: 8
Re: Adding Podcasts Programmatically
« Reply #10 on: June 02, 2012, 06:33:54 am »

Okay, I got it working.  Here's the latest code.  Even got this to set the 'Posted On' date correctly for each post.  I'm not entirely sure how it's different from the last way I had tried, but at this point I'm just happy it worked.  Only problem is, after I re-posted all the podcasts I realized I didn't include the images that were supposed to go along with them.  I have the URL to each image in the database too.  Guess I'll be starting over one more time.  At least if the code gets lost again I'll know where to find it!  I found this page in my search for answers and it helped quite a bit.

Thanks again for the terrific plugin!



Code: [Select]
try{
$DBH = new PDO("mysql:host=$host;dbname=$db_name", $user, $pass);
}
 catch (PDOException $e){
     echo $e->getMessage();
}

$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
 
$STH = $DBH->query("SELECT * FROM podcasts");
$STH->setFetchMode(PDO::FETCH_ASSOC);
 

function get_response($URL, $context) {
   
    $curlHandle = curl_init();

    curl_setopt($curlHandle, CURLOPT_URL, $URL);
    curl_setopt($curlHandle, CURLOPT_HEADER, false);
    curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);

    $response = curl_exec($curlHandle);
    return $response;
}
   

    while($row = $STH->fetch()){
        $pubdate = $row['date']."T17:51:00Z";
        $pubdate = str_replace('-', '', $pubdate);
       
        $content['date_created_gmt'] = '%pubdate%';
       
        $title = $row['title'];
        $body = $row['name']."\n";
        $body .= $row['comments']."\n";
       
        $enclosure_value = $row['url'];
        $enclosure_value .= "\n";
        $enclosure_value .= $row['size'];
        $enclosure_value .= "\n";
        $enclosure_value .= $type;

        $content['title'] = $title;
        $content['description'] = $body;
        $content['categories'] = array("Meetings");
        /*Pass custom fields*/
        $content['custom_fields'] = array(
            array( 'key' => 'enclosure', 'value' => $enclosure_value )
            );
        $toPublish = true;
        $request = xmlrpc_encode_request("metaWeblog.newPost",
        array(1,$USERNAME, $PASSWORD, $content, $toPublish));

        //TO SET THE 'POSTED ON' DATE
        $request = str_replace('<string>%pubdate%</string>',
                   '<dateTime.iso8601>' . $pubdate . '</dateTime.iso8601>',
                   $request);
       
        $xmlresponse = get_response($BLOGURL."/xmlrpc.php", $request);
        $response = xmlrpc_decode($xmlresponse);


            echo ":Post ID";
            echo "<pre>";
            print_r($response);
            echo "</pre>";
        echo "\n";
    }

angelo

  • CIO, RawVoice
  • Administrator
  • Hero Member
  • *****
  • Posts: 4483
Re: Adding Podcasts Programmatically
« Reply #11 on: June 02, 2012, 09:32:35 am »
Interesting code! That's one way to spend a Friday night! If you post this as a plugin on WordPress.org, let me know and we'll include a link in PowerPress's readme.txt, which will also appear in the PowerPress page on WordPress.org

machouinard

  • Newbie
  • Posts: 8
Re: Adding Podcasts Programmatically
« Reply #12 on: June 02, 2012, 10:15:54 pm »
Aaaaaah,  Looks like I was simply one dimension short on that enclosure array in the code two posts up.  Makes perfect sense now!

Going to start learning about plugins later today.  Many years ago I got the impression WordPress was just something opinionated loudmouth people used to spread their nonsense all over the web.  But it's so much more than that!  I see that now!    Heck, not long ago I was a die-hard Windows user.  Guess getting older has it's benefits.  I'll cut myself off here.  If/when I post this as a plugin, I'll let you know.

Thanks again,
Mark