Fix some bad line breaks on HTML supports
[fanfix.git] / src / be / nikiroo / fanfix / supported / Fimfiction.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.supported;
2
68686a37
NR
3import java.awt.image.BufferedImage;
4import java.io.IOException;
08fe2e33
NR
5import java.io.InputStream;
6import java.net.MalformedURLException;
7import java.net.URL;
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12import java.util.Map.Entry;
13import java.util.Scanner;
14
15import be.nikiroo.fanfix.Instance;
68686a37
NR
16import be.nikiroo.fanfix.data.MetaData;
17import be.nikiroo.utils.StringUtils;
08fe2e33
NR
18
19/**
20 * Support class for <a href="http://www.fimfiction.net/">FimFiction.net</a>
21 * stories, a website dedicated to My Little Pony.
22 *
23 * @author niki
24 */
25class Fimfiction extends BasicSupport {
26 @Override
27 protected boolean isHtml() {
28 return true;
29 }
30
31 @Override
32 public String getSourceName() {
33 return "FimFiction.net";
34 }
35
36 @Override
68686a37
NR
37 protected MetaData getMeta(URL source, InputStream in) throws IOException {
38 MetaData meta = new MetaData();
39
40 meta.setTitle(getTitle(reset(in)));
41 meta.setAuthor(getAuthor(reset(in)));
42 meta.setDate(getDate(reset(in)));
43 meta.setTags(getTags(reset(in)));
44 meta.setSource(getSourceName());
2206ef66 45 meta.setUrl(source.toString());
68686a37
NR
46 meta.setPublisher(getSourceName());
47 meta.setUuid(source.toString());
48 meta.setLuid("");
49 meta.setLang("EN");
50 meta.setSubject("MLP");
51 meta.setType(getType().toString());
52 meta.setImageDocument(false);
53 meta.setCover(getCover(reset(in)));
54
55 return meta;
08fe2e33
NR
56 }
57
58 @Override
59 public Map<String, String> getCookies() {
60 Map<String, String> cookies = new HashMap<String, String>();
61 cookies.put("view_mature", "true");
62 return cookies;
63 }
64
68686a37 65 private List<String> getTags(InputStream in) {
08fe2e33
NR
66 List<String> tags = new ArrayList<String>();
67 tags.add("MLP");
68
69 @SuppressWarnings("resource")
70 Scanner scan = new Scanner(in, "UTF-8");
71 scan.useDelimiter("\\n");
72 while (scan.hasNext()) {
73 String line = scan.next();
74 if (line.contains("story_category") && !line.contains("title=")) {
75 int pos = line.indexOf('>');
76 if (pos >= 0) {
77 line = line.substring(pos + 1);
78 pos = line.indexOf('<');
79 if (pos >= 0) {
80 line = line.substring(0, pos);
81 }
82 }
83
84 line = line.trim();
85 if (!tags.contains(line)) {
86 tags.add(line);
87 }
88 }
89 }
90
91 return tags;
92 }
93
68686a37 94 private String getTitle(InputStream in) {
08fe2e33
NR
95 String line = getLine(in, " property=\"og:title\"", 0);
96 if (line != null) {
97 int pos = -1;
98 for (int i = 0; i < 3; i++) {
99 pos = line.indexOf('"', pos + 1);
100 }
101
102 if (pos >= 0) {
103 line = line.substring(pos + 1);
104 pos = line.indexOf('"');
105 if (pos >= 0) {
68686a37 106 return StringUtils.unhtml(line.substring(0, pos)).trim();
08fe2e33
NR
107 }
108 }
109 }
110
111 return null;
112 }
113
68686a37 114 private String getAuthor(InputStream in) {
08fe2e33
NR
115 String line = getLine(in, " href=\"/user/", 0);
116 if (line != null) {
117 int pos = line.indexOf('"');
118 if (pos >= 0) {
119 line = line.substring(pos + 1);
120 pos = line.indexOf('"');
121 if (pos >= 0) {
122 line = line.substring(0, pos);
123 pos = line.lastIndexOf('/');
124 if (pos >= 0) {
125 line = line.substring(pos + 1);
126 return line.replace('+', ' ');
127 }
128 }
129 }
130 }
131
132 return null;
133 }
134
68686a37 135 private String getDate(InputStream in) {
08fe2e33
NR
136 String line = getLine(in, "<span class=\"date\">", 0);
137 if (line != null) {
138 int pos = -1;
139 for (int i = 0; i < 3; i++) {
140 pos = line.indexOf('>', pos + 1);
141 }
142
143 if (pos >= 0) {
144 line = line.substring(pos + 1);
145 pos = line.indexOf('<');
146 if (pos >= 0) {
147 return line.substring(0, pos).trim();
148 }
149 }
150 }
151
152 return null;
153 }
154
155 @Override
156 protected String getDesc(URL source, InputStream in) {
157 // the og: meta version is the SHORT resume, this is the LONG resume
158 return getLine(in, "class=\"more_button hidden\"", -1);
159 }
160
68686a37 161 private BufferedImage getCover(InputStream in) {
08fe2e33
NR
162 // Note: the 'og:image' is the SMALL cover, not the full version
163 String cover = getLine(in, "<div class=\"story_image\">", 1);
164 if (cover != null) {
165 int pos = cover.indexOf('"');
166 if (pos >= 0) {
167 cover = cover.substring(pos + 1);
168 pos = cover.indexOf('"');
169 if (pos >= 0) {
170 cover = cover.substring(0, pos);
171 }
172 }
173 }
174
333f0e7b 175 return getImage(this, null, cover);
08fe2e33
NR
176 }
177
178 @Override
179 protected List<Entry<String, URL>> getChapters(URL source, InputStream in) {
180 List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
181 @SuppressWarnings("resource")
182 Scanner scan = new Scanner(in, "UTF-8");
183 scan.useDelimiter("\\n");
184 while (scan.hasNext()) {
185 String line = scan.next();
186 if (line.contains("class=\"chapter_link\"")
187 || line.contains("class='chapter_link'")) {
188 // Chapter name
189 String name = line;
190 int pos = name.indexOf('>');
191 if (pos >= 0) {
192 name = name.substring(pos + 1);
193 pos = name.indexOf('<');
194 if (pos >= 0) {
195 name = name.substring(0, pos);
196 }
197 }
198 // Chapter content
199 pos = line.indexOf('/');
200 if (pos >= 0) {
201 line = line.substring(pos); // we take the /, not +1
202 pos = line.indexOf('"');
203 if (pos >= 0) {
204 line = line.substring(0, pos);
205 }
206 }
207
208 try {
209 final String key = name;
210 final URL value = new URL("http://www.fimfiction.net"
211 + line);
212 urls.add(new Entry<String, URL>() {
213 public URL setValue(URL value) {
214 return null;
215 }
216
217 public String getKey() {
218 return key;
219 }
220
221 public URL getValue() {
222 return value;
223 }
224 });
225 } catch (MalformedURLException e) {
226 Instance.syserr(e);
227 }
228 }
229 }
230
231 return urls;
232 }
233
234 @Override
235 protected String getChapterContent(URL source, InputStream in, int number) {
236 return getLine(in, "<div id=\"chapter_container\">", 1);
237 }
238
239 @Override
240 protected boolean supports(URL url) {
241 return "fimfiction.net".equals(url.getHost())
242 || "www.fimfiction.net".equals(url.getHost());
243 }
244}