Auto shared new video Dailymotion to twitter

The purpose of this post is to show how to link a dailymotion video rss feed to your twitter account using Codebird php, so when the script runs it adds one video post to your twitter account.. can then be run via a cron setup (won’t cover here as system dependent – windows v linux servers).

You need three things sorted out first..

(1) Down the codebird for php library from https://github.com/jublonet/codebird-php

(2) Obtain a twitter developer api key details from https://apps.twitter.com/app/new (best to log into your twitter account first)

(3) Have a Dailymotion rss feed – usually of format http://www.dailymotion.com/rss/user/[daily motion user name] – where [daily motion user name] is your user account name of account.

The code looks like the following.. things to replace are in [] brackets.

Note: I have taken out my code for shortening links and auto hash tagging using str_ireplace(), but the principles here should get you in the right direction.

<?php
require_once("codebird.php"); // reference the codebird php library file (downloaded in step one above)

class BlogPost{
    var $date;
    var $ts;
    var $link;
    var $title;
    var $text;
}

class BlogFeed{
    var $posts = array();

    function __construct($file_or_url)  {
        $file_or_url = $this->resolveFile($file_or_url);
        if (!($x = simplexml_load_file($file_or_url)))  {
           return;
        }

        foreach ($x->channel->item as $item) {

            $post = new BlogPost();
            $post->date  = (string) $item->pubDate;
            $post->ts    = strtotime($item->pubDate);
            $post->link  = (string) $item->link;
            $post->title = (string) $item->title;

            $post->text = ""; // not using this for tweets

            $this->posts[] = $post;
        }
    }

    private function resolveFile($file_or_url) {
        if (!preg_match('|^https?:|', $file_or_url))
            $feed_uri = $file_or_url;
        else
            $feed_uri = $file_or_url;

        return $feed_uri;
    }

    private function summarizeText($summary) {
        $summary = strip_tags($summary);

        // Truncate summary line to 100 characters
        $max_len = 100;
        if (strlen($summary) > $max_len)
            $summary = substr($summary, 0, $max_len) . '...';

        return $summary;
    }
}

// read in posts
$ps = new BlogFeed("http://www.dailymotion.com/rss/user/[daily motion user name]");
// your dailymotion rss feed (step three above)
$posts = $ps->posts;
$c=count($posts);
$r=rand(1,$c);
$r=$r-1;
$p=$posts[$r];
$link=$p->link;
$title=$p->title;
if($link!="") {

$message = $title." ".$link; // note that the twitter 140 character limit applies to the total of message sent
// now tweet - [] key information unique to twitter account - obtain keys via https://apps.twitter.com/app/new (step two above)

\Codebird\Codebird::setConsumerKey('[YOURKEY]', '[YOURSECRET]');
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("[YOURTOKEN]", "[YOURTOKENSECRET]");

$params = array(
    'status' => $message
);

$reply = $cb->statuses_update($params);

// display message to screen

echo($message);

}
?>

Note: disclaimers on code/snippet usage applies – no responsibility on usage.

Leave a Reply

Your email address will not be published. Required fields are marked *