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