blam r533 - in trunk: . lib src



Author: cmartin
Date: Sun Apr  6 08:57:08 2008
New Revision: 533
URL: http://svn.gnome.org/viewvc/blam?rev=533&view=rev

Log:
Adapt for use with RSSFeed

RSSFeed has been adapted to fit better with Blam.
Blam has been adapted to use RSSFeed.


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

Modified: trunk/lib/RSSFeed.cs
==============================================================================
--- trunk/lib/RSSFeed.cs	(original)
+++ trunk/lib/RSSFeed.cs	Sun Apr  6 08:57:08 2008
@@ -21,17 +21,17 @@
     public class RSSFeed {
         
         public string version = null;
-        public RSSGenChan[] Channel = null;
+        public RSSChannel[] Channel = null;
         
-        public static RSSFeed LoadFromFile(string uri)
+        public static RSSFeed Load(StringReader sr)
         {
-            string type = feed_type(uri);
+            string f_c = sr.ReadToEnd();
+            string type = feed_type(new StringReader(f_c));
             RSSFeed feed = new RSSFeed();
-            
             if(type == "rss10"){
-                feed.PopulateFromRSS10(uri);
+                feed.PopulateFromRSS10(new StringReader(f_c));
             } else if(type == "rss20"){
-                feed.PopulateFromRSS20(uri);
+                feed.PopulateFromRSS20(new StringReader(f_c));
             } else {
                 throw new NotSupportedException("Feed type not supported");
             }
@@ -39,28 +39,29 @@
             return feed;
         }
         
-        public void PopulateFromRSS10(string uri)
+        public void PopulateFromRSS10(StringReader sr)
         {
-            RSS10Feed feed = RSS10Feed.LoadFromXml(uri);
+            RSS10Feed feed = RSS10Feed.Load(sr);
 
             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 = new RSSChannel[feed.Channel.Length]; // Always one.
 
-            Channel[0] = new RSSGenChan();
+            Channel[0] = new RSSChannel();
             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];
+            Channel[0].Item = new RSSItem[feed.Item.Length];
             
             for(int i = 0; i < feed.Item.Length; ++i){
-                Channel[0].Item[i] = new RSSGenItem();
+                Channel[0].Item[i] = new RSSItem();
                 Channel[0].Item[i].Title = feed.Item[i].Title;
                 Channel[0].Item[i].Link = feed.Item[i].Link;
+                Channel[0].Item[i].Description = feed.Item[i].Description;
                 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;
@@ -68,36 +69,37 @@
             }
         }
         
-        public void PopulateFromRSS20(string uri)
+        public void PopulateFromRSS20(StringReader sr)
         {
-            RSS20Feed feed = RSS20Feed.LoadFromXml(uri);
+            RSS20Feed feed = RSS20Feed.Load(sr);
 
             version = "2.0";
             
-            Channel = new RSSGenChan[feed.Channel.Length];
+            Channel = new RSSChannel[feed.Channel.Length];
 
             for(int i = 0; i < feed.Channel.Length; ++i){
-                Channel[i] = new RSSGenChan();
+                Channel[i] = new RSSChannel();
                 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];
+                Channel[i].Item = new RSSItem[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] = new RSSItem();
                     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].Description = feed.Channel[i].Item[j].Description;
+                    Channel[i].Item[j].Content = feed.Channel[i].Item[j].ContEnc;
                     Channel[i].Item[j].Date = DateTime.Parse(feed.Channel[i].Item[j].PubDate);
                 }
             }
         }
         
-        private static string feed_type(string uri)
+        private static string feed_type(StringReader sr)
         {
-            XPathDocument doc = new XPathDocument(uri);
+            XPathDocument doc = new XPathDocument(sr);
             XPathNavigator nav = doc.CreateNavigator();
             XmlNamespaceManager nsm = new XmlNamespaceManager(nav.NameTable);
             XPathExpression expr = nav.Compile("/rss10:RDF|/rss20:rss");
@@ -130,19 +132,20 @@
         }
     }
     
