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