4f4e8d693271814bb645eb9c9a6ae3b81b1b5f7e
1 package be
.nikiroo
.fanfix
.supported
;
3 import java
.awt
.image
.BufferedImage
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
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
.StringUtils
;
20 * Support class for <a href="https://sofurry.com/">SoFurry.com</a>, a Furry
21 * website supporting images and stories (we only retrieve the stories).
25 class YiffStar
extends BasicSupport
{
28 public String
getSourceName() {
33 protected MetaData
getMeta(URL source
, InputStream in
) throws IOException
{
34 MetaData meta
= new MetaData();
36 meta
.setTitle(getTitle(reset(in
)));
37 meta
.setAuthor(getAuthor(source
, reset(in
)));
39 meta
.setTags(getTags(reset(in
)));
40 meta
.setSource(getSourceName());
41 meta
.setUrl(source
.toString());
42 meta
.setPublisher(getSourceName());
43 meta
.setUuid(source
.toString());
46 meta
.setSubject("Furry");
47 meta
.setType(getType().toString());
48 meta
.setImageDocument(false);
49 meta
.setCover(getCover(source
, reset(in
)));
55 protected boolean supports(URL url
) {
56 String host
= url
.getHost();
57 if (host
.startsWith("www.")) {
58 host
= host
.substring("www.".length());
61 return "sofurry.com".equals(host
);
65 protected boolean isHtml() {
70 public void login() throws IOException
{
71 Map
<String
, String
> post
= new HashMap
<String
, String
>();
72 post
.put("LoginForm[sfLoginUsername]",
73 Instance
.getConfig().getString(Config
.LOGIN_YIFFSTAR_USER
));
74 post
.put("LoginForm[sfLoginPassword]",
75 Instance
.getConfig().getString(Config
.LOGIN_YIFFSTAR_PASS
));
76 post
.put("YII_CSRF_TOKEN", "");
78 // Cookies will actually be retained by the cache manager once logged in
79 // TODO: not working yet, once fixed can be removed (adding "/guest" to
80 // URLs fix the access problem!):
82 * Instance.getCache() .openNoCache(new
83 * URL("https://www.sofurry.com/user/login"), this, post).close();
88 public URL
getCanonicalUrl(URL source
) throws IOException
{
89 if (source
.getPath().startsWith("/view")) {
90 InputStream in
= Instance
.getCache().open(source
, this, false);
91 String line
= getLine(in
, "/browse/folder/", 0);
93 String
[] tab
= line
.split("\"");
95 String groupUrl
= source
.getProtocol() + "://"
96 + source
.getHost() + tab
[1];
97 return new URL(groupUrl
);
102 return super.getCanonicalUrl(source
);
105 private List
<String
> getTags(InputStream in
) {
106 List
<String
> tags
= new ArrayList
<String
>();
108 String line
= getLine(in
, "class=\"sf-story-big-tags", 0);
110 String
[] tab
= StringUtils
.unhtml(line
).split(",");
111 for (String possibleTag
: tab
) {
112 String tag
= possibleTag
.trim();
113 if (!tag
.isEmpty() && !tag
.equals("...") && !tags
.contains(tag
)) {
122 private BufferedImage
getCover(URL source
, InputStream in
)
125 List
<Entry
<String
, URL
>> chaps
= getChapters(source
, in
);
126 if (!chaps
.isEmpty()) {
127 in
= Instance
.getCache().open(chaps
.get(0).getValue(), this, true);
128 String line
= getLine(in
, " name=\"og:image\"", 0);
131 for (int i
= 0; i
< 3; i
++) {
132 pos
= line
.indexOf('"', pos
+ 1);
136 line
= line
.substring(pos
+ 1);
137 pos
= line
.indexOf('"');
139 line
= line
.substring(0, pos
);
140 if (line
.contains("/thumb?")) {
141 line
= line
.replace("/thumb?",
142 "/auxiliaryContent?type=25&");
143 return getImage(this, null, line
);
153 private String
getAuthor(URL source
, InputStream in
) throws IOException
{
154 String author
= getLine(in
, "class=\"onlinestatus", 0);
155 if (author
!= null) {
156 return StringUtils
.unhtml(author
).trim();
162 private String
getTitle(InputStream in
) throws IOException
{
163 String title
= getLine(in
, "class=\"sflabel pagetitle", 0);
165 if (title
.contains("(series)")) {
166 title
= title
.replace("(series)", "");
168 return StringUtils
.unhtml(title
).trim();
175 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
176 return null; // TODO: no description at all? Cannot find one...
180 protected List
<Entry
<String
, URL
>> getChapters(URL source
, InputStream in
)
182 List
<Entry
<String
, URL
>> urls
= new ArrayList
<Entry
<String
, URL
>>();
184 @SuppressWarnings("resource")
185 Scanner scan
= new Scanner(in
, "UTF-8");
186 scan
.useDelimiter("\\n");
187 while (scan
.hasNext()) {
188 String line
= scan
.next();
189 if (line
.contains("\"/view/") && line
.contains("title=")) {
190 String
[] tab
= line
.split("\"");
191 if (tab
.length
> 5) {
192 String link
= tab
[5];
193 if (link
.startsWith("/")) {
194 link
= source
.getProtocol() + "://" + source
.getHost()
197 final URL value
= new URL(link
);
198 final String key
= StringUtils
.unhtml(line
).trim();
199 urls
.add(new Entry
<String
, URL
>() {
200 public URL
setValue(URL value
) {
204 public URL
getValue() {
208 public String
getKey() {
220 protected String
getChapterContent(URL source
, InputStream in
, int number
)
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
);
242 builder
.append('\n');
246 return builder
.toString();