Add support for lwn.net (WIP)
[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.Comment;
11 import be.nikiroo.gofetch.data.Story;
12
13 public abstract class BasicSupport {
14 public enum Type {
15 SLASHDOT, PIPEDOT, LWN,
16 }
17
18 static private String preselector;
19
20 private Type type;
21
22 abstract public List<Story> list() throws IOException;
23
24 abstract public List<Comment> getComments(Story story) throws IOException;
25
26 abstract public String getDescription();
27
28 public String getSelector() {
29 return getSelector(type);
30 }
31
32 public Type getType() {
33 return type;
34 }
35
36 protected void setType(Type type) {
37 this.type = type;
38 }
39
40 /**
41 * @param preselector
42 * the preselector to set
43 */
44 static public void setPreselector(String preselector) {
45 BasicSupport.preselector = preselector;
46 }
47
48 static public BasicSupport getSupport(Type type) {
49 BasicSupport support = null;
50
51 if (type != null) {
52 switch (type) {
53 case SLASHDOT:
54 support = new Slashdot();
55 break;
56 case PIPEDOT:
57 support = new Pipedot();
58 break;
59 case LWN:
60 support = new LWN();
61 break;
62 }
63
64 if (support != null) {
65 support.setType(type);
66 }
67 }
68
69 return support;
70 }
71
72 static public String getSelector(Type type) {
73 return preselector + "/" + type + "/";
74 }
75
76 // TODO: check Downloader.java?
77 static protected InputStream open(URL url) throws IOException {
78 URLConnection conn = url.openConnection();
79 conn.connect();
80 InputStream in = conn.getInputStream();
81 if ("gzip".equals(conn.getContentEncoding())) {
82 in = new GZIPInputStream(in);
83 }
84
85 return in;
86 }
87 }