X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fgofetch%2FFetcher.java;h=cb77f657beb706dbf9e1fefc48cfc567e01d090a;hb=3b9c23bd274b24cb4a13fc3435f83e9d7bbafab8;hp=b24d3d11ecadbefee0cc697b5ac29ccbf7b8fd72;hpb=9a2f4ede86734a5bae30ef38c9821946c51dfbe8;p=gofetch.git
diff --git a/src/be/nikiroo/gofetch/Fetcher.java b/src/be/nikiroo/gofetch/Fetcher.java
index b24d3d1..cb77f65 100644
--- a/src/be/nikiroo/gofetch/Fetcher.java
+++ b/src/be/nikiroo/gofetch/Fetcher.java
@@ -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
* (will 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(),
@@ -93,32 +98,32 @@ public class Fetcher {
}
ref = "../" + ref + "/index.html";
- htmlBuilder.append(getLink(support.getDescription(), ref, false,
+ htmlBuilder.append(getLink(support.getDescription(), ref, true,
true));
}
File gopherCache = new File(dir, preselector);
gopherCache.mkdirs();
File htmlIndex = new File(gopherCache, "index.html");
- gopherCache = new File(gopherCache, ".cache");
+ gopherCache = new File(gopherCache, "gophermap");
Output gopher = new Gopher(null, hostname, preselector, port);
Output html = new Html(null, hostname, preselector, port);
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 fetchedStories = new ArrayList(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,27 +196,35 @@ 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 gopherLines = new ArrayList();
List htmlLines = new ArrayList();
+ gopherLines.add(gopher.getIndexHeader(support));
+ htmlLines.add(html.getIndexHeader(support));
for (i = 0; i < headers.length; i++) {
- gopherLines
- .add(IOUtils.readSmallFile(new File(varDir, headers[i])));
- htmlLines.add(IOUtils.readSmallFile(new File(varDir, headers[i]
- + ".html")));
+ File gopherFile = new File(varDir, headers[i]);
+ File htmlFile = new File(varDir, headers[i] + ".html");
+
+ if (gopherFile.exists())
+ gopherLines.add(IOUtils.readSmallFile(gopherFile));
+ if (htmlFile.exists())
+ htmlLines.add(IOUtils.readSmallFile(htmlFile));
boolean enoughStories = (i > 0 && i % maxStories == 0);
boolean last = i == headers.length - 1;
if (enoughStories || last) {
if (!last) {
gopherLines.add(getLink("More", support.getSelector()
- + ".cache_" + (page + 1), false, false));
+ + "gophermap_" + (page + 1), true, false));
+
htmlLines.add(getLink("More", "index_" + (page + 1)
- + ".html", false, true));
+ + ".html", true, true));
}
- write(gopherLines, varDir, ".cache", "", page);
+ gopherLines.add(gopher.getIndexFooter(support));
+ htmlLines.add(html.getIndexFooter(support));
+ write(gopherLines, varDir, "gophermap", "", page);
write(htmlLines, varDir, "index", ".html", page);
gopherLines = new ArrayList();
htmlLines = new ArrayList();
@@ -212,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 lines, File varDir, String basename,
String ext, int page) throws IOException {
File file = new File(varDir, basename + (page > 0 ? "_" + page : "")
@@ -227,10 +266,24 @@ public class Fetcher {
}
}
- private String getLink(String name, String ref, boolean index, boolean html) {
+ /**
+ * Create a link.
+ *
+ * @param name
+ * the link name (what the user will see)
+ * @param ref
+ * the actual link reference (the target)
+ * @param menu
+ * menu (gophermap, i) mode -- not used in html mode
+ * @param html
+ * TRUE for html mode, FALSE for gopher mode
+ *
+ * @return the ready-to-use link in a {@link String}
+ */
+ private String getLink(String name, String ref, boolean menu, boolean html) {
if (!html) {
- return new StringBuilder().append((index ? "1" : "0") + name)
- .append("\t").append("1" + ref) //
+ return new StringBuilder().append((menu ? "1" : "0") + name)
+ .append("\t").append(ref) //
.append("\t").append(hostname) //
.append("\t").append(Integer.toString(port)) //
.append("\r\n").toString();