Version 1.4.2: fix for images documents...
[fanfix.git] / src / be / nikiroo / fanfix / supported / YiffStar.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.awt.image.BufferedImage;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Map.Entry;
13 import java.util.Scanner;
14
15 import be.nikiroo.fanfix.Instance;
16 import be.nikiroo.fanfix.bundles.Config;
17 import be.nikiroo.fanfix.data.MetaData;
18 import be.nikiroo.utils.StringUtils;
19
20 /**
21 * Support class for <a href="https://sofurry.com/">SoFurry.com</a>, a Furry
22 * website supporting images and stories (we only retrieve the stories).
23 *
24 * @author niki
25 */
26 class YiffStar extends BasicSupport {
27
28 @Override
29 public String getSourceName() {
30 return "YiffStar";
31 }
32
33 @Override
34 protected MetaData getMeta(URL source, InputStream in) throws IOException {
35 MetaData meta = new MetaData();
36
37 meta.setTitle(getTitle(reset(in)));
38 meta.setAuthor(getAuthor(source, reset(in)));
39 meta.setDate("");
40 meta.setTags(getTags(reset(in)));
41 meta.setSource(getSourceName());
42 meta.setUrl(source.toString());
43 meta.setPublisher(getSourceName());
44 meta.setUuid(source.toString());
45 meta.setLuid("");
46 meta.setLang("EN");
47 meta.setSubject("Furry");
48 meta.setType(getType().toString());
49 meta.setImageDocument(false);
50 meta.setCover(getCover(source, reset(in)));
51
52 return meta;
53 }
54
55 @Override
56 protected boolean supports(URL url) {
57 String host = url.getHost();
58 if (host.startsWith("www.")) {
59 host = host.substring("www.".length());
60 }
61
62 return "sofurry.com".equals(host);
63 }
64
65 @Override
66 protected boolean isHtml() {
67 return true;
68 }
69
70 @Override
71 public void login() throws IOException {
72 // Note: this should not be necessary anymore
73 // (the "/guest" trick is enough)
74 String login = Instance.getConfig().getString(
75 Config.LOGIN_YIFFSTAR_USER);
76 String password = Instance.getConfig().getString(
77 Config.LOGIN_YIFFSTAR_PASS);
78
79 if (login != null && !login.isEmpty() && password != null
80 && !password.isEmpty()) {
81 Map<String, String> post = new HashMap<String, String>();
82 post.put("sfLoginUsername", login);
83 post.put("sfLoginPassword", password);
84 post.put("YII_CSRF_TOKEN", "");
85
86 // Cookies will actually be retained by the cache manager once
87 // logged in
88 Instance.getCache()
89 .openNoCache(new URL("https://www.sofurry.com/user/login"),
90 this, post).close();
91 }
92 }
93
94 @Override
95 public URL getCanonicalUrl(URL source) throws IOException {
96 if (source.getPath().startsWith("/view")) {
97 source = new URL(source.toString() + "/guest");
98 InputStream in = Instance.getCache().open(source, this, false);
99 String line = getLine(in, "/browse/folder/", 0);
100 if (line != null) {
101 String[] tab = line.split("\"");
102 if (tab.length > 1) {
103 String groupUrl = source.getProtocol() + "://"
104 + source.getHost() + tab[1];
105 return guest(groupUrl);
106 }
107 }
108 }
109
110 return super.getCanonicalUrl(source);
111 }
112
113 private List<String> getTags(InputStream in) {
114 List<String> tags = new ArrayList<String>();
115
116 String line = getLine(in, "class=\"sf-story-big-tags", 0);
117 if (line != null) {
118 String[] tab = StringUtils.unhtml(line).split(",");
119 for (String possibleTag : tab) {
120 String tag = possibleTag.trim();
121 if (!tag.isEmpty() && !tag.equals("...") && !tags.contains(tag)) {
122 tags.add(tag);
123 }
124 }
125 }
126
127 return tags;
128 }
129
130 private BufferedImage getCover(URL source, InputStream in)
131 throws IOException {
132
133 List<Entry<String, URL>> chaps = getChapters(source, in);
134 if (!chaps.isEmpty()) {
135 in = Instance.getCache().open(chaps.get(0).getValue(), this, true);
136 String line = getLine(in, " name=\"og:image\"", 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 line = line.substring(0, pos);
148 if (line.contains("/thumb?")) {
149 line = line.replace("/thumb?",
150 "/auxiliaryContent?type=25&");
151 return getImage(this, null, line);
152 }
153 }
154 }
155 }
156 }
157
158 return null;
159 }
160
161 private String getAuthor(URL source, InputStream in) throws IOException {
162 String author = getLine(in, "class=\"onlinestatus", 0);
163 if (author != null) {
164 return StringUtils.unhtml(author).trim();
165 }
166
167 return null;
168 }
169
170 private String getTitle(InputStream in) throws IOException {
171 String title = getLine(in, "class=\"sflabel pagetitle", 0);
172 if (title != null) {
173 if (title.contains("(series)")) {
174 title = title.replace("(series)", "");
175 }
176 return StringUtils.unhtml(title).trim();
177 }
178
179 return null;
180 }
181
182 @Override
183 protected String getDesc(URL source, InputStream in) throws IOException {
184 return null; // TODO: no description at all? Cannot find one...
185 }
186
187 @Override
188 protected List<Entry<String, URL>> getChapters(URL source, InputStream in)
189 throws IOException {
190 List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
191
192 @SuppressWarnings("resource")
193 Scanner scan = new Scanner(in, "UTF-8");
194 scan.useDelimiter("\\n");
195 while (scan.hasNext()) {
196 String line = scan.next();
197 if (line.contains("\"/view/") && line.contains("title=")) {
198 String[] tab = line.split("\"");
199 if (tab.length > 5) {
200 String link = tab[5];
201 if (link.startsWith("/")) {
202 link = source.getProtocol() + "://" + source.getHost()
203 + link;
204 }
205 final URL value = guest(link);
206 final String key = StringUtils.unhtml(line).trim();
207 urls.add(new Entry<String, URL>() {
208 public URL setValue(URL value) {
209 return null;
210 }
211
212 public URL getValue() {
213 return value;
214 }
215
216 public String getKey() {
217 return key;
218 }
219 });
220 }
221 }
222 }
223
224 return urls;
225 }
226
227 @Override
228 protected String getChapterContent(URL source, InputStream in, int number)
229 throws IOException {
230 StringBuilder builder = new StringBuilder();
231
232 String startAt = "id=\"sfContentBody";
233 String endAt = "id=\"recommendationArea";
234 boolean ok = false;
235
236 @SuppressWarnings("resource")
237 Scanner scan = new Scanner(in, "UTF-8");
238 scan.useDelimiter("\\n");
239 while (scan.hasNext()) {
240 String line = scan.next();
241 if (!ok && line.contains(startAt)) {
242 ok = true;
243 } else if (ok && line.contains(endAt)) {
244 ok = false;
245 break;
246 }
247
248 if (ok) {
249 builder.append(line);
250 builder.append(' ');
251 }
252 }
253
254 return builder.toString();
255 }
256
257 /**
258 * Return a {@link URL} from the given link, but add the "/guest" part to it
259 * to make sure we don't need to be logged-in to see it.
260 *
261 * @param link
262 * the link
263 *
264 * @return the {@link URL}
265 *
266 * @throws MalformedURLException
267 */
268 private URL guest(String link) throws MalformedURLException {
269 if (link.contains("?")) {
270 if (link.contains("/?")) {
271 return new URL(link.replace("?", "guest?"));
272 } else {
273 return new URL(link.replace("?", "/guest?"));
274 }
275 } else {
276 return new URL(link + "/guest");
277 }
278 }
279 }