Question

I would like to make a script that gets lines from txt file and makes new rss feed item for every line. Txt file will be frequently updated with new lines of data, so the script should check every hour if there is a new line, and if there is, needs to make new feed item.

I found this txt2rss script, but I'm not so proficient and there is little documentation on how to modify and use it. http://users.ninthfloor.org/~ashawley/txt2rss/txt2rss.php.html

Could someone point me in the right direction? PHP with cronjob? Or maybe there is a simplier way?

Appreciate it

P.S:. I also found this script.

public static function create($info, $items, $format = 'rss2', $encoding = 'UTF-8') 
{ 
    $info += array('title' => 'Generated Feed', 'link' => '', 'generator' => 'KohanaPHP'); 

    $feed = '<?xml version="1.0" encoding="'.$encoding.'"?><rss version="2.0"><channel></channel></rss>'; 
    $feed = simplexml_load_string($feed); 

    foreach ($info as $name => $value) 
    { 
        if ($name === 'image') 
        { 
            // Create an image element 
            $image = $feed->channel->addChild('image'); 

            if ( ! isset($value['link'], $value['url'], $value['title'])) 
            { 
                throw new Kohana_Exception('Feed images require a link, url, and title'); 
            } 

            if (strpos($value['link'], '://') === FALSE) 
            { 
                // Convert URIs to URLs 
                $value['link'] = URL::site($value['link'], 'http'); 
            } 

            if (strpos($value['url'], '://') === FALSE) 
            { 
                // Convert URIs to URLs 
                $value['url'] = URL::site($value['url'], 'http'); 
            } 

            // Create the image elements 
            $image->addChild('link', $value['link']); 
            $image->addChild('url', $value['url']); 
            $image->addChild('title', $value['title']); 
        } 
        else 
        { 
            if (($name === 'pubDate' OR $name === 'lastBuildDate') AND (is_int($value) OR ctype_digit($value))) 
            { 
                // Convert timestamps to RFC 822 formatted dates 
                $value = date('r', $value); 
            } 
            elseif (($name === 'link' OR $name === 'docs') AND strpos($value, '://') === FALSE) 
            { 
                // Convert URIs to URLs 
                $value = URL::site($value, 'http'); 
            } 

            // Add the info to the channel 
            $feed->channel->addChild($name, $value); 
        } 
    } 

    foreach ($items as $item) 
    { 
        // Add the item to the channel 
        $row = $feed->channel->addChild('item'); 

        foreach ($item as $name => $value) 
        { 
            if ($name === 'pubDate' AND (is_int($value) OR ctype_digit($value))) 
            { 
                // Convert timestamps to RFC 822 formatted dates 
                $value = date('r', $value); 
            } 
            elseif (($name === 'link' OR $name === 'guid') AND strpos($value, '://') === FALSE) 
            { 
                // Convert URIs to URLs 
                $value = URL::site($value, 'http'); 
            } 

            // Add the info to the row 
            $row->addChild($name, $value); 
        } 
    } 

    if (function_exists('dom_import_simplexml')) 
    { 
        // Convert the feed object to a DOM object 
        $feed = dom_import_simplexml($feed)->ownerDocument; 

        // DOM generates more readable XML 
        $feed->formatOutput = TRUE; 

        // Export the document as XML 
        $feed = $feed->saveXML(); 
    } 
    else 
    { 
        // Export the document as XML 
        $feed = $feed->asXML(); 
    } 

    return $feed; 
}
Was it helpful?

Solution

In your crontab file:

59 * * * * /path/to/php -f /path/to/my/text/to/rss/file.php

If you want to be notified every time it runs do the following:

59 * * * * /path/to/php -f /path/to/my/text/to/rss/file.php 2>&1 | mailx -s "Ran Text to RSS Job" youremail@whatever.com

This will email you when the job has completed with a subject "Ran Text to RSS Job," if there was an error during execution it will be the body of the email otherwise the message will be blank.

This will run at the 59th minute of every hour.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top