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
.ArrayList
;
8 import java
.util
.HashMap
;
11 import java
.util
.Map
.Entry
;
12 import java
.util
.Scanner
;
14 import be
.nikiroo
.fanfix
.Instance
;
15 import be
.nikiroo
.fanfix
.bundles
.Config
;
16 import be
.nikiroo
.fanfix
.data
.MetaData
;
17 import be
.nikiroo
.utils
.Image
;
18 import be
.nikiroo
.utils
.Progress
;
19 import be
.nikiroo
.utils
.StringUtils
;
22 * Support class for <a href="https://sofurry.com/">SoFurry.com</a>, a Furry
23 * website supporting images and stories (we only retrieve the stories).
27 class YiffStar
extends BasicSupport
{
30 public String
getSourceName() {
35 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
36 MetaData meta
= new MetaData();
38 meta
.setTitle(getTitle(reset(in
)));
39 meta
.setAuthor(getAuthor(reset(in
)));
41 meta
.setTags(getTags(reset(in
)));
42 meta
.setSource(getSourceName());
43 meta
.setUrl(source
.toString());
44 meta
.setPublisher(getSourceName());
45 meta
.setUuid(source
.toString());
48 meta
.setSubject("Furry");
49 meta
.setType(getType().toString());
50 meta
.setImageDocument(false);
51 meta
.setCover(getCover(source
, reset(in
)));
57 protected boolean supports(URL url
) {
58 String host
= url
.getHost();
59 if (host
.startsWith("www.")) {
60 host
= host
.substring("www.".length());
63 return "sofurry.com".equals(host
);
67 protected boolean isHtml() {
72 public void login() throws IOException
{
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
);
80 if (login
!= null && !login
.isEmpty() && password
!= null
81 && !password
.isEmpty()) {
82 Map
<String
, String
> post
= new HashMap
<String
, String
>();
83 post
.put("sfLoginUsername", login
);
84 post
.put("sfLoginPassword", password
);
85 post
.put("YII_CSRF_TOKEN", "");
87 // Cookies will actually be retained by the cache manager once
90 .openNoCache(new URL("https://www.sofurry.com/user/login"),
91 this, post
, null, null).close();
96 public URL
getCanonicalUrl(URL source
) throws IOException
{
97 if (source
.getPath().startsWith("/view")) {
98 source
= new URL(source
.toString() + "/guest");
99 InputStream in
= Instance
.getCache().open(source
, this, false);
100 String line
= getLine(in
, "/browse/folder/", 0);
102 String
[] tab
= line
.split("\"");
103 if (tab
.length
> 1) {
104 String groupUrl
= source
.getProtocol() + "://"
105 + source
.getHost() + tab
[1];
106 return guest(groupUrl
);
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
.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 final URL value
= guest(link
);
206 final String key
= StringUtils
.unhtml(line
).trim();
207 urls
.add(new Entry
<String
, URL
>() {
209 public URL
setValue(URL value
) {
214 public URL
getValue() {
219 public String
getKey() {
231 protected String
getChapterContent(URL source
, InputStream in
, int number
,
232 Progress pg
) throws IOException
{
233 StringBuilder builder
= new StringBuilder();
235 String startAt
= "id=\"sfContentBody";
236 String endAt
= "id=\"recommendationArea";
239 @SuppressWarnings("resource")
240 Scanner scan
= new Scanner(in
, "UTF-8");
241 scan
.useDelimiter("\\n");
242 while (scan
.hasNext()) {
243 String line
= scan
.next();
244 if (!ok
&& line
.contains(startAt
)) {
246 } else if (ok
&& line
.contains(endAt
)) {
252 builder
.append(line
);
257 return builder
.toString();
261 * Return a {@link URL} from the given link, but add the "/guest" part to it
262 * to make sure we don't need to be logged-in to see it.
267 * @return the {@link URL}
269 * @throws MalformedURLException
270 * in case of data error
272 private URL
guest(String link
) throws MalformedURLException
{
273 if (link
.contains("?")) {
274 if (link
.contains("/?")) {
275 return new URL(link
.replace("?", "guest?"));
278 return new URL(link
.replace("?", "/guest?"));
281 return new URL(link
+ "/guest");