/src/jd/plugins/hoster/UlozTo.java

https://bitbucket.org/jorgenio/jdownloader · Java · 263 lines · 210 code · 29 blank · 24 comment · 57 complexity · 23c155a0a01ec5f56c12f2c5bfc4af79 MD5 · raw file

  1. // jDownloader - Downloadmanager
  2. // Copyright (C) 2012 JD-Team support@jdownloader.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package jd.plugins.hoster;
  17. import java.io.IOException;
  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;
  20. import jd.PluginWrapper;
  21. import jd.config.ConfigContainer;
  22. import jd.config.ConfigEntry;
  23. import jd.http.Browser;
  24. import jd.http.URLConnectionAdapter;
  25. import jd.nutils.encoding.Encoding;
  26. import jd.parser.Regex;
  27. import jd.parser.html.Form;
  28. import jd.plugins.Account;
  29. import jd.plugins.AccountInfo;
  30. import jd.plugins.DownloadLink;
  31. import jd.plugins.DownloadLink.AvailableStatus;
  32. import jd.plugins.HostPlugin;
  33. import jd.plugins.LinkStatus;
  34. import jd.plugins.PluginException;
  35. import jd.plugins.PluginForHost;
  36. import jd.utils.locale.JDL;
  37. import org.appwork.utils.formatter.SizeFormatter;
  38. @HostPlugin(revision = "$Revision: 15976 $", interfaceVersion = 2, names = { "uloz.to" }, urls = { "http://(www\\.)?((uloz\\.to|ulozto\\.sk|ulozto\\.cz|ulozto\\.net)/[a-zA-Z0-9]+/.+|bagruj\\.cz/[a-z0-9]{12}/.*?\\.html)" }, flags = { 2 })
  39. public class UlozTo extends PluginForHost {
  40. private static final String REPEAT_CAPTCHA = "REPEAT_CAPTCHA";
  41. private static final String CAPTCHA_TEXT = "CAPTCHA_TEXT";
  42. private static final String CAPTCHA_ID = "CAPTCHA_ID";
  43. public UlozTo(PluginWrapper wrapper) {
  44. super(wrapper);
  45. this.setConfigElements();
  46. this.enablePremium("http://uloz.to/kredit/");
  47. }
  48. public void correctDownloadLink(DownloadLink link) {
  49. link.setUrlDownload(link.getDownloadURL().replaceAll("(uloz\\.to|ulozto\\.sk|ulozto\\.cz|ulozto\\.net)", "uloz.to"));
  50. }
  51. @Override
  52. public String getAGBLink() {
  53. return "http://img.uloz.to/terms.pdf";
  54. }
  55. @Override
  56. public int getMaxSimultanFreeDownloadNum() {
  57. return -1;
  58. }
  59. @Override
  60. public int getMaxSimultanPremiumDownloadNum() {
  61. return -1;
  62. }
  63. @Override
  64. public AvailableStatus requestFileInformation(DownloadLink downloadLink) throws IOException, InterruptedException, PluginException {
  65. this.setBrowserExclusive();
  66. br.setCustomCharset("utf-8");
  67. br.setFollowRedirects(false);
  68. handleDownloadUrl(downloadLink);
  69. // not sure if this is still needed with 2012/02/01 changes
  70. String continuePage = br.getRegex("<p><a href=\"(http://.*?)\">Please click here to continue</a>").getMatch(0);
  71. if (continuePage != null) {
  72. downloadLink.setUrlDownload(continuePage);
  73. br.getPage(downloadLink.getDownloadURL());
  74. }
  75. // Wrong links show the mainpage so here we check if we got the mainpage
  76. // or not
  77. if (br.containsHTML("(multipart/form\\-data|Chybka 404 \\- požadovaná stránka nebyla nalezena<br>)")) throw new PluginException(LinkStatus.ERROR_FILE_NOT_FOUND);
  78. String filename = br.getRegex("<title>(.*?) \\| Ulož\\.to</title>").getMatch(0);
  79. if (filename == null) filename = br.getRegex("<a href=\"#download\" class=\"jsShowDownload\">(.*?)</a>").getMatch(0);
  80. String filesize = br.getRegex("<span id=\"fileSize\">.*?\\|(.*?)</span>").getMatch(0);
  81. if (filesize == null) filesize = br.getRegex("<span id=\"fileSize\">(.*?)</span>").getMatch(0);
  82. if (filesize == null) filesize = br.getRegex("(?i)([\\d\\.]+ ?(MB|GB))").getMatch(0);
  83. if (filename == null) throw new PluginException(LinkStatus.ERROR_FILE_NOT_FOUND);
  84. downloadLink.setFinalFileName(Encoding.htmlDecode(filename.trim()));
  85. if (filesize != null) downloadLink.setDownloadSize(SizeFormatter.getSize(filesize));
  86. return AvailableStatus.TRUE;
  87. }
  88. private void handleDownloadUrl(DownloadLink downloadLink) throws IOException {
  89. br.getPage(downloadLink.getDownloadURL());
  90. if (downloadLink.getDownloadURL().matches(".*?bagruj\\.cz/[a-z0-9]{12}.*?") && br.getRedirectLocation() != null) {
  91. downloadLink.setUrlDownload(br.getRedirectLocation());
  92. br.getPage(downloadLink.getDownloadURL());
  93. } else if (br.getRedirectLocation() != null) {
  94. logger.info("Getting redirect-page");
  95. br.getPage(br.getRedirectLocation());
  96. }
  97. }
  98. @Override
  99. public void handleFree(DownloadLink downloadLink) throws Exception {
  100. requestFileInformation(downloadLink);
  101. String dllink = null;
  102. Browser br2 = br.cloneBrowser();
  103. br2.setFollowRedirects(true);
  104. boolean failed = true;
  105. for (int i = 0; i <= 5; i++) {
  106. String captchaUrl = br.getRegex(Pattern.compile("\"(http://img\\.uloz\\.to/captcha/\\d+\\.png)\"")).getMatch(0);
  107. Form captchaForm = br.getFormbyProperty("id", "frm-downloadDialog-freeDownloadForm");
  108. if (captchaForm == null || captchaUrl == null) throw new PluginException(LinkStatus.ERROR_PLUGIN_DEFECT);
  109. String key = null, code = null;
  110. // Tries to read if property selected
  111. if (getPluginConfig().getBooleanProperty(REPEAT_CAPTCHA)) {
  112. key = getPluginConfig().getStringProperty(CAPTCHA_ID);
  113. code = getPluginConfig().getStringProperty(CAPTCHA_TEXT);
  114. }
  115. // If property not selected or read failed (no data), asks to solve
  116. if (key == null || code == null) {
  117. code = getCaptchaCode(captchaUrl, downloadLink);
  118. Matcher m = Pattern.compile("http://img\\.uloz\\.to/captcha/(\\d+)\\.png").matcher(captchaUrl);
  119. if (m.find()) {
  120. key = m.group(1);
  121. getPluginConfig().setProperty(CAPTCHA_ID, key);
  122. getPluginConfig().setProperty(CAPTCHA_TEXT, code);
  123. }
  124. }
  125. // if something failed
  126. if (key == null || code == null) throw new PluginException(LinkStatus.ERROR_PLUGIN_DEFECT);
  127. captchaForm.put("captcha%5Bid%5D", key);
  128. captchaForm.put("captcha%5Btext%5D", code);
  129. captchaForm.remove(null);
  130. br.submitForm(captchaForm);
  131. // If captcha fails, throws exception
  132. // If in automatic mode, clears saved data
  133. if (br.containsHTML("Text je opsán špatně")) {
  134. if (getPluginConfig().getBooleanProperty(REPEAT_CAPTCHA)) {
  135. getPluginConfig().setProperty(CAPTCHA_ID, null);
  136. getPluginConfig().setProperty(CAPTCHA_TEXT, null);
  137. }
  138. throw new PluginException(LinkStatus.ERROR_CAPTCHA);
  139. }
  140. dllink = br.getRedirectLocation();
  141. if (dllink == null) break;
  142. URLConnectionAdapter con = null;
  143. try {
  144. br2.setDebug(true);
  145. con = br2.openGetConnection(dllink);
  146. if (!con.getContentType().contains("html")) {
  147. failed = false;
  148. break;
  149. } else {
  150. br2.followConnection();
  151. if (br2.containsHTML("Stránka nenalezena")) throw new PluginException(LinkStatus.ERROR_FILE_NOT_FOUND);
  152. br.clearCookies("http://www.uloz.to/");
  153. handleDownloadUrl(downloadLink);
  154. continue;
  155. }
  156. } finally {
  157. try {
  158. con.disconnect();
  159. } catch (Throwable e) {
  160. }
  161. }
  162. }
  163. if (dllink == null) throw new PluginException(LinkStatus.ERROR_PLUGIN_DEFECT);
  164. if (dllink.contains("/error404/?fid=file_not_found")) {
  165. logger.info("The user entered the correct captcha but this file is offline...");
  166. throw new PluginException(LinkStatus.ERROR_FILE_NOT_FOUND);
  167. }
  168. if (failed) throw new PluginException(LinkStatus.ERROR_CAPTCHA);
  169. br.setDebug(true);
  170. dl = jd.plugins.BrowserAdapter.openDownload(br, downloadLink, dllink, true, 1);
  171. if (dl.getConnection().getContentType().contains("html")) {
  172. logger.warning("The finallink doesn't seem to be a file..." + dllink);
  173. br.followConnection();
  174. throw new PluginException(LinkStatus.ERROR_PLUGIN_DEFECT);
  175. }
  176. dl.startDownload();
  177. }
  178. public void handlePremium(DownloadLink parameter, Account account) throws Exception {
  179. requestFileInformation(parameter);
  180. login(account);
  181. br.getPage(parameter.getDownloadURL());
  182. String dllink = br.getRegex("<div class=\"downloadForm\"><form action=\"(/[^<>\"\\']+)\"").getMatch(0);
  183. if (dllink == null) dllink = br.getRegex("\"(/\\d+/[^<>\"\\']+\\?do=downloadDialog\\-downloadForm\\-submit)\"").getMatch(0);
  184. if (dllink == null) throw new PluginException(LinkStatus.ERROR_PLUGIN_DEFECT);
  185. dllink = Encoding.htmlDecode(dllink);
  186. dl = jd.plugins.BrowserAdapter.openDownload(br, parameter, "http://uloz.to" + dllink, "download=St%C3%A1hnout", true, 0);
  187. if (dl.getConnection().getContentType().contains("html")) {
  188. br.followConnection();
  189. throw new PluginException(LinkStatus.ERROR_PLUGIN_DEFECT);
  190. }
  191. dl.startDownload();
  192. }
  193. // do not add @Override here to keep 0.* compatibility
  194. public boolean hasCaptcha() {
  195. return true;
  196. }
  197. public void login(Account account) throws Exception {
  198. setBrowserExclusive();
  199. br.setFollowRedirects(true);
  200. br.setCustomCharset("utf-8");
  201. br.getPage("http://uloz.to/?do=web-login");
  202. final String key = new Regex(br.getURL(), "uloz\\.to/login\\?key=([a-z0-9]+)").getMatch(0);
  203. if (key == null) throw new PluginException(LinkStatus.ERROR_PREMIUM, PluginException.VALUE_ID_PREMIUM_DISABLE);
  204. br.postPage("http://uloz.to/login?key=" + key + "&do=loginForm-submit", "username=" + Encoding.urlEncode(account.getUser()) + "&password=" + Encoding.urlEncode(account.getPass()) + "&remember=on&login=P%C5%99ihl%C3%A1sit");
  205. if (br.getCookie("http://uloz.to/", "permanentLogin") == null) throw new PluginException(LinkStatus.ERROR_PREMIUM, PluginException.VALUE_ID_PREMIUM_DISABLE);
  206. }
  207. @Override
  208. public AccountInfo fetchAccountInfo(Account account) throws Exception {
  209. AccountInfo ai = new AccountInfo();
  210. try {
  211. login(account);
  212. } catch (PluginException e) {
  213. account.setValid(false);
  214. return ai;
  215. }
  216. String trafficleft = br.getRegex("href=\"/kredit/\" title=\"([0-9\\.]+ GB)").getMatch(0);
  217. if (trafficleft != null) ai.setTrafficLeft(SizeFormatter.getSize(trafficleft));
  218. ai.setStatus("Premium User");
  219. account.setValid(true);
  220. return ai;
  221. }
  222. @Override
  223. public void reset() {
  224. }
  225. @Override
  226. public void resetDownloadlink(DownloadLink link) {
  227. }
  228. @Override
  229. public void resetPluginGlobals() {
  230. }
  231. private void setConfigElements() {
  232. getConfig().addEntry(new ConfigEntry(ConfigContainer.TYPE_CHECKBOX, this.getPluginConfig(), UlozTo.REPEAT_CAPTCHA, JDL.L("plugins.hoster.uloz.to.captchas", "Solve captcha by replaying previous (disable to solve manually)")).setDefaultValue(true));
  233. }
  234. }