RSS-Reader
Der RSS-Reader liest RSS-Feeds ein und bearbeitet sie auf. Er ist allerdings nicht konform zu bestehenden Standards und ist eher als Entwicklungsansatz gedacht.
Er besteht aus zwei Skripten. Zunächst einmal der RSS-Reader selbst, der einen Webstream einliest:
PHP:
<?php
/*
* RSS-Reader Class
*
*
* Takes an XML-Stream (preferably RSS 2.0) at constructor.
* Provides results with function GetItems via a two-dimensional array.
*
* (c) 2004 by Kai Boenke [ kai.boenke@gmx.de ]
*/
class RSSreader{
// Global variables
var $fURL;
// Constructor
// takes feed-url as parameter
function RSSreader($feedURL){
global $fURL;
$fURL = $feedURL;
}
// Item-interface
// returns items in an array
function GetItems(){
global $fURL;
$fReader = @fopen($fURL, "r");
if(!$fReader){
@fclose($fReader);
return(false);
}
$fCount = 0;
while(!feof($fReader)){
$fLine = trim(fgets($fReader, 4096));
if(preg_match("/<item[^>]*>/i", $fLine)){
$fData[$fCount] = $this->ReadItem($fReader);
$fCount++;
}
}
fclose($fReader);
return($fData);
}
// Item-Reader
// reads feed item-wise
// Internal use only!
function ReadItem($fReader){
$fItem = array();
while($fLine = trim(fgets($fReader, 4096))){
if($fLine == "</item>"){
break;
}
$fCommand = substr($fLine, strpos($fLine, "<")+1, strpos($fLine, ">", 1)-1);
$fContent = substr($fLine, strpos($fLine, ">", 1)+1, strpos($fLine, "<//")-strlen($fCommand)-3);
$fItem[$fCommand] = $fContent;
}
return($fItem);
}
}
?>
/*
* RSS-Reader Class
*
*
* Takes an XML-Stream (preferably RSS 2.0) at constructor.
* Provides results with function GetItems via a two-dimensional array.
*
* (c) 2004 by Kai Boenke [ kai.boenke@gmx.de ]
*/
class RSSreader{
// Global variables
var $fURL;
// Constructor
// takes feed-url as parameter
function RSSreader($feedURL){
global $fURL;
$fURL = $feedURL;
}
// Item-interface
// returns items in an array
function GetItems(){
global $fURL;
$fReader = @fopen($fURL, "r");
if(!$fReader){
@fclose($fReader);
return(false);
}
$fCount = 0;
while(!feof($fReader)){
$fLine = trim(fgets($fReader, 4096));
if(preg_match("/<item[^>]*>/i", $fLine)){
$fData[$fCount] = $this->ReadItem($fReader);
$fCount++;
}
}
fclose($fReader);
return($fData);
}
// Item-Reader
// reads feed item-wise
// Internal use only!
function ReadItem($fReader){
$fItem = array();
while($fLine = trim(fgets($fReader, 4096))){
if($fLine == "</item>"){
break;
}
$fCommand = substr($fLine, strpos($fLine, "<")+1, strpos($fLine, ">", 1)-1);
$fContent = substr($fLine, strpos($fLine, ">", 1)+1, strpos($fLine, "<//")-strlen($fCommand)-3);
$fItem[$fCommand] = $fContent;
}
return($fItem);
}
}
?>
Anschließend werden die Daten vom Stream-Reader aufgearbeitet:
PHP:
<?php
/*
* Some RSS-Reading classes
*
* These classes use RSSreader to open specific streams and
* provide their contents to other scripts.
*
* (c) 2004 by Kai Boenke
*/
// Get RSS-Reader
require("class.RSSreader.php");
/*
* Abstract stream-reader class
*/
class AbstractStreamReader{
// Declare class-global parameters
var $fName; // The streams name
var $fURL; // The streams URL
var $fData; // The streams content
var $pEnabled; // Wether or not this stream is mobile-enhanced
var $pIdent; // The Streams link-identifier
var $pSearch; // Search-pattern for parsing mobile-links
var $pReplace; // Replace-pattern for parsing mobile-links
var $sReader; // The actual stream-reader
// Constructor that initializes global variables
// Takes URL and Name of Stream as parameters
function Init($URL, $Name){
$this->fURL = $URL;
$this->fName = $Name;
$this->sReader = new RSSreader($this->fURL);
}
// Return the name of this stream
function GetName(){
return($this->fName);
}
// Read Data-Items from stream
// Returns false if stream could not be opened
function ReadStream(){
return ($this->fData = $this->sReader->GetItems());
}
// Return data-items
function GetData(){
if(!$this->ReadStream()){
return(false);
}
if(function_exists('this->Parse()')){
$this->Parse();
}
return($this->fData);
}
// Return stream with mobile-links enabled
function GetDataForMobile(){
if(!$this->ReadStream()){
return(false);
}
if(function_exists('this->Parse()')){
$this->Parse();
}
if($this->pEnabled){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i][$this->pIdent] =
preg_replace($this->pSearch, $this->pReplace, $this->fData[$i][$this->pIdent]);
}
return($this->fData);
}else{
return($this->GetData());
}
}
// Return specified number of items
function GetDataByNum($num){
if(!$this->GetData()){
return(false);
}
for($i = 0; $i < $num && $i < count($this->fData); $i++){
$tData[$i] = $this->fData[$i];
}
return($tData);
}
// Return specified number of mobile-link enabled items
function GetDataForMobileByNum($num){
if(!$this->GetDataForMobile()){
return(false);
}
for($i = 0; $i < $num && $i < count($this->fData); $i++){
$tData[$i] = $this->fData[$i];
}
return($tData);
}
}
/*
* Tagesschau-Reader
*/
class TagesschauReader extends AbstractStreamReader{
function TagesschauReader(){
$this->Init("http://www.tagesschau.de/xml/tagesschau-meldungen/", "Tagesschau");
$this->pEnabled = true;
$this->pIdent = "link";
$this->pSearch = "/_REF(\d+),(\d+)\.html/";
$this->pReplace = "_TYP4,\$2.html";
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* SPIEGEL-Reader
*/
class SpiegelReader extends AbstractStreamReader{
function SpiegelReader(){
$this->Init("http://www.spiegel.de/schlagzeilen/rss/0,5291,,00.xml", "SPIEGEL Online");
$this->pEnabled = true;
$this->pIdent = "link";
$this->pSearch = "/(\d+),(\d+)\.html/";
$this->pReplace = "druck-\$1,\$2.html";
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* Golem.de-Reader
*/
class GolemReader extends AbstractStreamReader{
function GolemReader(){
$this->Init("http://www.golem.de/rss.php?feed=RSS2.0", "Golem.de");
$this->pEnabled = true;
$this->pIdent = "link";
$this->pSearch = "/\/\d+\/(\d+)\.html/";
$this->pReplace = "/print.php?a=\$1";
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* PocketPC Thoughts-Reader
*/
class PPCTReader extends AbstractStreamReader{
function PPCTReader(){
$this->Init("http://www.pocketpcthoughts.com/xml/", "PocketPC Thoughts");
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* Slashdot-Reader
*/
class SlashdotReader extends AbstractStreamReader{
function SlashdotReader(){
$this->Init("http://slashdot.org/index.rss", "Slashdot");
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* LPde-Reader
*/
class LPdeReader extends AbstractStreamReader{
function LPdeReader(){
$this->Init("http://www.lanparty.de/news.xml", "LANparty.de");
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* Telepolis-Reader
*/
class TelepolisReader extends AbstractStreamReader{
function TelepolisReader(){
$this->Init("http://www.heise.de/tp/news.rdf", "Telepolis");
$this->pEnabled = true;
$this->pIdent = "link";
$this->pSearch = "/\/tp\/[a-zA-Z\/]+\/(\d+)\/\d+\.html/";
$this->pReplace = "/bin/tp/issue/dl-print.cgi?artikelnr=\$1";
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
?>
/*
* Some RSS-Reading classes
*
* These classes use RSSreader to open specific streams and
* provide their contents to other scripts.
*
* (c) 2004 by Kai Boenke
*/
// Get RSS-Reader
require("class.RSSreader.php");
/*
* Abstract stream-reader class
*/
class AbstractStreamReader{
// Declare class-global parameters
var $fName; // The streams name
var $fURL; // The streams URL
var $fData; // The streams content
var $pEnabled; // Wether or not this stream is mobile-enhanced
var $pIdent; // The Streams link-identifier
var $pSearch; // Search-pattern for parsing mobile-links
var $pReplace; // Replace-pattern for parsing mobile-links
var $sReader; // The actual stream-reader
// Constructor that initializes global variables
// Takes URL and Name of Stream as parameters
function Init($URL, $Name){
$this->fURL = $URL;
$this->fName = $Name;
$this->sReader = new RSSreader($this->fURL);
}
// Return the name of this stream
function GetName(){
return($this->fName);
}
// Read Data-Items from stream
// Returns false if stream could not be opened
function ReadStream(){
return ($this->fData = $this->sReader->GetItems());
}
// Return data-items
function GetData(){
if(!$this->ReadStream()){
return(false);
}
if(function_exists('this->Parse()')){
$this->Parse();
}
return($this->fData);
}
// Return stream with mobile-links enabled
function GetDataForMobile(){
if(!$this->ReadStream()){
return(false);
}
if(function_exists('this->Parse()')){
$this->Parse();
}
if($this->pEnabled){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i][$this->pIdent] =
preg_replace($this->pSearch, $this->pReplace, $this->fData[$i][$this->pIdent]);
}
return($this->fData);
}else{
return($this->GetData());
}
}
// Return specified number of items
function GetDataByNum($num){
if(!$this->GetData()){
return(false);
}
for($i = 0; $i < $num && $i < count($this->fData); $i++){
$tData[$i] = $this->fData[$i];
}
return($tData);
}
// Return specified number of mobile-link enabled items
function GetDataForMobileByNum($num){
if(!$this->GetDataForMobile()){
return(false);
}
for($i = 0; $i < $num && $i < count($this->fData); $i++){
$tData[$i] = $this->fData[$i];
}
return($tData);
}
}
/*
* Tagesschau-Reader
*/
class TagesschauReader extends AbstractStreamReader{
function TagesschauReader(){
$this->Init("http://www.tagesschau.de/xml/tagesschau-meldungen/", "Tagesschau");
$this->pEnabled = true;
$this->pIdent = "link";
$this->pSearch = "/_REF(\d+),(\d+)\.html/";
$this->pReplace = "_TYP4,\$2.html";
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* SPIEGEL-Reader
*/
class SpiegelReader extends AbstractStreamReader{
function SpiegelReader(){
$this->Init("http://www.spiegel.de/schlagzeilen/rss/0,5291,,00.xml", "SPIEGEL Online");
$this->pEnabled = true;
$this->pIdent = "link";
$this->pSearch = "/(\d+),(\d+)\.html/";
$this->pReplace = "druck-\$1,\$2.html";
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* Golem.de-Reader
*/
class GolemReader extends AbstractStreamReader{
function GolemReader(){
$this->Init("http://www.golem.de/rss.php?feed=RSS2.0", "Golem.de");
$this->pEnabled = true;
$this->pIdent = "link";
$this->pSearch = "/\/\d+\/(\d+)\.html/";
$this->pReplace = "/print.php?a=\$1";
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* PocketPC Thoughts-Reader
*/
class PPCTReader extends AbstractStreamReader{
function PPCTReader(){
$this->Init("http://www.pocketpcthoughts.com/xml/", "PocketPC Thoughts");
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* Slashdot-Reader
*/
class SlashdotReader extends AbstractStreamReader{
function SlashdotReader(){
$this->Init("http://slashdot.org/index.rss", "Slashdot");
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* LPde-Reader
*/
class LPdeReader extends AbstractStreamReader{
function LPdeReader(){
$this->Init("http://www.lanparty.de/news.xml", "LANparty.de");
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
/*
* Telepolis-Reader
*/
class TelepolisReader extends AbstractStreamReader{
function TelepolisReader(){
$this->Init("http://www.heise.de/tp/news.rdf", "Telepolis");
$this->pEnabled = true;
$this->pIdent = "link";
$this->pSearch = "/\/tp\/[a-zA-Z\/]+\/(\d+)\/\d+\.html/";
$this->pReplace = "/bin/tp/issue/dl-print.cgi?artikelnr=\$1";
}
// Parsing-function for title-entities
function Parse(){
for($i = 0; $i < count($this->fData); $i++){
$this->fData[$i]['title'] = htmlentities($this->fData[$i]['title']);
}
}
}
?>
Kommentieren