Commit | Line | Data |
---|---|---|
a4143cd7 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
a4143cd7 NR |
3 | import java.io.IOException; |
4 | import java.io.InputStream; | |
13285ff8 | 5 | import java.net.MalformedURLException; |
a4143cd7 | 6 | import java.net.URL; |
ce297a79 | 7 | import java.util.AbstractMap; |
a4143cd7 | 8 | import java.util.ArrayList; |
6e06d2cc | 9 | import java.util.HashMap; |
a4143cd7 NR |
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; | |
6e06d2cc | 16 | import be.nikiroo.fanfix.bundles.Config; |
a4143cd7 | 17 | import be.nikiroo.fanfix.data.MetaData; |
16a81ef7 | 18 | import be.nikiroo.utils.Image; |
ed08c171 | 19 | import be.nikiroo.utils.Progress; |
a4143cd7 NR |
20 | import 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 | 28 | class 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) | |
d66deb8d NR |
70 | String login = Instance.getInstance().getConfig().getString(Config.LOGIN_YIFFSTAR_USER); |
71 | String password = Instance.getInstance().getConfig().getString(Config.LOGIN_YIFFSTAR_PASS); | |
13285ff8 NR |
72 | |
73 | if (login != null && !login.isEmpty() && password != null | |
74 | && !password.isEmpty()) { | |
27f3b37f | 75 | |
13285ff8 | 76 | Map<String, String> post = new HashMap<String, String>(); |
27f3b37f N |
77 | post.put("LoginForm[sfLoginUsername]", login); |
78 | post.put("LoginForm[sfLoginPassword]", password); | |
13285ff8 | 79 | post.put("YII_CSRF_TOKEN", ""); |
27f3b37f N |
80 | post.put("yt1", "Login"); |
81 | post.put("returnUrl", "/"); | |
13285ff8 NR |
82 | |
83 | // Cookies will actually be retained by the cache manager once | |
84 | // logged in | |
d66deb8d NR |
85 | Instance.getInstance().getCache() |
86 | .openNoCache(new URL("https://www.sofurry.com/user/login"), 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")) { | |
27f3b37f N |
94 | source = guest(source.toString()); |
95 | // NO CACHE because we don't want the NotLoggedIn message later | |
d66deb8d | 96 | InputStream in = Instance.getInstance().getCache().openNoCache(source, this, null, null, null); |
0ffa4754 NR |
97 | String line = getLine(in, "/browse/folder/", 0); |
98 | if (line != null) { | |
99 | String[] tab = line.split("\""); | |
100 | if (tab.length > 1) { | |
101 | String groupUrl = source.getProtocol() + "://" | |
102 | + source.getHost() + tab[1]; | |
103 | return guest(groupUrl); | |
104 | } | |
6e06d2cc | 105 | } |
a4143cd7 | 106 | } |
0ffa4754 | 107 | } catch (Exception e) { |
d66deb8d | 108 | Instance.getInstance().getTraceHandler().error(e); |
a4143cd7 NR |
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 | ||
16a81ef7 | 131 | private Image getCover(URL source, InputStream in) throws IOException { |
a4143cd7 | 132 | |
ed08c171 | 133 | List<Entry<String, URL>> chaps = getChapters(source, in, null); |
a4143cd7 | 134 | if (!chaps.isEmpty()) { |
d66deb8d | 135 | in = Instance.getInstance().getCache().open(chaps.get(0).getValue(), this, true); |
a4143cd7 NR |
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 | ||
211f7ddb | 161 | private String getAuthor(InputStream in) { |
a4143cd7 NR |
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 | ||
211f7ddb | 170 | private String getTitle(InputStream in) { |
a4143cd7 NR |
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 | |
ed08c171 NR |
188 | protected List<Entry<String, URL>> getChapters(URL source, InputStream in, |
189 | Progress pg) throws IOException { | |
a4143cd7 NR |
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 | } | |
ce297a79 NR |
205 | urls.add(new AbstractMap.SimpleEntry<String, URL>( |
206 | StringUtils.unhtml(line).trim(), guest(link))); | |
a4143cd7 NR |
207 | } |
208 | } | |
209 | } | |
210 | ||
211 | return urls; | |
212 | } | |
213 | ||
214 | @Override | |
ed08c171 NR |
215 | protected String getChapterContent(URL source, InputStream in, int number, |
216 | Progress pg) throws IOException { | |
a4143cd7 NR |
217 | StringBuilder builder = new StringBuilder(); |
218 | ||
219 | String startAt = "id=\"sfContentBody"; | |
220 | String endAt = "id=\"recommendationArea"; | |
221 | boolean ok = false; | |
222 | ||
223 | @SuppressWarnings("resource") | |
224 | Scanner scan = new Scanner(in, "UTF-8"); | |
225 | scan.useDelimiter("\\n"); | |
226 | while (scan.hasNext()) { | |
227 | String line = scan.next(); | |
228 | if (!ok && line.contains(startAt)) { | |
229 | ok = true; | |
230 | } else if (ok && line.contains(endAt)) { | |
231 | ok = false; | |
232 | break; | |
233 | } | |
234 | ||
235 | if (ok) { | |
236 | builder.append(line); | |
406447a4 | 237 | builder.append(' '); |
a4143cd7 NR |
238 | } |
239 | } | |
240 | ||
241 | return builder.toString(); | |
242 | } | |
13285ff8 NR |
243 | |
244 | /** | |
245 | * Return a {@link URL} from the given link, but add the "/guest" part to it | |
246 | * to make sure we don't need to be logged-in to see it. | |
247 | * | |
248 | * @param link | |
249 | * the link | |
250 | * | |
251 | * @return the {@link URL} | |
252 | * | |
253 | * @throws MalformedURLException | |
0efd25e3 | 254 | * in case of data error |
13285ff8 NR |
255 | */ |
256 | private URL guest(String link) throws MalformedURLException { | |
257 | if (link.contains("?")) { | |
258 | if (link.contains("/?")) { | |
259 | return new URL(link.replace("?", "guest?")); | |
13285ff8 | 260 | } |
211f7ddb NR |
261 | |
262 | return new URL(link.replace("?", "/guest?")); | |
13285ff8 | 263 | } |
211f7ddb NR |
264 | |
265 | return new URL(link + "/guest"); | |
13285ff8 | 266 | } |
a4143cd7 | 267 | } |