1 package be
.nikiroo
.gofetch
.support
;
3 import java
.io
.IOException
;
4 import java
.text
.ParseException
;
5 import java
.text
.SimpleDateFormat
;
6 import java
.util
.ArrayList
;
10 import org
.jsoup
.helper
.StringUtil
;
11 import org
.jsoup
.nodes
.Element
;
12 import org
.jsoup
.nodes
.Node
;
13 import org
.jsoup
.nodes
.TextNode
;
14 import org
.jsoup
.select
.Elements
;
15 import org
.jsoup
.select
.NodeTraversor
;
16 import org
.jsoup
.select
.NodeVisitor
;
18 import be
.nikiroo
.gofetch
.data
.Story
;
19 import be
.nikiroo
.utils
.Downloader
;
22 * Base class for website support.
26 public abstract class BasicSupport
{
27 /** The downloader to use for all websites. */
28 protected static Downloader downloader
= new Downloader("gofetcher");
31 * The support type (each website we support has a single type).
36 /** EN: Any, but mostly IT/Sci */
38 /** EN: Clone of Slashdot, mostly abandoned */
51 * Used to process an element into lines.
55 public interface ElementProcessor
{
57 * Detect if this node is a quote and should be trated as such.
61 * @return TRUE if it is
63 public boolean detectQuote(Node node
);
66 * Process text content (will be called on each text element, allowing
67 * you to modify it if needed).
72 * @return the resulting text
74 public String
processText(String text
);
81 * @return TRUE if it has to be ignored
83 public boolean ignoreNode(Node node
);
86 * Manually process this node (and return the manual processing value)
89 * If the node is manually processed, it and its children will not be
90 * automatically processed.
93 * the node to optionally process
95 * @return NULL if not processed (will thus be automatically processed
96 * as usual), a {@link String} (may be empty) if we process it
97 * manually -- the given {@link String} will be used instead of
98 * the usual automatic processing if not NULL
100 public String
manualProcessing(Node node
);
104 * A default {@link ElementProcessor} (will not detect or process anything
109 public class BasicElementProcessor
implements ElementProcessor
{
111 public boolean detectQuote(Node node
) {
116 public String
processText(String text
) {
121 public boolean ignoreNode(Node node
) {
126 public String
manualProcessing(Node node
) {
131 static private String preselector
;
136 * List all the recent items, but only assure the ID and internal URL to
137 * fetch it later on (until it has been fetched, the rest of the
138 * {@link Story} is not confirmed).
140 * @return the list of new stories
142 * @throws IOException
145 abstract public List
<Story
> list() throws IOException
;
148 * Fetch the full article content as well as all the comments associated to
149 * this {@link Story}, if any (can be empty, but not NULL).
152 * the story to fetch the comments of
154 * @throws IOException
155 * in case of I/O error
157 abstract public void fetch(Story story
) throws IOException
;
160 * The website textual description, to add in the dispatcher page.
164 * @return the description
166 abstract public String
getDescription();
169 * The gopher "selector" to use for output.
171 * A kind of "URL path", like "/news/" or "/misc/news/" or...
173 * @return the selector
175 public String
getSelector() {
176 return getSelector(type
);
184 public Type
getType() {
194 protected void setType(Type type
) {
199 * The {@link String} to append to the selector (the selector will be
200 * constructed as "this string" then "/type/".
203 * the preselector to set
205 static public void setPreselector(String preselector
) {
206 BasicSupport
.preselector
= preselector
;
210 * Return a {@link BasicSupport} that is compatible with the given
211 * {@link Type} if it exists (or NULL if not).
216 * @return a compatible {@link BasicSupport} if it exists (or NULL if not)
218 static public BasicSupport
getSupport(Type type
) {
219 BasicSupport support
= null;
224 support
= new Slashdot();
227 support
= new Pipedot();
233 support
= new LeMonde();
236 support
= new TheRegister();
239 support
= new TooLinux();
243 if (support
!= null) {
244 support
.setType(type
);
252 * The gopher "selector" to use for output for this type, using the
255 * A kind of "URL path", like "/news/" or "/misc/news/" or...
258 * the type to get the selector of
260 * @return the selector
262 static public String
getSelector(Type type
) {
263 return preselector
+ "/" + type
+ "/";
267 * Get the first {@link Element} of the given class, or an empty span
268 * {@link Element} if none found.
271 * the element to look in
273 * the class to look for
275 * @return the value or an empty span {@link Element}
277 static protected Element
firstOrEmpty(Element element
, String className
) {
278 Elements subElements
= element
.getElementsByClass(className
);
279 if (subElements
.size() > 0) {
280 return subElements
.get(0);
283 return new Element("span");
287 * Get the first {@link Element} of the given tag, or an empty span
288 * {@link Element} if none found.
291 * the element to look in
293 * the tag to look for
295 * @return the value or an empty span {@link Element}
297 static protected Element
firstOrEmptyTag(Element element
, String tagName
) {
298 Elements subElements
= element
.getElementsByTag(tagName
);
299 if (subElements
.size() > 0) {
300 return subElements
.get(0);
303 return new Element("span");
307 * Process the given element into text (each line is a text paragraph and
308 * can be prepended with ">" signs to indicate a quote or sub-quote or
312 * the element to process
313 * @param elementProcessor
314 * the element processor, must not be NULL
316 * @return text lines, each line is a paragraph
318 static protected List
<String
> toLines(Element element
,
319 final ElementProcessor elementProcessor
) {
320 final List
<String
> lines
= new ArrayList
<String
>();
321 final StringBuilder currentLine
= new StringBuilder();
322 final List
<Integer
> quoted
= new ArrayList
<Integer
>();
323 final List
<Node
> ignoredNodes
= new ArrayList
<Node
>();
325 if (element
!= null) {
326 new NodeTraversor(new NodeVisitor() {
328 public void head(Node node
, int depth
) {
329 String manual
= null;
330 boolean ignore
= elementProcessor
.ignoreNode(node
)
331 || ignoredNodes
.contains(node
.parentNode());
333 manual
= elementProcessor
.manualProcessing(node
);
334 if (manual
!= null) {
335 currentLine
.append(manual
);
341 ignoredNodes
.add(node
);
346 for (int i
= 0; i
< quoted
.size(); i
++) {
351 boolean enterQuote
= elementProcessor
.detectQuote(node
);
352 boolean leaveQuote
= quoted
.contains(depth
);
359 quoted
.remove(Integer
.valueOf(depth
));
362 if (enterQuote
|| leaveQuote
) {
363 if (currentLine
.length() > 0) {
364 if (currentLine
.charAt(currentLine
.length() - 1) == '\n') {
365 currentLine
.setLength(currentLine
.length() - 1);
367 for (String l
: currentLine
.toString().split("\n")) {
371 currentLine
.setLength(0);
374 if (node
instanceof Element
) {
375 Element element
= (Element
) node
;
376 boolean block
= element
.isBlock()
377 || element
.tagName().equalsIgnoreCase("br");
378 if (block
&& currentLine
.length() > 0) {
379 currentLine
.append("\n");
381 } else if (node
instanceof TextNode
) {
382 TextNode textNode
= (TextNode
) node
;
383 String line
= StringUtil
.normaliseWhitespace(textNode
386 currentLine
.append(elementProcessor
.processText(line
));
387 currentLine
.append(" ");
392 public void tail(Node node
, int depth
) {
394 }).traverse(element
);
397 if (currentLine
.length() > 0) {
399 for (int i
= 0; i
< quoted
.size(); i
++) {
403 if (currentLine
.length() > 0) {
404 if (currentLine
.charAt(currentLine
.length() - 1) == '\n') {
405 currentLine
.setLength(currentLine
.length() - 1);
407 for (String l
: currentLine
.toString().split("\n")) {
413 for (int i
= 0; i
< lines
.size(); i
++) {
414 lines
.set(i
, lines
.get(i
).replace(" ", " ").trim());
421 * Reformat the date if possible.
426 * @return the reformated date, or the same value if it was not parsable
428 static protected String
date(String date
) {
429 SimpleDateFormat out
= new SimpleDateFormat("yyyy/MM/dd");
433 epoch
= Long
.parseLong(date
);
434 } catch (Exception e
) {
439 return out
.format(new Date(1000 * epoch
));
443 Date dat
= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
445 return out
.format(dat
);
446 } catch (ParseException e
) {