blam r531 - in trunk: . lib src



Author: cmartin
Date: Sat Apr  5 14:59:46 2008
New Revision: 531
URL: http://svn.gnome.org/viewvc/blam?rev=531&view=rev

Log:
Check-in the RSSFeed library.

Added:
   trunk/lib/RSSFeed.cs
Modified:
   trunk/ChangeLog
   trunk/lib/Makefile.am
   trunk/src/FeedUpdater.cs

Modified: trunk/lib/Makefile.am
==============================================================================
--- trunk/lib/Makefile.am	(original)
+++ trunk/lib/Makefile.am	Sat Apr  5 14:59:46 2008
@@ -6,17 +6,26 @@
 
 af_ASSEMBLIES = /r:System.Xml
 af_SOURCES = AtomFeed.cs
-af_LIBDIR = $(extra_libdir)
-af_FLAGS = /target:library
+
+feed_LIBDIR = $(extra_libdir)
+feed_FLAGS = /target:library
+
+rf_SOURCES = RSSFeed.cs
 
 EXTRA_DIST = $(extra_lib_DATA) \
-	     $(af_SOURCES)
+	     $(af_SOURCES) \
+	     $(rf_SOURCES)
 
 all: AtomFeed.dll
 
 AtomFeed.dll: AtomFeed.cs
-	$(CSC) $(af_FLAGS) $(af_ASSEMBLIES) $(af_SOURCES) /out:$@
+	$(CSC) $(feed_FLAGS) $(af_ASSEMBLIES) $(af_SOURCES) /out:$@
+
+RSSFeed.dll: RSSFeed.cs
+	$(CSC) $(feed_FLAGS) $(rf_SOURCES) /out:$@
 
 install-data-local: AtomFeed.dll
-	$(mkinstalldirs) $(DESTDIR)$(af_LIBDIR)
-	$(INSTALL_DATA) AtomFeed.dll $(DESTDIR)$(af_LIBDIR)/AtomFeed.dll
\ No newline at end of file
+	$(mkinstalldirs) $(DESTDIR)$(feed_LIBDIR)
+	$(INSTALL_DATA) AtomFeed.dll $(DESTDIR)$(feed_LIBDIR)/AtomFeed.dll
+	$(INSTALL_DATA) RSSFeed.dll $(DESTDIR)$(feed_LIBDIR)/RSSFeed.dll
+

