Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 33 total  RSS 

PHPアップローダー RSS機能

PHPアップローダー RSS機能

<?php
/**** PHPアップローダー RSS機能 ***
 * by ◆RTphpfqies
 ****/

  $title	= 'PHPアップローダー';
  $logfile	= "./log/upup.log";	//ログファイル名(変更する事)
  $updir 	= "./log/";	//アップ用ディレクトリ(変更する場合は、35.48.50行も変更)
  $prefix	= '';		//接頭語(up001.txt,up002.jpgならup)
  $page_def	= 20;		//RSSの表示行数


function FormatByte($size){//バイトのフォーマット(B→kB)
  if($size == 0)			$format = "";
  else if($size <= 1024)		$format = $size."B";
  else if($size <= (1024*1024))		$format = sprintf ("%dKB",($size/1024));
  else if($size <= (10*1024*1024))	$format = sprintf ("%.2fMB",($size/(1024*1024)));
  else					$format = $size."B";

  return $format;
}

error_reporting(0);
$base_dir = 'http://'.$_SERVER['HTTP_HOST'].str_replace('\\','/', dirname($_SERVER['SCRIPT_NAME'])); if(substr($base_dir,-1) != '/') $base_dir .= '/'; // URL
	header('Content-Type: text/xml;charset=shift-jis');
echo'<?xml version="1.0" encoding="shift-jis"?>
<rss version="2.0">
<channel>
<title>'.$title.'</title>
<link>'.$base_dir.'</link>
<description>'.$title.'</description>
<language>ja</language>
<generator>PHPアップローダー RSS機能</generator>';

$lines = file($logfile);
for($i = 0; $i < $page_def; $i++){
  if($lines[$i]=="") continue;
  list($id,$ext,$com,$host,$now,$size,$mtype,$pas,$orig,)=explode("\t",$lines[$i]);
  $fsize = FormatByte($size);

  $filename = $prefix.$id.".$ext";
  $target = $updir.$filename;

  echo '<item>
	<title>'.$filename.' - '.$orig.' ('.$fsize.')</title>
	<link>'.$base_dir.$target.'</link>
	<description>
	<![CDATA[
'.$com.'
	]]>
	</description>
	<pubDate>'.$now.'</pubDate>
</item>
';
  }
echo '</channel>
</rss>';

Simple Ruby Snarl RSS alerter

This simple Ruby program will send a Snarl alert when a new post is added to a feed. Assumes your Snarl command line tool is named "snarl.exe" and in your PATH.

require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'

def snarl(title, msg)
  # This part actually sends the notification.
  # Change "snarl" to your snarl command line tool if you have problems
  system "snarl /M \"#{title}\" \"#{msg}\""
end

source = ARGV[0]
content = ''
open(source) { |s| content = s.read }
rss = RSS::Parser.parse(content, false)
puts rss.channel.title
puts rss.channel.link
puts rss.channel.description

last_item = nil

while true
  puts "Checking for updates..."
  open(source) { |s| content = s.read }
  rss = RSS::Parser.parse(content, false)
  if last_item == rss.items[0]
    sleep 5*60*1000
  else
    last_item = rss.items[0]
    snarl last_item.title + " (#{rss.channel.title})", last_item.description.gsub(/<\/?[^>]*>/, "")
    sleep 10*60*1000
  end
end

RSS output of a Prologue post

// This is how Prologue outputs a post

