# -*- coding: utf-8 -*-

import re

from ..internal.SimpleHoster import SimpleHoster


class RockfileEu(SimpleHoster):
    __name__ = "RockfileEu"
    __type__ = "hoster"
    __version__ = "0.04"
    __status__ = "testing"

    __pattern__ = r'https?://(?:www\.)?rockfile\.eu/\w{12}.html'
    __config__ = [("activated", "bool", "Activated", True),
                  ("use_premium", "bool", "Use premium account if available", True),
                  ("fallback", "bool", "Fallback to free download if premium fails", True),
                  ("chk_filesize", "bool", "Check file size", True),
                  ("max_wait", "int", "Reconnect if waiting time is greater than minutes", 10)]

    __description__ = """Rockfile.eu hoster plugin"""
    __license__ = "GPLv3"
    __authors__ = [("GammaC0de", "nitzo2001[AT]yahoo[DOT]com")]

    NAME_PATTERN = r'name="fname" value="(?P<N>.+?)"'
    SIZE_PATTERN = r'var iniFileSize = (\d+)'

    WAIT_PATTERN = r'<span id="countdown_str".+?>(\d+)</span>'
    DL_LIMIT_PATTERN = r'You have to wait (?:<b>)?(.+?)(?:</b>)? until you can start another download'

    TEMP_OFFLINE_PATTERN = "Connection limit reached|Server error"

    LINK_FREE_PATTERN = r'href="(http://.+?\.rfservers\.eu.+?)"'

    COOKIES = [("rockfile.eu", "lang", "english")]

    def setup(self):
        self.multiDL = True
        self.chunk_limit = 1
        self.resume_download = True

    def handle_free(self, pyfile):
        url, inputs = self.parse_html_form("action=''")

        if not inputs:
            self.error("Free download form not found")

        self.data = self.load(pyfile.url, post=inputs)

        for _i in range(5):
            self.check_errors()

            url, inputs = self.parse_html_form('name="F1"')
            if not inputs:
                self.error("Form F1 not found")

            captcha_code = "".join(chr(int(_x[2:4])) if _x[0:2] == '&#' else _x for _p, _x in
                                   sorted(re.findall(r'<span style=[\'"]color:#5d5d5d; text-shadow: 1px 1px #f2f2f2;.+?padding-left:(\d+)px;.+?[\'"]>(.+?)</span>', self.data),
                                          key=lambda _i: int(_i[0])))

            if not captcha_code:
                self.error("Captcha not found")

            captcha_code = captcha_code[1:] if captcha_code[
                0] == '0' else captcha_code  #: Remove leading zero
            captcha_code = captcha_code[1:] if captcha_code[
                0] == '0' else captcha_code  #: Remove leading zero

            inputs['code'] = captcha_code

            self.data = self.load(pyfile.url, post=inputs)

            if r'>Wrong captcha<' in self.data:
                self.log_warning(_("Invalid captcha"), captcha_code)

            else:
                break

        else:
            self.fail(_("Max captcha retries reached"))

        m = re.search(self.LINK_FREE_PATTERN, self.data)
        if m is not None:
            self.link = m.group(1)

@@ -29,16 +29,58 @@
 ort re%0A%0A
+from ..captcha.ReCaptcha import ReCaptcha%0A
 from ..i
@@ -228,17 +228,17 @@
 _ = %220.0
-4
+5
 %22%0A    __
@@ -1672,40 +1672,8 @@
 s)%0A%0A
-        for _i in range(5):%0A    
     
@@ -1697,20 +1697,16 @@
 rors()%0A%0A
-    
         
@@ -1753,20 +1753,16 @@
 =%22F1%22')%0A
-    
         
@@ -1780,36 +1780,32 @@
 ts:%0A            
-    
 self.error(%22Form
@@ -1833,338 +1833,87 @@
     
-    
+self.
 captcha
-_code = %22%22.join(chr(int(_x%5B2:4%5D)) if _x%5B0:2%5D == '&#' else _x for _p, _x in%0A                                   sorted(re.findall(r'%3Cspan style=%5B%5C'%22%5Dcolor:#5d5d5d; text-shadow: 1px 1px #f2f2f2;.+?padding-left:(%5Cd+)px;.+?%5B%5C'%22%5D%3E(.+?)%3C/span%3E', self.data),%0A                                          key=lambda _i: int(_i%5B0%5D))
+ = ReCaptcha(pyfile)%0A        captcha_key = self.captcha.detect_key(
 )%0A%0A 
@@ -1919,26 +1919,18 @@
         
-  
-  if not
+if
  captcha
@@ -1930,20 +1930,19 @@
 captcha_
-code
+key
 :%0A      
@@ -1951,100 +1951,61 @@
     
-    self.error(%22Captcha not found%22)%0A%0A            captcha_code = captcha_code%5B1:%5D if 
+response, challenge = self.captcha.challenge(
 captcha_
 code
@@ -1992,37 +1992,36 @@
 allenge(captcha_
-code%5B
+key)
 %0A               
@@ -2021,241 +2021,174 @@
     
-    0%5D == '0' else captcha_code  #: Remove leading zero%0A            captcha_code = captcha_code%5B1:%5D if captcha_code%5B%0A                0%5D == '0' else captcha_code  #: Remove leading zero%0A%0A            inputs%5B'code'%5D = captcha_code%0A%0A    
+inputs%5B'recaptcha_challenge_field'%5D = challenge%0A            inputs%5B'recaptcha_response_field'%5D = response%0A%0A        else:%0A            self.error(%22Captcha not found%22)%0A%0A
     
@@ -2251,31 +2251,46 @@
     
-    if r'%3EWrong captcha
+if not r'%3E Preparing download link ...
 %3C' i
@@ -2318,175 +2318,77 @@
     
-    
 self.
-log_warning(_(%22Invalid captcha%22), captcha_code)%0A%0A            else:%0A                break%0A%0A        else:%0A            self.fail(_(%22Max captcha retries reached%22)
+retry_captcha()%0A%0A        else:%0A            self.captcha.correct(
 )%0A%0A 

Fix #2802