Added: trunk/lib/RSSFeed.cs
==============================================================================
--- (empty file)
+++ trunk/lib/RSSFeed.cs	Sat Apr  5 14:59:46 2008
@@ -0,0 +1,240 @@
+/*
+ * Copyright (c) 2008 Carlos MartÃn Nieto
+ * 
+ * This file is released under the terms of the GNU GPLv2.
+ */
+
+using System;
+using System.Text;
+using System.IO;
+using System.Collections;
+using System.Xml.Serialization;
+using System.Xml;
+using System.Xml.XPath;
+
+namespace RSS
+{
+    /*
+     * This class serves as a way to use both RSS10Feed and RSS20Feed
+     * in the same way so the program doesn't need to care.
+     */
+    public class RSSFeed {
+        
+        public string version = null;
+        public RSSGenChan[] Channel = null;
+        
+        public static RSSFeed LoadFromFile(string uri)
+        {
+            string type = feed_type(uri);
+            RSSFeed feed = new RSSFeed();
+            
+            if(type == "rss10"){
+                feed.PopulateFromRSS10(uri);
+            } else if(type == "rss20"){
+                feed.PopulateFromRSS20(uri);
+            } else {
+                throw new NotSupportedException("Feed type not supported");
+            }
+            
+            return feed;
+        }
+        
+        public void PopulateFromRSS10(string uri)
+        {
+            RSS10Feed feed = RSS10Feed.LoadFromXml(uri);
+
+            version = "1.0";
+            
+            /* FIXME: I think it's possible to have multiple channels. Try to
+             * figure it out and implement it.
+             */
+            Channel = new RSSGenChan[feed.Channel.Length]; // Always one.
+
+            Channel[0] = new RSSGenChan();
+            Channel[0].Title = feed.Channel[0].Title;
+            Channel[0].Description = feed.Channel[0].Description;
+            Channel[0].Link = feed.Channel[0].Link;
+            
+            Channel[0].Item = new RSSGenItem[feed.Item.Length];
+            
+            for(int i = 0; i < feed.Item.Length; ++i){
+                Channel[0].Item[i] = new RSSGenItem();
+                Channel[0].Item[i].Title = feed.Item[i].Title;
+                Channel[0].Item[i].Link = feed.Item[i].Link;
+                Channel[0].Item[i].Content = feed.Item[i].ContEnc;
+                Channel[0].Item[i].Date = feed.Item[i].Date;
+                Channel[0].Item[i].Author = feed.Item[i].Creator;
+                Channel[0].Item[i].Guid = feed.Item[i].Link;
+            }
+        }
+        
+        public void PopulateFromRSS20(string uri)
+        {
+            RSS20Feed feed = RSS20Feed.LoadFromXml(uri);
+
+            version = "2.0";
+            
+            Channel = new RSSGenChan[feed.Channel.Length];
+
+            for(int i = 0; i < feed.Channel.Length; ++i){
+                Channel[i] = new RSSGenChan();
+                Channel[i].Title = feed.Channel[i].Title;
+                Channel[i].Link = feed.Channel[i].Link;
+                Channel[i].Language = feed.Channel[i].Language;
+                Channel[i].Description = feed.Channel[i].Description;
+                
+                Channel[i].Item = new RSSGenItem[feed.Channel[i].Item.Length];
+                
+                for(int j= 0; j < feed.Channel[i].Item.Length; ++j){
+                    Channel[i].Item[j] = new RSSGenItem();
+                    Channel[i].Item[j].Title = feed.Channel[i].Item[j].Title;
+                    Channel[i].Item[j].Guid = feed.Channel[i].Item[j].Guid;
+                    Channel[i].Item[j].Content = feed.Channel[i].Item[j].Description;
+                    Channel[i].Item[j].Date = DateTime.Parse(feed.Channel[i].Item[j].PubDate);
+                }
+            }
+        }
+        
+        private static string feed_type(string uri)
+        {
+            XPathDocument doc = new XPathDocument(uri);
+            XPathNavigator nav = doc.CreateNavigator();
+            XmlNamespaceManager nsm = new XmlNamespaceManager(nav.NameTable);
+            XPathExpression expr = nav.Compile("/rss10:RDF|/rss20:rss");
+            
+            nsm.AddNamespace("rss10", "http://www.w3.org/1999/02/22-rdf-syntax-ns#";);
+            nsm.AddNamespace("rss20", "");
+            
+            expr.SetContext(nsm);
+            
+            XPathNodeIterator iter = nav.Select(expr);
+            iter.MoveNext();
+        
+            string str = "(none)";
+            
+            if(iter.Current != null){
+                switch(iter.Current.NamespaceURI){
+                case "http://www.w3.org/1999/02/22-rdf-syntax-ns#":
+                    str = "rss10";
+                    break;
+                case "":
+                    str = "rss20";
+                    break;
+                default:
+                    str = "(unknown)";
+                    break;
+                }
+            }
+            
+            return str;
+        }
+    }
+    
+    public class RSSGenChan {
+        public string Title = null;
+        public string Link = null;
+        public string Language = null;
+        public string Description = null;
+        public string Generator = null;
+        public RSSGenItem[] Item;
+    }
+    
+    public class RSSGenItem {
+        public string Title = null;
+        public string Author = null;
+        public DateTime Date;
+        public string Content = null;
+        public string Guid = null;
+        public string Link = null;
+    }
+    
+    [XmlRoot("RDF")]
+    public class RSS10Feed {
+        [XmlElement("channel", Namespace="http://purl.org/rss/1.0/";)] public RSSChannel[] Channel = null;
+        [XmlElement("item", Namespace="http://purl.org/rss/1.0/"; )] public RSSItem[] Item = null;
+        
+    static XmlSerializer ser = new XmlSerializer(typeof(RSS10Feed),
+                              "http://www.w3.org/1999/02/22-rdf-syntax-ns#";);
+//	"http://purl.org/rss/1.0";);
+
+
+	public static RSS10Feed LoadFromXml(string uri)
+	{
+		try {
+		FileStream fs = new FileStream(uri, FileMode.Open);
+		return (RSS10Feed)ser.Deserialize(fs);
+		}
+		catch(FileNotFoundException){
+			Console.WriteLine("Can't open the flie");
+			return null;
+		}
+
+	}
+
+	public static RSS10Feed Load(StringReader sr)
+	{
+		return (RSS10Feed)ser.Deserialize(sr);
+	}
+    }
+    
+    [XmlType("rss")]
+    public class RSS20Feed {
+	[XmlAttribute("version")] public float version;
+	[XmlElement("channel")] public RSSChannel[] Channel = null;
+	
+	static XmlSerializer ser = new XmlSerializer(typeof(RSS20Feed));//,
+	//"http://purl.org/dc/elements/1.1/";);
+
+	public static RSS20Feed LoadFromXml(string uri)
+	{
+		try {
+		FileStream fs = new FileStream(uri, FileMode.Open);
+		return (RSS20Feed)ser.Deserialize(fs);
+		}
+		catch(FileNotFoundException){
+			Console.WriteLine("Can't open the flie");
+			return null;
+		}
+
+	}
+
+	public static RSS20Feed Load(StringReader sr)
+	{
+		return (RSS20Feed)ser.Deserialize(sr);
+	}
+    }
+
+    [XmlType("channel")]
+    public class RSSChannel {
+        [XmlElement("title")] public string Title = null;
+        [XmlElement("link")] public string Link = null;
+        [XmlElement("language")] public string Language = null;
+        [XmlElement("description")] public string Description = null;
+        [XmlElement("lastBuildDate")] public DateTime LastBuildDate = DateTime.MinValue;
+        [XmlElement("generator")] public string Generator = null;
+        [XmlElement("item")] public RSSItem[] Item = null;
+    }
+
+    [XmlType("item")]
+    public class RSSItem {
+        [XmlElement("title")] public string Title = null;
+        [XmlElement("guid")] public string Guid = null;
+        [XmlElement("link")] public string Link = null;
+        [XmlElement("description")] public string Description = null;
+        [XmlElement("encoded", Namespace="http://purl.org/rss/1.0/modules/content/";)] public string ContEnc = null;
+        //[XmlElement("pubDate")] public DateTime PubDate = DateTime.MinValue;
+        [XmlElement("pubDate")] public string PubDate = null;
+        [XmlElement("creator", Namespace="http://purl.org/dc/elements/1.1/";)] public string Creator = null;
+        [XmlElement("date", Namespace="http://purl.org/dc/elements/1.1/";)] public DateTime Date;
+    }
+
+    [XmlType("image")] /* Probably a LiveJournal thing. */
+    public class RSSImage {
+        [XmlElement("url")] public string Url = null;
+        [XmlElement("title")] public string Title = null;
+        [XmlElement("link")] public string Link = null;
+        [XmlElement("width")] public int Width;
+        [XmlElement("height")] public int Height;
+    }
+}
+

Modified: trunk/src/FeedUpdater.cs
==============================================================================
--- trunk/src/FeedUpdater.cs	(original)
+++ trunk/src/FeedUpdater.cs	Sat Apr  5 14:59:46 2008
@@ -105,7 +105,6 @@
         }
 
         if(channel.Type == ""){
-
             string type = feed_type(ref feed);
             
             /* If we know for certain, assign it directly. */



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