<item>
		<title></title>
		<link>http://localhost/wordpress/?p=3</link>
		<comments>http://localhost/wordpress/?p=3#comments</comments>
		<pubDate>Tue, 29 Jan 2008 23:54:15 +0000</pubDate>

		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[welcome]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=3</guid>
		<description><![CDATA[Pretty cool, here&#8217;s the Twitta!]]></description>
			
                <content:encoded><![CDATA[<p>Pretty cool, here&#8217;s the Twitta!</p>]]></content:encoded>
			<wfw:commentRss>http://localhost/wordpress/?feed=rss2&p=3</wfw:commentRss>

</item>

Transforming XML into RSS

Using the previous code snippet which prepared an XML file it can now be transformed to RSS using the XSL below.


file: gang2rss.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

	<xsl:output method="xml" encoding="iso-8859-1" indent="yes"  />

	<xsl:template match="rss">

		<rss version="2.0">	
		<channel>
		<title>The Gang</title>
		<link>http://newsgang.net/audio/</link>
		<description>The Gang podcast</description>

  	<language>en</language>

	<xsl:apply-templates select="item" />

		</channel>
		</rss>

	</xsl:template>

	<xsl:template match="item">

	<item>
		<title><xsl:value-of select="title"/></title>
		<link>http://newsgang.net<xsl:value-of select="href"/></link>
		<description><xsl:value-of select="date"/></description>
		<enclosure>http://newsgang.net<xsl:value-of select="href_audio"/></enclosure>
	</item>

	</xsl:template>	

</xsl:stylesheet>


To transform the xsl file from the command-line you would type:

xsltproc gang2rss.xsl thegang_rss.xml

output
<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
  <channel>
    <title>The Gang</title>
    <link>http://newsgang.net/audio/</link>
    <description>The Gang podcast</description>
    <language>en</language>
    <item>
      <title>TheGangXII-II</title>
      <link>http://newsgang.net/gangitem/id=6501</link>
      <description>Jan 25</description>
      <enclosure>http://newsgang.net/gangitem/id=6501&amp;from=audio</enclosure>
    </item>
    <item>
      <title>TheGangXII-I</title>
      <link>http://newsgang.net/gangitem/id=6499</link>
      <description>Jan 25</description>
      <enclosure>http://newsgang.net/gangitem/id=6499&amp;from=audio</enclosure>
    </item>
    <item>
      <title>NewsGangLive01.24.08</title>
      <link>http://newsgang.net/gangitem/id=6445</link>
      <description>Jan 24</description>
      <enclosure>http://newsgang.net/gangitem/id=6445&amp;from=audio</enclosure>
    </item>
    <item>
      <title>NewsGangLiveII</title>
      <link>http://newsgang.net/gangitem/id=6377</link>
      <description>Jan 23</description>
      <enclosure>http://newsgang.net/gangitem/id=6377&amp;from=audio</enclosure>
    </item>
  </channel>
</rss>

Note: The enclosure url in this example does not reference the media file directly.

see also: http://en.wikipedia.org/wiki/RSS_(file_format)

Scrape an XHTML document using Ruby

A simple Ruby script to scrape an XHTML file with the selected content being saved to an xml file ready for transformation into an RSS feed. This example uses the XHTML file from http://newsgang.net/audio/ which is then saved locally as 'thegang.xml'.

#!/usr/bin/ruby
# file: thegang.rb

require 'rexml/document'
include REXML

class TheGang
  def initialize()
  end
  
  def rssify()
    file = File.new('thegang.xml','r')
    doc = Document.new(file)
    rss_doc = Document.new
    root = Element.new('rss')
    rss_doc.add_element(root)
    
    doc.root.elements.each("body/div/ul/li/h2/a") do |node|    
      o_rssitem = Element.new('item')
      o_li = node.parent.parent
      
      o_rsstitle = Element.new('title')
      o_rsstitle.text = node.text.gsub(/[\n,' ']/,'')
      o_rssitem.add_element(o_rsstitle)
      
      o_rsshref_audio = Element.new('href_audio')
      o_rsshref_audio.text = node.attributes.get_attribute('href').to_s.gsub('amp;&','')      
      o_rssitem.add_element(o_rsshref_audio)
      
      o_rsshref = Element.new('href')
      o_rsshref.text = o_rsshref_audio.text.gsub('&amp;from=audio','')      
      o_rssitem.add_element(o_rsshref)
      
      o_rssdate = Element.new('date')
      o_rssdate.text = "#{o_li.elements["p/span[1]"].text} #{o_li.elements["p/span[2]"].text}"
      o_rssitem.add_element(o_rssdate)
      rss_doc.root.add_element(o_rssitem)
      
    end

    file = File.new('thegang_rss.xml','w')
    file.puts rss_doc
    file.close
  end
end


if __FILE__ == $0
  gang = TheGang.new
  gang.rssify
end


see also: www.dapper.net

output (extract)
<rss>
  <item><title>TheGangXII-II</title><href_audio>/gangitem/id=6501&amp;from=audio</href_audio><href>/gangitem/id=6501</href><date>Jan 25</date></item>
  <item><title>TheGangXII-I</title><href_audio>/gangitem/id=6499&amp;from=audio</href_audio><href>/gangitem/id=6499</href><date>Jan 25</date></item>
  <item><title>NewsGangLive01.24.08</title><href_audio>/gangitem/id=6445&amp;from=audio</href_audio><href>/gangitem/id=6445</href><date>Jan 24</date></item>
  <item><title>NewsGangLiveII</title><href_audio>/gangitem/id=6377&amp;from=audio</href_audio><href>/gangitem/id=6377</href><date>Jan 23</date></item>
  ...
</rss>

Archive Flagged Items from NetNewsWire into Yojimbo

This lil Ruby-OSA script will allow you to import your "Flagged Items" in NetNewsWire as Web Archive Items in Yojimbo. Thus allowing you to save RSS articles for off-line viewing/storage.

#!/usr/local/bin/ruby
['rubygems', 'rbosa'].each {|lib| require lib}
nnw = OSA.app('NetNewsWire')
yojimbo = OSA.app('Yojimbo')

nnw.subscriptions.find { |s| s if s.display_name == 'Flagged Items' }.headlines.each do |article|
  unless yojimbo.web_archive_items.map { |f| f.source_url }.include?(article.url)
    archived = yojimbo.make(OSA::Yojimbo::WebArchiveItem, 
                            article.url, 
                            :name => article.title)
    # Adjust this for slower/faster bandwidth connections (or your feeling lucky, punk)
    sleep(5)
    # Uncomment below to remove the flagged items upon successfully archiving
    # if archived.name == 'untitled' && archived.source_url.empty?
    #   puts "!!! #{article.title} does not look to be imported !!!"
    # else
    #   article.delete
    # end
  end
end

A simple RSS Reader and Podcatcher

Written in Ruby this class reads an RSS feed and downloads the latest enclosure if it exists.

require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'
require 'open-uri'

class Rssreader
  def initialize(url)
    source = url # url or local file
    content = "" # raw content of rss feed will be loaded here
    open(source) do |s| content = s.read end
    @rss = RSS::Parser.parse(content, false)
  end

  # returns the first 3 titles from the rss feed
  def get_summary()
    buffer = '['
    for i in 0..2
      buffer += @rss.items[i.to_i].title + ' | '
    end      
    buffer.slice(0,buffer.length-3) + ']'
  end

  def enclosure?
    @rss.items.to_s.scan('<enclosure').length > 0
  end

  def get_enclosure_url
    enclosure = @rss.items[0].enclosure
    enclosure.url
  end

  def rwget(url, filename)
    file = File.new(filename, 'w')
    file.puts open(url, 'User-Agent' => 'Ruby-wget').read
  end

  def download_enclosure()
    if self.enclosure? then
      enclosure_url = self.get_enclosure_url()
      local_filename = File.basename(enclosure_url)
      #puts local_filename
      if not File.exist?(local_filename) then
        puts 'downloading enclosure ...'
        self.rwget(enclosure_url, local_filename)
        puts 'download completed'
      else
        puts 'enclosure downloaded already'
      end
    end
  end    
end

if __FILE__ == $0
  url = "http://mysite.com/gwd/feed/lugradio.rss"
  rss = Rssreader.new(url)
  puts rss.get_summary()
  rss.download_enclosure()
end

Twitter bot weatherlisbon

This little bash script shows how to use curl, grep, tail, sed and perl one-liners in order to compose a bleeding-edge twitter bot.
This one returns daily weather forecasts for Lisbon city based on the BBC weather forecast rss feed.

#! /bin/sh

#Goto here
here=/home/guillaume/Personal
cd $here

#BBC Lisbon weather id
id=0048

#BBC weather RSS feed address
feed="http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/${id}.xml"

#City
city=lisbon

#temporary file
file="weather${city}"

#Weather twitter bot
twitbot=weatherlisbon:*******

#Timestamp the log file
echo .>> $file.log
date >> $file.log

#Read the RSS feed and filter it
curl $feed | grep 'title' | tail -n 1 | perl -wlne'm/title>(.*)<\/title/i && print $1' | sed -e "s/&#xB0;//g" > $file.txt

#Read the forecast into a weather variable
read weather < $file.txt

#Twit the weather variable away
curl --basic --user $twitbot --data status="$weather" http://twitter.com/statuses/update.xml >> $file.log

RSS mod for phpBB

// This is a mod from phpBB that creates an RSS feed for your forum. I am hoping the folks at phpbb-seo.com can help me out with making this compatible with the 'advanced mod rewrite' mod.

<?php
/***************************************************************************
 *                                rss.php
 *                            -------------------
 *   begin                : Saturday, Feb 13, 2001
 *   copyright            : (C) 2005 by Lucas van Dijk
 *   email                : lucas@aoe3capitol.nl
 *
 *   $Id: rss.php,v 1.00 2004/07/11 16:46:15 mrlucky Exp $
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

define('IN_PHPBB', 1);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
include($phpbb_root_path.'includes/functions_post.php');
include($phpbb_root_path.'includes/bbcode.php');

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
//
// End session management
//

// -------
// Begin Page specific functions
//
function make_xml_compatible($text, $bbcode_uid = '', $use_bbcode = 0)
{
        global $board_config, $base_url;

        if($use_bbcode)
        {
                if($bbcode_uid != '')
                {
                        $text = ( $board_config['allow_bbcode'] ) ? bbencode_second_pass($text, $bbcode_uid) : preg_replace('/\:[0-9a-z\:]+\]/si', ']', $text);
                }
                else
                {
                        $text = preg_replace('/\:[0-9a-z\:]+\]/si', ']', $text);
                }
                $text = make_clickable($text);
                if($board_config['allow_smilies'])
                {
                        $text = smilies_pass($text);
                        $text = str_replace("./".$board_config['smilies_path'], $base_url.$board_config['smilies_path'], $text);
                }
        }
        $text = nl2br($text);

        $text = str_replace('&pound', '&amp;#163;', $text);
        $text = str_replace('&copy;', '(c)', $text);

        $text = htmlspecialchars($text);

        return $text;
}
//
// End page specific functions
// ------

//
// If running with PHP3
//
if(!is_array($_SERVER))
{
        $_SERVER = $HTTP_SERVER_VARS;
        $_GET = $HTTP_GET_VARS;
}

//
// Get Various vars
//

$base_url = ($board_config['cookie_secure'] ? "https://" : "http://").$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);

if(substr($base_url, -1) != "/")
{
        $base_url .= "/";
}

//

//
// Start RSS output
//
$rss_result = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
<rss version=\"2.0\">
<channel>
  <title>".make_xml_compatible($board_config['sitename'])."</title>
  <link>".$base_url."index.php</link>
  <description>".make_xml_compatible($board_config['site_desc'])."</description>
  <language>".$board_config['default_lang']."</language>
  <copyright>(c) Copyright 2007 by ".make_xml_compatible($board_config['sitename'])."</copyright>
  <managingEditor>".$board_config['board_email']."</managingEditor>
  <webMaster>".$board_config['board_email']."</webMaster>
  <pubDate>".create_date($board_config['default_dateformat'], time(), $board_config['board_timezone'])."</pubDate>
  <lastBuildDate>".create_date($board_config['default_dateformat'], time(), $board_config['board_timezone'])."</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>".make_xml_compatible($board_config['sitename'])."</title>
    <url>".$board_config['rss_image']."</url>
    <link>".$base_url."</link>
    <description>".make_xml_compatible($board_config['site_desc'])."</description>
  </image>
";

//
// Get latest topics
//


$forum=1;


        //
        // This SQL query selects the latest topics of a specific forum
        //
        $sql = "SELECT t.topic_title, t.topic_last_post_id, p.post_time, pt.post_text, pt.bbcode_uid, pt.post_link, u.username, u.user_id
                FROM ".TOPICS_TABLE." t, ".POSTS_TABLE." p, ".POSTS_TEXT_TABLE." pt, ".USERS_TABLE." u
                WHERE t.forum_id = ".$forum."
                AND t.topic_status != 1
                AND p.post_id = t.topic_last_post_id
                AND pt.post_id = p.post_id
                AND u.user_id = p.poster_id
                ORDER BY t.topic_last_post_id DESC
                LIMIT 0, ".intval($board_config['max_rss_topics']);
        if(!$result = $db->sql_query($sql))
        {
                message_die(GENERAL_ERROR, "Could not get Latest topics", '', __LINE__, __FILE__, $sql);
        }

        $is_auth = array();
        $is_auth = auth(AUTH_ALL, $forum, $userdata);
		
		

        while($row = $db->sql_fetchrow($result))
        {
		//Read link MOD
		$post_link = ( $row['post_link'] != '' ) ? $row['post_link'] : '';
		//end read link
		
                if($is_auth['auth_view'] && $is_auth['auth_read'])
                {
                        $description = "
                                       ".$lang['Posted']." ".create_date($board_config['default_dateformat'], $row['post_time'], $board_config['board_timezone'])."<br />
                                      <br />
                                      ".$row['post_text']
									  ."<br /><br />
									  <a href=\"$post_link\">Read</a>&nbsp;|&nbsp;<a href=\"".$base_url."viewtopic.php?".POST_POST_URL."=".$row['topic_last_post_id']."\" >Permalink</a>"
									  ."<hr />";

                        $rss_result .= "
                                      <item>
                                        <title>".make_xml_compatible(strip_tags($row['topic_title']))."</title>
                                        <link>".$base_url."viewtopic.php?".POST_POST_URL."=".$row['topic_last_post_id']."</link>
                                        <description>".make_xml_compatible($description, $row['bbcode_uid'], true)."</description>
                                        <pubDate>".create_date($board_config['default_dateformat'], $row['post_time'], $board_config['board_timezone'])."</pubDate>
                                        <guid isPermaLink=\"true\">".$base_url."viewtopic.php?".POST_POST_URL."=".$row['topic_last_post_id']."#".$row['topic_last_post_id']."</guid>
                                      </item>";
                }
        }


$rss_result .= "</channel></rss>";

header("Content-type: text/xml", true);
echo $rss_result;

?>
« Newer Snippets
Older Snippets »
Showing 1-10 of 33 total  RSS