Add title in index pages, add reference in story
[gofetch.git] / src / be / nikiroo / gofetch / Fetcher.java
index 5f81edabcb887cdb9e211c558331bab8a78339ec..cb77f657beb706dbf9e1fefc48cfc567e01d090a 100644 (file)
@@ -14,7 +14,7 @@ import be.nikiroo.gofetch.output.Gopher;
 import be.nikiroo.gofetch.output.Html;
 import be.nikiroo.gofetch.output.Output;
 import be.nikiroo.gofetch.support.BasicSupport;
-import be.nikiroo.gofetch.support.BasicSupport.Type;
+import be.nikiroo.gofetch.support.Type;
 import be.nikiroo.utils.IOUtils;
 
 /**
@@ -42,8 +42,8 @@ public class Fetcher {
         *            the sub directory and (pre-)selector to use for the resources
         *            (<b>will</b> have an impact on the files' content)
         * @param type
-        *            the type of news to get (or the special keyword ALL to get all
-        *            of the supported sources)
+        *            the type of news to get (or NULL to get all of the supported
+        *            sources)
         * @param maxStories
         *            the maximum number of stories to show on the resume page
         * @param hostname
@@ -81,7 +81,12 @@ public class Fetcher {
                        BasicSupport support = BasicSupport.getSupport(type);
 
                        if (type == this.type || this.type == null) {
-                               list(support);
+                               try {
+                                       list(support);
+                               } catch (Exception e) {
+                                       new Exception("Failed to process support: " + type, e)
+                                                       .printStackTrace();
+                               }
                        }
 
                        gopherBuilder.append(getLink(support.getDescription(),
@@ -107,18 +112,18 @@ public class Fetcher {
 
                FileWriter writer = new FileWriter(gopherCache);
                try {
-                       writer.append(gopher.getIndexHeader());
+                       writer.append(gopher.getMainIndexHeader());
                        writer.append(gopherBuilder.toString());
-                       writer.append(gopher.getIndexFooter());
+                       writer.append(gopher.getMainIndexFooter());
                } finally {
                        writer.close();
                }
 
                try {
                        writer = new FileWriter(htmlIndex);
-                       writer.append(html.getIndexHeader());
+                       writer.append(html.getMainIndexHeader());
                        writer.append(htmlBuilder.toString());
-                       writer.append(html.getIndexFooter());
+                       writer.append(html.getMainIndexFooter());
                } finally {
                        writer.close();
                }
@@ -142,12 +147,20 @@ public class Fetcher {
 
                // Get comments (and update stories if needed):
                int i = 1;
+               List<Story> fetchedStories = new ArrayList<Story>(stories.size());
                for (Story story : stories) {
-                       System.err.println(String.format("%02d/%02d", i, stories.size())
+                       System.err.print(String.format("%02d/%02d", i, stories.size())
                                        + " Fetching full story " + story.getId() + "...");
-                       support.fetch(story);
+                       try {
+                               support.fetch(story);
+                               fetchedStories.add(story);
+                               System.err.println();
+                       } catch (IOException e) {
+                               System.err.println(" Failed to get story!");
+                       }
                        i++;
                }
+               stories = fetchedStories;
 
                Output gopher = new Gopher(support.getType(), hostname, preselector,
                                port);
@@ -183,10 +196,12 @@ public class Fetcher {
                headers = tmp.toArray(new String[] {});
                //
 
-               // Write the index (with "MORE" links if needed)
+               // Write the main index (with "MORE" links if needed)
                int page = 0;
                List<String> gopherLines = new ArrayList<String>();
                List<String> htmlLines = new ArrayList<String>();
+               gopherLines.add(gopher.getIndexHeader(support));
+               htmlLines.add(html.getIndexHeader(support));
                for (i = 0; i < headers.length; i++) {
                        File gopherFile = new File(varDir, headers[i]);
                        File htmlFile = new File(varDir, headers[i] + ".html");
@@ -207,6 +222,8 @@ public class Fetcher {
                                                        + ".html", true, true));
                                }
 
+                               gopherLines.add(gopher.getIndexFooter(support));
+                               htmlLines.add(html.getIndexFooter(support));
                                write(gopherLines, varDir, "gophermap", "", page);
                                write(htmlLines, varDir, "index", ".html", page);
                                gopherLines = new ArrayList<String>();
@@ -216,6 +233,24 @@ public class Fetcher {
                }
        }
 
+       /**
+        * Write an index/gophermap file with the given link content for the
+        * selected supported web site.
+        * 
+        * @param lines
+        *            the link content (the stories and a short description)
+        * @param varDir
+        *            the base directory to write into
+        * @param basename
+        *            the base file name
+        * @param ext
+        *            the file extension (for instance, ".html")
+        * @param page
+        *            the page number (0 = main index)
+        * 
+        * @throws IOException
+        *             in case of I/O errors
+        */
        private void write(List<String> lines, File varDir, String basename,
                        String ext, int page) throws IOException {
                File file = new File(varDir, basename + (page > 0 ? "_" + page : "")