Merge commit '3519cb5c518d569235beaedfc3071cba45ec848d'
[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()) {
27f3b37f 77
13285ff8 78 Map<String, String> post = new HashMap<String, String>();
27f3b37f
N
79 post.put("LoginForm[sfLoginUsername]", login);
80 post.put("LoginForm[sfLoginPassword]", password);
13285ff8 81 post.put("YII_CSRF_TOKEN", "");
27f3b37f
N
82 post.put("yt1", "Login");
83 post.put("returnUrl", "/");
13285ff8
NR
84
85 // Cookies will actually be retained by the cache manager once
86 // logged in
87 Instance.getCache()
88 .openNoCache(new URL("https://www.sofurry.com/user/login"),
315f14ae 89 this, post, null, null).close();
13285ff8 90 }
a4143cd7
NR
91 }
92
93 @Override
0ffa4754
NR
94 public URL getCanonicalUrl(URL source) {
95 try {
96 if (source.getPath().startsWith("/view")) {
27f3b37f
N
97 source = guest(source.toString());
98 // NO CACHE because we don't want the NotLoggedIn message later
99 InputStream in = Instance.getCache().openNoCache(source, this,
100 null, null, null);
0ffa4754
NR
101 String line = getLine(in, "/browse/folder/", 0);
102 if (line != null) {
103 String[] tab = line.split("\"");
104 if (tab.length > 1) {
105 String groupUrl = source.getProtocol() + "://"
106 + source.getHost() + tab[1];
107 return guest(groupUrl);
108 }
6e06d2cc 109 }
a4143cd7 110 }
0ffa4754
NR
111 } catch (Exception e) {
112 Instance.getTraceHandler().error(e);
a4143cd7
NR
113 }
114
115 return super.getCanonicalUrl(source);
116 }
117
118 private List<String> getTags(InputStream in) {
119 List<String> tags = new ArrayList<String>();
120
121 String line = getLine(in, "class=\"sf-story-big-tags", 0);
122 if (line != null) {
123 String[] tab = StringUtils.unhtml(line).split(",");
124 for (String possibleTag : tab) {
125 String tag = possibleTag.trim();
126 if (!tag.isEmpty() && !tag.equals("...") && !tags.contains(tag)) {
127 tags.add(tag);
128 }
129 }
130 }
131
132 return tags;
133 }
134
16a81ef7 135 private Image getCover(URL source, InputStream in) throws IOException {
a4143cd7 136
ed08c171 137 List<Entry<String, URL>> chaps = getChapters(source, in, null);
a4143cd7
NR
138 if (!chaps.isEmpty()) {
139 in = Instance.getCache().open(chaps.get(0).getValue(), this, true);
140 String line = getLine(in, " name=\"og:image\"", 0);
141 if (line != null) {
142 int pos = -1;
143 for (int i = 0; i < 3; i++) {
144 pos = line.indexOf('"', pos + 1);
145 }
146
147 if (pos >= 0) {
148 line = line.substring(pos + 1);
149 pos = line.indexOf('"');
150 if (pos >= 0) {
151 line = line.substring(0, pos);
152 if (line.contains("/thumb?")) {
153 line = line.replace("/thumb?",
154 "/auxiliaryContent?type=25&");
155 return getImage(this, null, line);
156 }
157 }
158 }
159 }
160 }
161
162 return null;
163 }
164
211f7ddb 165 private String getAuthor(InputStream in) {
a4143cd7
NR
166 String author = getLine(in, "class=\"onlinestatus", 0);
167 if (author != null) {
168 return StringUtils.unhtml(author).trim();
169 }
170
171 return null;
172 }
173
211f7ddb 174 private String getTitle(InputStream in) {
a4143cd7
NR
175 String title = getLine(in, "class=\"sflabel pagetitle", 0);
176 if (title != null) {
177 if (title.contains("(series)")) {
178 title = title.replace("(series)", "");
179 }
180 return StringUtils.unhtml(title).trim();
181 }
182
183 return null;
184 }
185
186 @Override
187 protected String getDesc(URL source, InputStream in) throws IOException {
188 return null; // TODO: no description at all? Cannot find one...
189 }
190
191 @Override
ed08c171
NR
192 protected List<Entry<String, URL>> getChapters(URL source, InputStream in,
193 Progress pg) throws IOException {
a4143cd7
NR
194 List<Entry<String, URL>> urls = new ArrayList<Entry<String, URL>>();
195
196 @SuppressWarnings("resource")
197 Scanner scan = new Scanner(in, "UTF-8");
198 scan.useDelimiter("\\n");
199 while (scan.hasNext()) {
200 String line = scan.next();
201 if (line.contains("\"/view/") && line.contains("title=")) {
202 String[] tab = line.split("\"");
203 if (tab.length > 5) {
204 String link = tab[5];
205 if (link.startsWith("/")) {
206 link = source.getProtocol() + "://" + source.getHost()
207 + link;
208 }
ce297a79
NR
209 urls.add(new AbstractMap.SimpleEntry<String, URL>(
210 StringUtils.unhtml(line).trim(), guest(link)));
a4143cd7
NR
211 }
212 }
213 }
214
215 return urls;
216 }
217
218 @Override
ed08c171
NR
219 protected String getChapterContent(URL source, InputStream in, int number,
220 Progress pg) throws IOException {
a4143cd7
NR
221 StringBuilder builder = new StringBuilder();
222
223 String startAt = "id=\"sfContentBody";
224 String endAt = "id=\"recommendationArea";
225 boolean ok = false;
226
227 @SuppressWarnings("resource")
228 Scanner scan = new Scanner(in, "UTF-8");
229 scan.useDelimiter("\\n");
230 while (scan.hasNext()) {
231 String line = scan.next();
232 if (!ok && line.contains(startAt)) {
233 ok = true;
234 } else if (ok && line.contains(endAt)) {
235 ok = false;
236 break;
237 }
238
239 if (ok) {
240 builder.append(line);
406447a4 241 builder.append(' ');
a4143cd7
NR
242 }
243 }
244
245 return builder.toString();
246 }
13285ff8
NR
247
248 /**
249 * Return a {@link URL} from the given link, but add the "/guest" part to it
250 * to make sure we don't need to be logged-in to see it.
251 *
252 * @param link
253 * the link
254 *
255 * @return the {@link URL}
256 *
257 * @throws MalformedURLException
0efd25e3 258 * in case of data error
13285ff8
NR
259 */
260 private URL guest(String link) throws MalformedURLException {
261 if (link.contains("?")) {
262 if (link.contains("/?")) {
263 return new URL(link.replace("?", "guest?"));
13285ff8 264 }
211f7ddb
NR
265
266 return new URL(link.replace("?", "/guest?"));
13285ff8 267 }
211f7ddb
NR
268
269 return new URL(link + "/guest");
13285ff8 270 }
a4143cd7 271}