new yiffstar, part 1
[fanfix.git] / src / be / nikiroo / fanfix / supported / YiffStar2.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Map.Entry;
12
13 import org.jsoup.helper.DataUtil;
14 import org.jsoup.nodes.Document;
15
16 import be.nikiroo.fanfix.Instance;
17 import be.nikiroo.fanfix.bundles.Config;
18 import be.nikiroo.fanfix.data.MetaData;
19 import be.nikiroo.utils.IOUtils;
20 import be.nikiroo.utils.Progress;
21
22 /**
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).
25 *
26 * @author niki
27 */
28 class YiffStar2 extends BasicSupport {
29 @Override
30 protected String getSourceName() {
31 return "YiffStar";
32 }
33
34 @Override
35 protected void login() throws IOException {
36 // Note: this is not necessary anymore for NSFW
37 // (the "/guest" trick is enough)
38 // ...but still required for RegUsersOnly pages
39 String login = Instance.getConfig().getString(
40 Config.LOGIN_YIFFSTAR_USER);
41 String password = Instance.getConfig().getString(
42 Config.LOGIN_YIFFSTAR_PASS);
43
44 if (login != null && !login.isEmpty() && password != null
45 && !password.isEmpty()) {
46 Map<String, String> post = new HashMap<String, String>();
47 post.put("YII_CSRF_TOKEN", "");
48 post.put("LoginForm[sfLoginUsername]", login);
49 post.put("LoginForm[sfLoginPassword]", password);
50 post.put("returnUrl", "/");
51 post.put("yt1", "Login");
52
53 // Cookies will actually be retained by the cache manager once
54 // logged in
55 setCurrentReferer(null);
56 Instance.getCache()
57 .openNoCache(new URL("https://www.sofurry.com/user/login"),
58 this, post, null, null).close();
59 }
60 }
61
62 @Override
63 protected Document loadDocument(URL source) throws IOException {
64 String url = getCanonicalUrl(source).toString();
65 return DataUtil
66 .load(Instance.getCache().openNoCache(source, this, null, null,
67 null), "UTF-8", url.toString());
68 }
69
70 @Override
71 protected boolean isHtml() {
72 return true;
73 }
74
75 @Override
76 protected MetaData getMeta() throws IOException {
77
78 IOUtils.writeSmallFile(new File("/tmp/node.html"), getSourceNode()
79 .outerHtml());
80
81 MetaData meta = new MetaData();
82
83 meta.setTitle("");
84 meta.setAuthor("");
85 meta.setDate("");
86 meta.setTags(new ArrayList<String>());
87 meta.setSource(getSourceName());
88 meta.setUrl(getSource().toString());
89 meta.setPublisher(getSourceName());
90 meta.setUuid(getSource().toString());
91 meta.setLuid("");
92 meta.setLang("en");
93 meta.setSubject("Furry");
94 meta.setType(getType().toString());
95 meta.setImageDocument(false);
96 meta.setCover(null);
97
98 return meta;
99 }
100
101 @Override
102 protected String getDesc() throws IOException {
103 // TODO Auto-generated method stub
104 return "";
105 }
106
107 @Override
108 protected List<Entry<String, URL>> getChapters(Progress pg)
109 throws IOException {
110 // TODO Auto-generated method stub
111 return new ArrayList<Map.Entry<String, URL>>();
112 }
113
114 @Override
115 protected String getChapterContent(URL chapUrl, int number, Progress pg)
116 throws IOException {
117 // TODO Auto-generated method stub
118 return "";
119 }
120
121 @Override
122 protected boolean supports(URL url) {
123 String host = url.getHost();
124 if (host.startsWith("www.")) {
125 host = host.substring("www.".length());
126 }
127
128 return "sofurry.com".equals(host);
129 }
130
131 /**
132 * Return a {@link URL} from the given link, but add the "/guest" part to it
133 * to make sure we don't need to be logged-in to see it.
134 *
135 * @param link
136 * the link
137 *
138 * @return the {@link URL}
139 *
140 * @throws MalformedURLException
141 * in case of data error
142 */
143 static private URL guestUrl(String link) throws MalformedURLException {
144
145 if (true)
146 return new URL(link);
147
148 if (link.contains("?")) {
149 if (link.contains("/?")) {
150 return new URL(link.replace("?", "guest?"));
151 }
152
153 return new URL(link.replace("?", "/guest?"));
154 }
155
156 return new URL(link + "/guest");
157 }
158 }