1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.net
.MalformedURLException
;
7 import java
.util
.AbstractMap
;
8 import java
.util
.ArrayList
;
9 import java
.util
.HashMap
;
10 import java
.util
.List
;
12 import java
.util
.Map
.Entry
;
13 import java
.util
.Scanner
;
15 import be
.nikiroo
.fanfix
.Instance
;
16 import be
.nikiroo
.fanfix
.bundles
.Config
;
17 import be
.nikiroo
.fanfix
.data
.MetaData
;
18 import be
.nikiroo
.utils
.Image
;
19 import be
.nikiroo
.utils
.Progress
;
20 import be
.nikiroo
.utils
.StringUtils
;
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).
28 class YiffStar
extends BasicSupport_Deprecated
{
30 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
31 MetaData meta
= new MetaData();
33 meta
.setTitle(getTitle(reset(in
)));
34 meta
.setAuthor(getAuthor(reset(in
)));
36 meta
.setTags(getTags(reset(in
)));
37 meta
.setSource(getType().getSourceName());
38 meta
.setUrl(source
.toString());
39 meta
.setPublisher(getType().getSourceName());
40 meta
.setUuid(source
.toString());
43 meta
.setSubject("Furry");
44 meta
.setType(getType().toString());
45 meta
.setImageDocument(false);
46 meta
.setCover(getCover(source
, reset(in
)));
52 protected boolean supports(URL url
) {
53 String host
= url
.getHost();
54 if (host
.startsWith("www.")) {
55 host
= host
.substring("www.".length());
58 return "sofurry.com".equals(host
);
62 protected boolean isHtml() {
67 public void login() throws IOException
{
68 // Note: this should not be necessary anymore
69 // (the "/guest" trick is enough)
70 String login
= Instance
.getInstance().getConfig().getString(Config
.LOGIN_YIFFSTAR_USER
);
71 String password
= Instance
.getInstance().getConfig().getString(Config
.LOGIN_YIFFSTAR_PASS
);
73 if (login
!= null && !login
.isEmpty() && password
!= null
74 && !password
.isEmpty()) {
76 Map
<String
, String
> post
= new HashMap
<String
, String
>();
77 post
.put("LoginForm[sfLoginUsername]", login
);
78 post
.put("LoginForm[sfLoginPassword]", password
);
79 post
.put("YII_CSRF_TOKEN", "");
80 post
.put("yt1", "Login");
81 post
.put("returnUrl", "/");
83 // Cookies will actually be retained by the cache manager once
85 Instance
.getInstance().getCache()
86 .openNoCache(new URL("https://www.sofurry.com/user/login"), this, post
, null, null).close();
91 public URL
getCanonicalUrl(URL source
) {
93 if (source
.getPath().startsWith("/view")) {
94 source
= guest(source
.toString());
95 // NO CACHE because we don't want the NotLoggedIn message later
96 InputStream in
= Instance
.getInstance().getCache().openNoCache(source
, this, null, null, null);
97 String line
= getLine(in
, "/browse/folder/", 0);
99 String
[] tab
= line
.split("\"");
100 if (tab
.length
> 1) {
101 String groupUrl
= source
.getProtocol() + "://"
102 + source
.getHost() + tab
[1];
103 return guest(groupUrl
);
107 } catch (Exception e
) {
108 Instance
.getInstance().getTraceHandler().error(e
);
111 return super.getCanonicalUrl(source
);
114 private List
<String
> getTags(InputStream in
) {
115 List
<String
> tags
= new ArrayList
<String
>();
117 String line
= getLine(in
, "class=\"sf-story-big-tags", 0);
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
)) {
131 private Image
getCover(URL source
, InputStream in
) throws IOException
{
133 List
<Entry
<String
, URL
>> chaps
= getChapters(source
, in
, null);
134 if (!chaps
.isEmpty()) {
135 in
= Instance
.getInstance().getCache().open(chaps
.get(0).getValue(), this, true);
136 String line
= getLine(in
, " name=\"og:image\"", 0);
139 for (int i
= 0; i
< 3; i
++) {
140 pos
= line
.indexOf('"', pos
+ 1);
144 line
= line
.substring(pos
+ 1);
145 pos
= line
.indexOf('"');
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
);
161 private String
getAuthor(InputStream in
) {
162 String author
= getLine(in
, "class=\"onlinestatus", 0);
163 if (author
!= null) {
164 return StringUtils
.unhtml(author
).trim();
170 private String
getTitle(InputStream in
) {
171 String title
= getLine(in
, "class=\"sflabel pagetitle", 0);
173 if (title
.contains("(series)")) {
174 title
= title
.replace("(series)", "");
176 return StringUtils
.unhtml(title
).trim();
183 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
184 return null; // TODO: no description at all? Cannot find one...
188 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
189 Progress pg
) throws IOException
{
190 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
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()
205 urls
.add(new AbstractMap
.SimpleEntry
<String
, URL
>(
206 StringUtils
.unhtml(line
).trim(), guest(link
)));
215 protected String
getChapterContent(URL source
, InputStream in
, int number
,
216 Progress pg
) throws IOException
{
217 StringBuilder builder
= new StringBuilder();
219 String startAt
= "id=\"sfContentBody";
220 String endAt
= "id=\"recommendationArea";
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
)) {
230 } else if (ok
&& line
.contains(endAt
)) {
236 builder
.append(line
);
241 return builder
.toString();
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.
251 * @return the {@link URL}
253 * @throws MalformedURLException
254 * in case of data error
256 private URL
guest(String link
) throws MalformedURLException
{
257 if (link
.contains("?")) {
258 if (link
.contains("/?")) {
259 return new URL(link
.replace("?", "guest?"));
262 return new URL(link
.replace("?", "/guest?"));
265 return new URL(link
+ "/guest");