Fix download order and comments/content storing
[gofetch.git] / src / be / nikiroo / gofetch / support / BasicSupport.java
1 package be.nikiroo.gofetch.support;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.net.URLConnection;
7 import java.util.List;
8 import java.util.zip.GZIPInputStream;
9
10 import be.nikiroo.gofetch.data.Story;
11
12 public abstract class BasicSupport {
13 public enum Type {
14 SLASHDOT, PIPEDOT, LWN,
15 }
16
17 static private String preselector;
18
19 private Type type;
20
21 abstract public List<Story> list() throws IOException;
22
23 /**
24 * Fetch the full article content as well as all the comments associated to
25 * this {@link Story}, if any (can be empty, but not NULL).
26 *
27 * @param story
28 * the story to fetch the comments of
29 *
30 * @throws IOException
31 * in case of I/O error
32 */
33 abstract public void fetch(Story story) throws IOException;
34
35 abstract public String getDescription();
36
37 public String getSelector() {
38 return getSelector(type);
39 }
40
41 public Type getType() {
42 return type;
43 }
44
45 protected void setType(Type type) {
46 this.type = type;
47 }
48
49 /**
50 * @param preselector
51 * the preselector to set
52 */
53 static public void setPreselector(String preselector) {
54 BasicSupport.preselector = preselector;
55 }
56
57 static public BasicSupport getSupport(Type type) {
58 BasicSupport support = null;
59
60 if (type != null) {
61 switch (type) {
62 case SLASHDOT:
63 support = new Slashdot();
64 break;
65 case PIPEDOT:
66 support = new Pipedot();
67 break;
68 case LWN:
69 support = new LWN();
70 break;
71 }
72
73 if (support != null) {
74 support.setType(type);
75 }
76 }
77
78 return support;
79 }
80
81 static public String getSelector(Type type) {
82 return preselector + "/" + type + "/";
83 }
84
85 // TODO: check Downloader.java?
86 static protected InputStream open(URL url) throws IOException {
87 URLConnection conn = url.openConnection();
88 conn.connect();
89 InputStream in = conn.getInputStream();
90 if ("gzip".equals(conn.getContentEncoding())) {
91 in = new GZIPInputStream(in);
92 }
93
94 return in;
95 }
96 }