ed75b10ef4e7e290276d599f6c591f4fc78ef6e6
1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.awt
.image
.BufferedImage
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.net
.MalformedURLException
;
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
.StringUtils
;
21 * Support class for <a href="https://sofurry.com/">SoFurry.com</a>, a Furry
22 * website supporting images and stories (we only retrieve the stories).
26 class YiffStar
extends BasicSupport
{
29 public String
getSourceName() {
34 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
35 MetaData meta
= new MetaData();
37 meta
.setTitle(getTitle(reset(in
)));
38 meta
.setAuthor(getAuthor(source
, reset(in
)));
40 meta
.setTags(getTags(reset(in
)));
41 meta
.setSource(getSourceName());
42 meta
.setUrl(source
.toString());
43 meta
.setPublisher(getSourceName());
44 meta
.setUuid(source
.toString());
47 meta
.setSubject("Furry");
48 meta
.setType(getType().toString());
49 meta
.setImageDocument(false);
50 meta
.setCover(getCover(source
, reset(in
)));
56 protected boolean supports(URL url
) {
57 String host
= url
.getHost();
58 if (host
.startsWith("www.")) {
59 host
= host
.substring("www.".length());
62 return "sofurry.com".equals(host
);
66 protected boolean isHtml() {
71 public void login() throws IOException
{
72 // Note: this should not be necessary anymore
73 // (the "/guest" trick is enough)
74 String login
= Instance
.getConfig().getString(
75 Config
.LOGIN_YIFFSTAR_USER
);
76 String password
= Instance
.getConfig().getString(
77 Config
.LOGIN_YIFFSTAR_PASS
);
79 if (login
!= null && !login
.isEmpty() && password
!= null
80 && !password
.isEmpty()) {
81 Map
<String
, String
> post
= new HashMap
<String
, String
>();
82 post
.put("sfLoginUsername", login
);
83 post
.put("sfLoginPassword", password
);
84 post
.put("YII_CSRF_TOKEN", "");
86 // Cookies will actually be retained by the cache manager once
89 .openNoCache(new URL("https://www.sofurry.com/user/login"),
95 public URL
getCanonicalUrl(URL source
) throws IOException
{
96 if (source
.getPath().startsWith("/view")) {
97 source
= new URL(source
.toString() + "/guest");
98 InputStream in
= Instance
.getCache().open(source
, this, false);
99 String line
= getLine(in
, "/browse/folder/", 0);
101 String
[] tab
= line
.split("\"");
102 if (tab
.length
> 1) {
103 String groupUrl
= source
.getProtocol() + "://"
104 + source
.getHost() + tab
[1];
105 return guest(groupUrl
);
110 return super.getCanonicalUrl(source
);
113 private List
<String
> getTags(InputStream in
) {
114 List
<String
> tags
= new ArrayList
<String
>();
116 String line
= getLine(in
, "class=\"sf-story-big-tags", 0);
118 String
[] tab
= StringUtils
.unhtml(line
).split(",");
119 for (String possibleTag
: tab
) {
120 String tag
= possibleTag
.trim();
121 if (!tag
.isEmpty() && !tag
.equals("...") && !tags
.contains(tag
)) {
130 private BufferedImage
getCover(URL source
, InputStream in
)
133 List
<Entry
<String
, URL
>> chaps
= getChapters(source
, in
);
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(URL source
, InputStream in
) throws IOException
{
162 String author
= getLine(in
, "class=\"onlinestatus", 0);
163 if (author
!= null) {
164 return StringUtils
.unhtml(author
).trim();
170 private String
getTitle(InputStream in
) throws IOException
{
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
)
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
>() {
208 public URL
setValue(URL value
) {
212 public URL
getValue() {
216 public String
getKey() {
228 protected String
getChapterContent(URL source
, InputStream in
, int number
)
230 StringBuilder builder
= new StringBuilder();
232 String startAt
= "id=\"sfContentBody";
233 String endAt
= "id=\"recommendationArea";
236 @SuppressWarnings("resource")
237 Scanner scan
= new Scanner(in
, "UTF-8");
238 scan
.useDelimiter("\\n");
239 while (scan
.hasNext()) {
240 String line
= scan
.next();
241 if (!ok
&& line
.contains(startAt
)) {
243 } else if (ok
&& line
.contains(endAt
)) {
249 builder
.append(line
);
254 return builder
.toString();
258 * Return a {@link URL} from the given link, but add the "/guest" part to it
259 * to make sure we don't need to be logged-in to see it.
264 * @return the {@link URL}
266 * @throws MalformedURLException
268 private URL
guest(String link
) throws MalformedURLException
{
269 if (link
.contains("?")) {
270 if (link
.contains("/?")) {
271 return new URL(link
.replace("?", "guest?"));
273 return new URL(link
.replace("?", "/guest?"));
276 return new URL(link
+ "/guest");