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