-fix missing file causes exception (e.g., when old html files are deleted but not gopher files)
- (hopefully) fix slashdot connection error via new nikiroo-utils Downloader
}
ref = "../" + ref + "/index.html";
- htmlBuilder.append(getLink(support.getDescription(),
- ref, true, true));
+ htmlBuilder.append(getLink(support.getDescription(), ref, true,
+ true));
}
File gopherCache = new File(dir, preselector);
List<String> gopherLines = new ArrayList<String>();
List<String> htmlLines = new ArrayList<String>();
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()
- + "gophermap_"
- + (page + 1),
- true,
- false));
-
- htmlLines.add(getLink("More",
- "index_"
- + (page + 1)
- + ".html",
- true,
- true));
+ gopherLines.add(getLink("More", support.getSelector()
+ + "gophermap_" + (page + 1), true, false));
+
+ htmlLines.add(getLink("More", "index_" + (page + 1)
+ + ".html", true, true));
}
write(gopherLines, varDir, "gophermap", "", page);
}
/**
+ * 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
+ * menu (gophermap, i) mode -- not used in html mode
* @param html
- * @return
+ * 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) {
package be.nikiroo.gofetch.support;
import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
-import java.util.zip.GZIPInputStream;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Element;
import org.jsoup.select.NodeVisitor;
import be.nikiroo.gofetch.data.Story;
+import be.nikiroo.utils.Downloader;
public abstract class BasicSupport {
+ protected static Downloader downloader = new Downloader("gofetcher");
+
public enum Type {
SLASHDOT, PIPEDOT, LWN, LEMONDE,
}
return preselector + "/" + type + "/";
}
- // TODO: check Downloader.java?
- static protected InputStream open(URL url) throws IOException {
- URLConnection conn = url.openConnection();
- conn.connect();
- InputStream in = conn.getInputStream();
- if ("gzip".equals(conn.getContentEncoding())) {
- in = new GZIPInputStream(in);
- }
-
- return in;
- }
-
/**
* Get the first {@link Element} of the given class, or an empty span
* {@link Element} if none found.
List<Story> list = new ArrayList<Story>();
URL url = new URL("https://lwn.net/");
- InputStream in = open(url);
+ InputStream in = downloader.open(url);
Document doc = DataUtil.load(in, "UTF-8", url.toString());
Elements articles = doc.getElementsByClass("pure-u-1");
for (Element article : articles) {
// Do not try the paid-for stories...
if (!story.getTitle().startsWith("[$]")) {
URL url = new URL(story.getUrlInternal());
- InputStream in = open(url);
+ InputStream in = downloader.open(url);
Document doc = DataUtil.load(in, "UTF-8", url.toString());
Elements fullContentElements = doc
.getElementsByClass("ArticleText");
for (String topic : new String[] { "international", "politique",
"societe", "sciences" }) {
URL url = new URL("http://www.lemonde.fr/" + topic + "/1.html");
- InputStream in = open(url);
+ InputStream in = downloader.open(url);
Document doc = DataUtil.load(in, "UTF-8", url.toString());
Elements articles = doc.getElementsByTag("article");
for (Element article : articles) {
// some javascript, I need to check...)
URL url = new URL(story.getUrlInternal());
- InputStream in = open(url);
+ InputStream in = downloader.open(url);
Document doc = DataUtil.load(in, "UTF-8", url.toString());
Element article = doc.getElementById("articleBody");
if (article != null) {
List<Story> list = new ArrayList<Story>();
URL url = new URL("https://pipedot.org/");
- InputStream in = open(url);
+ InputStream in = downloader.open(url);
Document doc = DataUtil.load(in, "UTF-8", url.toString());
Elements articles = doc.getElementsByClass("story");
for (Element article : articles) {
List<Comment> comments = new ArrayList<Comment>();
URL url = new URL(story.getUrlInternal());
- InputStream in = open(url);
+ InputStream in = downloader.open(url);
Document doc = DataUtil.load(in, "UTF-8", url.toString());
Elements listing = doc.getElementsByTag("main");
if (listing.size() > 0) {
List<Story> list = new ArrayList<Story>();
URL url = new URL("https://slashdot.org/");
- InputStream in = open(url);
+ InputStream in = downloader.open(url);
Document doc = DataUtil.load(in, "UTF-8", url.toString());
Elements articles = doc.getElementsByTag("header");
for (Element article : articles) {
List<Comment> comments = new ArrayList<Comment>();
URL url = new URL(story.getUrlInternal());
- InputStream in = open(url);
+ InputStream in = downloader.open(url);
Document doc = DataUtil.load(in, "UTF-8", url.toString());
Element listing = doc.getElementById("commentlisting");
if (listing != null) {