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
{
31 public String
getSourceName() {
36 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
37 MetaData meta
= new MetaData();
39 meta
.setTitle(getTitle(reset(in
)));
40 meta
.setAuthor(getAuthor(reset(in
)));
42 meta
.setTags(getTags(reset(in
)));
43 meta
.setSource(getSourceName());
44 meta
.setUrl(source
.toString());
45 meta
.setPublisher(getSourceName());
46 meta
.setUuid(source
.toString());
49 meta
.setSubject("Furry");
50 meta
.setType(getType().toString());
51 meta
.setImageDocument(false);
52 meta
.setCover(getCover(source
, reset(in
)));
58 protected boolean supports(URL url
) {
59 String host
= url
.getHost();
60 if (host
.startsWith("www.")) {
61 host
= host
.substring("www.".length());
64 return "sofurry.com".equals(host
);
68 protected boolean isHtml() {
73 public void login() throws IOException
{
74 // Note: this should not be necessary anymore
75 // (the "/guest" trick is enough)
76 String login
= Instance
.getConfig().getString(
77 Config
.LOGIN_YIFFSTAR_USER
);
78 String password
= Instance
.getConfig().getString(
79 Config
.LOGIN_YIFFSTAR_PASS
);
81 if (login
!= null && !login
.isEmpty() && password
!= null
82 && !password
.isEmpty()) {
83 Map
<String
, String
> post
= new HashMap
<String
, String
>();
84 post
.put("sfLoginUsername", login
);
85 post
.put("sfLoginPassword", password
);
86 post
.put("YII_CSRF_TOKEN", "");
88 // Cookies will actually be retained by the cache manager once
91 .openNoCache(new URL("https://www.sofurry.com/user/login"),
92 this, post
, null, null).close();
97 public URL
getCanonicalUrl(URL source
) {
99 if (source
.getPath().startsWith("/view")) {
100 source
= new URL(source
.toString() + "/guest");
101 InputStream in
= Instance
.getCache().open(source
, this, false);
102 String line
= getLine(in
, "/browse/folder/", 0);
104 String
[] tab
= line
.split("\"");
105 if (tab
.length
> 1) {
106 String groupUrl
= source
.getProtocol() + "://"
107 + source
.getHost() + tab
[1];
108 return guest(groupUrl
);
112 } catch (Exception e
) {
113 Instance
.getTraceHandler().error(e
);
116 return super.getCanonicalUrl(source
);
119 private List
<String
> getTags(InputStream in
) {
120 List
<String
> tags
= new ArrayList
<String
>();
122 String line
= getLine(in
, "class=\"sf-story-big-tags", 0);
124 String
[] tab
= StringUtils
.unhtml(line
).split(",");
125 for (String possibleTag
: tab
) {
126 String tag
= possibleTag
.trim();
127 if (!tag
.isEmpty() && !tag
.equals("...") && !tags
.contains(tag
)) {
136 private Image
getCover(URL source
, InputStream in
) throws IOException
{
138 List
<Entry
<String
, URL
>> chaps
= getChapters(source
, in
, null);
139 if (!chaps
.isEmpty()) {
140 in
= Instance
.getCache().open(chaps
.get(0).getValue(), this, true);
141 String line
= getLine(in
, " name=\"og:image\"", 0);
144 for (int i
= 0; i
< 3; i
++) {
145 pos
= line
.indexOf('"', pos
+ 1);
149 line
= line
.substring(pos
+ 1);
150 pos
= line
.indexOf('"');
152 line
= line
.substring(0, pos
);
153 if (line
.contains("/thumb?")) {
154 line
= line
.replace("/thumb?",
155 "/auxiliaryContent?type=25&");
156 return getImage(this, null, line
);
166 private String
getAuthor(InputStream in
) {
167 String author
= getLine(in
, "class=\"onlinestatus", 0);
168 if (author
!= null) {
169 return StringUtils
.unhtml(author
).trim();
175 private String
getTitle(InputStream in
) {
176 String title
= getLine(in
, "class=\"sflabel pagetitle", 0);
178 if (title
.contains("(series)")) {
179 title
= title
.replace("(series)", "");
181 return StringUtils
.unhtml(title
).trim();
188 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
189 return null; // TODO: no description at all? Cannot find one...
193 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
,
194 Progress pg
) throws IOException
{
195 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
197 @SuppressWarnings("resource")
198 Scanner scan
= new Scanner(in
, "UTF-8");
199 scan
.useDelimiter("\\n");
200 while (scan
.hasNext()) {
201 String line
= scan
.next();
202 if (line
.contains("\"/view/") && line
.contains("title=")) {
203 String
[] tab
= line
.split("\"");
204 if (tab
.length
> 5) {
205 String link
= tab
[5];
206 if (link
.startsWith("/")) {
207 link
= source
.getProtocol() + "://" + source
.getHost()
210 urls
.add(new AbstractMap
.SimpleEntry
<String
, URL
>(
211 StringUtils
.unhtml(line
).trim(), guest(link
)));
220 protected String
getChapterContent(URL source
, InputStream in
, int number
,
221 Progress pg
) throws IOException
{
222 StringBuilder builder
= new StringBuilder();
224 String startAt
= "id=\"sfContentBody";
225 String endAt
= "id=\"recommendationArea";
228 @SuppressWarnings("resource")
229 Scanner scan
= new Scanner(in
, "UTF-8");
230 scan
.useDelimiter("\\n");
231 while (scan
.hasNext()) {
232 String line
= scan
.next();
233 if (!ok
&& line
.contains(startAt
)) {
235 } else if (ok
&& line
.contains(endAt
)) {
241 builder
.append(line
);
246 return builder
.toString();
250 * Return a {@link URL} from the given link, but add the "/guest" part to it
251 * to make sure we don't need to be logged-in to see it.
256 * @return the {@link URL}
258 * @throws MalformedURLException
259 * in case of data error
261 private URL
guest(String link
) throws MalformedURLException
{
262 if (link
.contains("?")) {
263 if (link
.contains("/?")) {
264 return new URL(link
.replace("?", "guest?"));
267 return new URL(link
.replace("?", "/guest?"));
270 return new URL(link
+ "/guest");