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