contrib/rss_downloader: fix nullable usage for title and link
[nit.git] / contrib / rss_downloader / src / rss_downloader.nit
index 3551686..022543e 100644 (file)
@@ -55,6 +55,9 @@ class Config
        # XML tag of the link to act upon
        fun tag_link: String do return "link"
 
+       # Are the feeds at `rss_source_urls` compressed?
+       var compressed: nullable Bool
+
        # Action to apply on each selected RSS element
        fun act_on(element: Element)
        do
@@ -134,6 +137,7 @@ class Downloader
                var elements = new HashSet[Element]
                for rss_url in config.rss_source_urls do
                        var rss = rss_url.fetch_rss_content
+                       if config.compressed == true then rss = rss.gunzip
                        elements.add_all rss.to_rss_elements
                end
 
@@ -147,7 +151,7 @@ class Downloader
 
                if sys.verbose then
                        print "\n# {matches.length} matching elements:"
-                       print matches.join("\n")
+                       print "* " + matches.join("\n* ")
                        print "\n# Downloading..."
                end
 
@@ -158,15 +162,15 @@ class Downloader
                                # Do not download a file that is not unique according to `unique_id`
                                if not element.is_unique_exception(config) then
                                        # We make some exceptions
-                                       if sys.verbose then print "File in log, skipping {element}"
+                                       if sys.verbose then print "- Skipping {element}"
                                        continue
                                end
                        end
 
                        # Download element
-                       if sys.verbose then print "Acting on {element}"
+                       if sys.verbose then print "+ Acting on {element}"
 
-                       tool_config.act_on element
+                       tool_config.as(not null).act_on element
 
                        # Add `unique_id` to log
                        history.add unique_id
@@ -201,7 +205,7 @@ class Downloader
                                continue
                        end
 
-                       for dir in source_folder.files do if dir.stat.is_dir then
+                       for dir in source_folder.files do if dir.stat.as(not null).is_dir then
                                folder_names.add dir.filename
                        end
                end
@@ -267,19 +271,38 @@ redef class Text
 
                var elements = new Array[Element]
                for item in items do
-                       var title = item[tool_config.tag_title].first.as(XMLStartTag).data
-                       var link = item[tool_config.tag_link].first.as(XMLStartTag).data
+                       var title = item[tool_config.as(not null).tag_title].first.as(XMLStartTag).data
+                       var link = item[tool_config.as(not null).tag_link].first.as(XMLStartTag).data
+
+                       if title == null then
+                               print_error "RSS Parse Error: title is null"
+                               return elements
+                       end
+
+                       if link == null then
+                               print_error "RSS Parse Error: link is null"
+                               return elements
+                       end
 
                        elements.add new Element(title, link)
                end
 
                if sys.verbose then
                        print "# Found elements:"
-                       print elements.join("\n")
+                       print "* " + elements.join("\n* ")
                end
 
                return elements
        end
+
+       # Expand the Lempel-Ziv encoded `self`
+       fun gunzip: String
+       do
+               var proc = new ProcessDuplex("gunzip", new Array[String]...)
+               var res = proc.write_and_read(self)
+               assert proc.status == 0 else print_error "gunzip failed: {proc.last_error or else "Unknown"}"
+               return res
+       end
 end
 
 # Implement this method in your module to configure this tool