-    public class RSSGenChan {
+    public class RSSChannel {
         public string Title = null;
         public string Link = null;
         public string Language = null;
         public string Description = null;
         public string Generator = null;
-        public RSSGenItem[] Item;
+        public RSSItem[] Item;
     }
     
-    public class RSSGenItem {
+    public class RSSItem {
         public string Title = null;
         public string Author = null;
-        public DateTime Date;
+        public DateTime Date = DateTime.MinValue;
+        public string Description = null;
         public string Content = null;
         public string Guid = null;
         public string Link = null;
@@ -150,8 +153,8 @@
     
     [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;
+        [XmlElement("channel", Namespace="http://purl.org/rss/1.0/";)] public RSSGenChannel[] Channel = null;
+        [XmlElement("item", Namespace="http://purl.org/rss/1.0/"; )] public RSSGenItem[] Item = null;
         
     static XmlSerializer ser = new XmlSerializer(typeof(RSS10Feed),
                               "http://www.w3.org/1999/02/22-rdf-syntax-ns#";);
@@ -180,7 +183,7 @@
     [XmlType("rss")]
     public class RSS20Feed {
 	[XmlAttribute("version")] public float version;
-	[XmlElement("channel")] public RSSChannel[] Channel = null;
+	[XmlElement("channel")] public RSSGenChannel[] Channel = null;
 	
 	static XmlSerializer ser = new XmlSerializer(typeof(RSS20Feed));//,
 	//"http://purl.org/dc/elements/1.1/";);
@@ -205,18 +208,18 @@
     }
 
     [XmlType("channel")]
-    public class RSSChannel {
+    public class RSSGenChannel {
         [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;
+        [XmlElement("item")] public RSSGenItem[] Item = null;
     }
 
     [XmlType("item")]
-    public class RSSItem {
+    public class RSSGenItem {
         [XmlElement("title")] public string Title = null;
         [XmlElement("guid")] public string Guid = null;
         [XmlElement("link")] public string Link = null;
@@ -228,7 +231,7 @@
         [XmlElement("date", Namespace="http://purl.org/dc/elements/1.1/";)] public DateTime Date;
     }
 
-    [XmlType("image")] /* Probably a LiveJournal thing. */
+    [XmlType("image")]
     public class RSSImage {
         [XmlElement("url")] public string Url = null;
         [XmlElement("title")] public string Title = null;

Modified: trunk/src/Channel.cs
==============================================================================
--- trunk/src/Channel.cs	(original)
+++ trunk/src/Channel.cs	Sun Apr  6 08:57:08 2008
@@ -4,7 +4,7 @@
 // (C) 2004 Imendio AB
 // 
 
-using Rss;
+using RSS;
 using Atom;
 using System.Collections;
 using System;
@@ -126,7 +126,7 @@
 			}
 		}
 
-		public bool UpdateItem (string id, RssItem rssItem)
+		public bool UpdateItem (string id, RSSItem rssItem)
 		{ 
 			Item item = GetItem (id);
 

Modified: trunk/src/FeedUpdater.cs
==============================================================================
--- trunk/src/FeedUpdater.cs	(original)
+++ trunk/src/FeedUpdater.cs	Sun Apr  6 08:57:08 2008
@@ -5,7 +5,7 @@
 // (C) 2004 Imendio HB
 // 
 
-using Rss;
+using RSS;
 using Atom;
 using System;
 using System.IO;
@@ -41,14 +41,16 @@
     private static string feed_type(ref string feed)
     {
         string type = null;
-
+        try {
         XPathDocument doc = new XPathDocument(new StringReader(feed));
         XPathNavigator nav = doc.CreateNavigator();
         XmlNamespaceManager nsm = new XmlNamespaceManager(nav.NameTable);
-        XPathExpression expr = nav.Compile("/atom03:feed|/atom10:feed");
+        XPathExpression expr = nav.Compile("/atom03:feed|/atom10:feed|/rss10:RDF|/rss20:rss");
         
         nsm.AddNamespace("atom10", "http://www.w3.org/2005/Atom";);
         nsm.AddNamespace("atom03", "http://purl.org/atom/ns#";);
+        nsm.AddNamespace("rss10",  "http://www.w3.org/1999/02/22-rdf-syntax-ns#";);
+        nsm.AddNamespace("rss20", "");
         expr.SetContext(nsm);
         
         XPathNodeIterator iter = nav.Select(expr);
@@ -62,6 +64,12 @@
                 case "http://purl.org/atom/ns#":
                     type = "Atom 0.3";
                 break;
+                case "http://www.w3.org/1999/02/22-rdf-syntax-ns#":
+                    type = "RSS 1.0";
+                break;
+                case "":
+                    type = "RSS 2.0";
+                break;
                 default:
                     type = "unknown";
                 break;
@@ -69,6 +77,9 @@
         } else {
             type = "unknown";
         }
+        } catch(Exception e){
+                System.Console.WriteLine("Error determining feed type: {0}", e.Message);
+        }
         
         return type;
     }
@@ -110,26 +121,8 @@
             /* If we know for certain, assign it directly. */
             if(type == "Atom 0.3" || type == "Atom 1.0"){
                 channel.Type = "Atom";
-            } else {
-                try{
-                /* We don't know the type of feed. Try RSS. */
-                RssReader reader = new RssReader(new StringReader(feed));
-                RssElement e = reader.Read();
-                    
-                if(e != null){ /* If the RSS library will take it, we say it's RSS. */
-                    if(e is RssRedirect){ /* Redirection needs special handling. */
-                        channel.Url = ((RssRedirect)e).Location.ToString();
-                        return Update(channel);
-                    } else {
-                        channel.Type = "RSS";
-                    }
-                }
-                
-                reader.Close();
-                    
-                } catch(Exception e){
-                    System.Console.WriteLine("Error whilst determining feed type: {0}", e.Message);
-                }
+            } else if (type == "RSS 1.0" || type == "RSS 2.0"){
+                channel.Type = "RSS";
             }
 
             /* If it's still unknown, we don't speak it. */
@@ -160,7 +153,7 @@
         } catch (Exception e) {
             Console.WriteLine("Error whilst updating the feed {0}: {1}", channel.Name, e.Message);
         }
-        
+
         return updated;
 	}
 	
@@ -287,7 +280,48 @@
         return reader.ReadToEnd();
     }
 
-	private static bool UpdateRssChannel (Channel channel, ref string feed_content)
+    private static bool UpdateRssChannel(Channel channel, ref string feed_content)
+    {
+        bool ChanUpdated = false;
+        RSSFeed feed = RSSFeed.Load(new StringReader(feed_content));
+
+        channel.StartRefresh();
+
+        /* FIXME: Figure out how we should support multiple channels in one feed. */
+
+        if((channel.Name == "" || channel.Name == channel.Url) &&
+                   feed.Channel[0].Title != null){
+            channel.Name = HtmlUtils.StripHtml(feed.Channel[0].Title);
+            ChanUpdated = true;
+        }
+
+        foreach (RSSItem item in feed.Channel[0].Item){
+            string id = null;
+            bool ItemUpdated = false;
+
+            id = GenerateItemId(item);
+
+            if(item.Title == null || item.Title == ""){
+                if(item.Date != DateTime.MinValue){
+                    item.Title = item.Date.ToString("d MMM yyyy");
+                } else {
+                    item.Title = feed.Channel[0].Title;
+                }
+            }
+
+            ItemUpdated = channel.UpdateItem(id, item);
+
+            if(ItemUpdated){
+                ChanUpdated = true;
+            }
+        }
+
+        channel.FinishRefresh();
+
+        return ChanUpdated;
+    }
+
+/*	private static bool UpdateRssChannel_old (Channel channel, ref string feed_content)
 	{
 	    bool channelUpdated = false;
 
@@ -307,14 +341,14 @@
 
 	    if (rssChannel != null) {
 	        /* Check if this channel is updated */
-	        channel.StartRefresh ();
+/*	        channel.StartRefresh (); */
 
 	        /*if (channel.Image.Equals ("") &&
                       rssChannel.Image != null) {
                       channel.Image = rssChannel.Image.Url.ToString ();
 	        }*/
 
-	        if ((channel.Name.Equals ("") || channel.Name.Equals(channel.Url)) &&
+	/*        if ((channel.Name.Equals ("") || channel.Name.Equals(channel.Url)) &&
 		    rssChannel.Title != null) {
 	            channel.Name = HtmlUtils.StripHtml(rssChannel.Title);
 	            channelUpdated = true;
@@ -344,7 +378,7 @@
 
 	    return channelUpdated;
 	}
-
+*/
 	private static bool UpdateAtomChannel(Channel channel, ref string feed_content)
 	{
 	    bool channelUpdated = false;
@@ -389,15 +423,15 @@
         return channelUpdated;
 	}
 
-	public static string GenerateItemId (RssItem item)
+	public static string GenerateItemId (RSSItem item)
 	{
 	    if (item.Guid != null) {
-		return item.Guid.Name;
+		    return item.Guid;
 	    } 
-	    else if (item.Link != RssDefault.Uri) {
-		return item.Link.ToString ();
+	    else if (item.Link != null) {
+		    return item.Link;
 	    } else {
-		return item.Title;
+		    return item.Title;
 	    }
 	}
 

Modified: trunk/src/Item.cs
==============================================================================
--- trunk/src/Item.cs	(original)
+++ trunk/src/Item.cs	Sun Apr  6 08:57:08 2008
@@ -4,7 +4,7 @@
 // (C) 2004 Imendio AB
 // 
 
-using Rss;
+using RSS;
 using Atom;
 using System.Xml.Serialization;
 using System;
@@ -44,25 +44,30 @@
 		{
 		}
 
-		public Item (string id, RssItem rssItem)
+		public Item (string id, RSSItem rssItem)
 		{
 			this.Id = id;
 			this.Title = HtmlUtils.StripHtml(rssItem.Title.Trim ());
-		    this.Text = rssItem.Description.Trim ();
-		    if(rssItem.Content != RssDefault.String){
-		        this.Text = rssItem.Content.Trim();
-		    }
-			if (rssItem.Link != RssDefault.Uri) {
-				this.Link = rssItem.Link.ToString().Trim();
-			} else if (rssItem.Guid != null && !rssItem.Guid.PermaLink.IsFalse) {
-				this.Link = rssItem.Guid.Name;
+            
+            if(rssItem.Description != null){
+                this.Text = rssItem.Description.Trim();
+            }
+
+            if(rssItem.Content != null){
+                this.Text = rssItem.Content.Trim();
+            }
+
+			if (rssItem.Link != null) {
+				this.Link = rssItem.Link.Trim();
+			//} else if (rssItem.Guid != null && !rssItem.Guid.PermaLink.IsFalse) {
+			//	this.Link = rssItem.Guid.Name;
 			} else {
 				this.Link = "";
 			}
 
-			this.Author = rssItem.Author.Trim();
+			this.Author = rssItem.Author;
 
-			this.PubDate = rssItem.PubDate;
+			this.PubDate = rssItem.Date;
 		}
 
 		public Item (string id, AtomEntry entry)
@@ -116,22 +121,21 @@
 		/* This is called in the middle of an refresh so the channel will be
 		 * updated when refresh is done
 		 */
-		public bool Update (RssItem rssItem)
+		public bool Update (RSSItem rssItem)
 		{
 			if (this.Title != HtmlUtils.StripHtml(rssItem.Title.Trim ()) ) {
 				this.Title = HtmlUtils.StripHtml(rssItem.Title.Trim ());
 				this.SetUnread (true, true);
 				return true;
 			}
-
-			if (this.Text != rssItem.Description.Trim ()) {
-			    this.Text = rssItem.Description.Trim ();
-			    if(this.Text != rssItem.Content.Trim()){
-			        this.Text = rssItem.Content.Trim();
-			    }
-				return true;
-			}
-
+            
+            if(rssItem.Description != null && this.Text != rssItem.Description.Trim()){
+                this.Text = rssItem.Description.Trim();
+                if(rssItem.Content != null && this.Text != rssItem.Content.Trim()){
+                    this.Text = rssItem.Content.Trim();
+                }
+                    return true;
+            }
 			return false;
 		}
 

Modified: trunk/src/Makefile.am
==============================================================================
--- trunk/src/Makefile.am	(original)
+++ trunk/src/Makefile.am	Sun Apr  6 08:57:08 2008
@@ -14,7 +14,8 @@
 	     -r:System.Web   \
 	     -r:RSS.NET.dll      \
 	     -r:Mono.Posix   \
-	     -r:../lib/AtomFeed.dll
+	     -r:../lib/AtomFeed.dll \
+	     -r:../lib/RSSFeed.dll
 
 BLAM_CSFILES = Application.cs \
 	       ChannelDialog.cs \



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