RssOWL Backend feedback needed



Hi hackers, Kevin kubasik and me are working on a Backend/Filter for RssOwl
Now we have this try of backend but we think that feedback of people with
more experience doing this type of hacks would be nice.

So, code corrections, insults and all type of feedback is welcome


Greets.

--
David Uvalle
//
// System.Comment.cs: Handles comments in System files.
// RssOwel.cs: Backend form RssOWL (http://www.rssowl.org)
//
// Author:
//   David Uvalle (davor.trex at gmail.com)
//

using System;
using System.IO;
using System.Xml;
using System.Collections;


namespace Beagle.Util {

	// Rss Items and Atom Entries class 

	// FIXME: add more item elements
	public class Item {
		public string ITitle;
		public string IDescription;
		public string ILink;
	}

	// Feed generic class
	public class Feed {
		public string URI;
		public string Type;
		public string Title;
		public Item [] Items;
	}

	public class RssOwl {

		private ArrayList files;
		private string location;
		private int counter = 0;
		private int size = 0;
		public Feed [] feeds;
		

		public RssOwl () 
		{
			string home = Environment.GetEnvironmentVariable ("HOME");
			location = Path.Combine (home, ".rssowl/cache");


			if (!Directory.Exists (location))
				return;

			string [] cache = Directory.GetFiles (location);
			
			if (cache == null)
				return;
	
			files = new ArrayList ();

			foreach (string file in cache) {
				if (!file.EndsWith (".xml"))
					continue;
				
				files.Add (file);
				size++;
			}
			
			feeds = new Feed [size];
			XmlConverter (files);
				
		}

		public void XmlConverter (ArrayList feeds)
		{
			try {
				XmlDocument doc;
				foreach (string feed in feeds) {
					doc = new XmlDocument ();
					doc.Load (feed);
					IdentifyFeed (doc);
				}		
			}
			catch (Exception ex) {
				Console.WriteLine("{0}", ex);
			}
		}

		public void IdentifyFeed (XmlDocument doc)
		{
			feeds [counter] = new Feed ();
			XmlElement root = doc.DocumentElement;
			
			if (root.Name.Equals ("rss"))
				feeds [counter].Type = "RSSV2";
			else if (root.Name.Equals ("rdf:RDF"))
				feeds [counter].Type = "RSSV1";
			else if (root.Name.Equals ("feed") )
				feeds [counter].Type = "ATOM";


			feeds [counter].URI = doc.BaseURI;
			feeds [counter].Title = GetFeedTitle (feeds [counter], doc);

			int ItemSize = GetItemSize (feeds [counter], doc);
			feeds [counter].Items = new Item [ItemSize];

			for (int i=0; i< ItemSize; i++)
				feeds [counter].Items [i] = GetItemContent (feeds [counter], doc, i);
						
			counter++;
			
		}

		public Item GetItemContent (Feed feed, XmlDocument doc, int ItemCount)
		{
			Item item = new Item ();

			XmlNode node;
			string number = Convert.ToString (ItemCount+1);
			
			if ( feed.Type.Equals ("RSSV2")) {
				node = doc.SelectSingleNode ("/rss/channel/item["+number+"]/title");
				item.ITitle = node.InnerText;
				node = doc.SelectSingleNode ("/rss/channel/item["+number+"]/description");
				item.IDescription = node.InnerText;
				node = doc.SelectSingleNode ("/rss/channel/item["+number+"]/link");
				item.ILink = node.InnerText;
			}
			else if ( feed.Type.Equals ("RSSV1")) {
				node = doc.SelectSingleNode ("/*/*["+number+"]/*[1]");
				item.ITitle = node.InnerText;
				node = doc.SelectSingleNode ("/*/*["+number+"]/*[3]");
				item.IDescription = node.InnerText;
				node = doc.SelectSingleNode("/*/*["+number+"]/*[2]");
				item.ILink = node.InnerText;
			}
			else if ( feed.Type.Equals("ATOM")) {
				// FIXME: Add atom link attribute to Item.ILink
				item.ILink = " ";

				int temp = Convert.ToInt32(number);
				temp += 7;
				number = Convert.ToString(temp);

				node = doc.SelectSingleNode ("/*/*["+number+"]/*[1]");
				item.ITitle = node.InnerText;
				node = doc.SelectSingleNode ("/*/*["+number+"]/*[7]");
				item.IDescription = node.InnerText;
				
			}

			return item;
		}

		public string GetFeedTitle (Feed feed, XmlDocument doc)
		{
			XmlNodeList list;
			XmlNode rssTitle;
			string title = "";
			
			if ( feed.Type.Equals ("ATOM") || feed.Type.Equals ("RSSV1") ) {
				list = doc.SelectNodes ("//*");
				foreach(XmlNode node in list ) {
					if (node.Name.Equals ("title"))
					{
						title = node.InnerText;
						break;
					}
				}
			}
			else if ( feed.Type.Equals ("RSSV2")) {
				rssTitle = doc.SelectSingleNode ("/rss/channel/title");
				title = rssTitle.InnerText;
			}

			return title;
		}

		public int GetItemSize (Feed feed, XmlDocument doc)
		{
			XmlNodeList list;
			int temp = 0;
			if ( feed.Type.Equals ("RSSV2")) {
				list = doc.SelectNodes ("//item");
				temp = list.Count;
			}
			else if ( feed.Type.Equals ("RSSV1")) {
				list = doc.SelectNodes ("//*");
				foreach (XmlNode node in list) {
					if (node.Name.Equals ("item"))
						temp++;
				}
			}
			else if ( feed.Type.Equals ("ATOM"))
			{
				list = doc.SelectNodes ("//*");
				foreach (XmlNode node in list) {
					if (node.Name.Equals ("entry"))
						temp++;	
				}
			}	

			return temp;
			
		}

		public Feed[] GetFeeds ()
		{
			return feeds;
		}



	}

}

using System;

using Beagle.Util;

public class Test {

	public static void Main (string[] args)
	{
		RssOwl rssowl = new RssOwl ();
		Feed [] feeds = rssowl.GetFeeds ();
		
		
		foreach (Feed feed in feeds) {
			Console.WriteLine("type: {0} Uri: {1} Title: {2} ", feed.Type, feed.URI, feed.Title );
			Console.WriteLine("\n\n");
			foreach (Item item in feed.Items) {
				if ( item.ITitle !=null && item.IDescription !=null && item.ILink != null ) 
					Console.WriteLine ("Title: {0}\n\nDes: {1}\n\n Link: {2}\n\n\n", 	item.ITitle, 
											     		item.IDescription,
													item.ILink );
				
			}
			
		}
		
		
		
		
		
		
		

		

	}


}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]