#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import string
from urllib import unquote
from module.plugins.Hoster import Hoster
from module.plugins.ReCaptcha import ReCaptcha
from module.utils import chunks
from module.network.RequestFactory import getURL
from module.common.json_layer import json_loads
def getInfo(urls):
    yield [(url, 0, 1, url) for url in urls]
def getId(url):
    match = re.search(FilesonicCom.FILE_ID_PATTERN, url)
    if match:
        return string.replace(match.group("id"), "/", "-")
    else:
        return None
class FilesonicCom(Hoster):
    __name__ = "FilesonicCom"
    __type__ = "hoster"
    __pattern__ = r"http://[\w\.]*?(sharingmatrix|filesonic)\..*?/.*?file/([a-zA-Z0-9]+(/.+)?|[a-z0-9]+/[0-9]+(/.+)?|[0-9]+(/.+)?)"
    __version__ = "0.36"
    __description__ = """FilesonicCom und Sharingmatrix Download Hoster"""
    __author_name__ = ("jeix", "paulking")
    __author_mail__ = ("jeix@hasnomail.de", "")
    API_ADDRESS = "http://api.filesonic.com"
    URL_DOMAIN_PATTERN = r'(?P.*?)(?P.(filesonic|sharingmatrix)\..+?)(?P/.*)'
    FILE_ID_PATTERN = r'/file/(?P([a-z][0-9]+/)?[a-zA-Z0-9\-._+]+)(/.*)?' #change may break wupload - be careful
    FILE_LINK_PATTERN = r'Your download is ready
\s*\d+)'
    WAIT_TM_PATTERN = r"name='tm' value='(.*?)' />"
    WAIT_TM_HASH_PATTERN = r"name='tm_hash' value='(.*?)' />"
    CAPTCHA_TYPE1_PATTERN = r'Recaptcha.create\("(.*?)",'
    CAPTCHA_TYPE2_PATTERN = r'id="recaptcha_image">image?c=(.+?)) 300:
                self.wantReconnect = True
            self.setWait(wait)
            self.logDebug("Waiting %d seconds." % wait)
            self.wait()
            tm = re.search(self.WAIT_TM_PATTERN, self.html)
            tm_hash = re.search(self.WAIT_TM_HASH_PATTERN, self.html)
            if tm and tm_hash:
                tm = tm.group(1)
                tm_hash = tm_hash.group(1)
                self.html = self.load(url, post={"tm": tm, "tm_hash": tm_hash})
                self.handleErrors()
                break
            else:
                self.html = self.load(url)
                self.handleErrors()
                waitSearch = re.search(self.WAIT_TIME_PATTERN, self.html)
    def handleErrors(self):
        if "This file is available for premium users only." in self.html:
            self.fail("need premium account for file")
        if "The file that you're trying to download is larger than" in self.html:
            self.fail("need premium account for file")
        if "Free users may only download 1 file at a time" in self.html:
            self.fail("only 1 file at a time for free users")
        if "Free user can not download files" in self.html:
            self.fail("need premium account for file")
        if "Download session in progress" in self.html:
            self.fail("already downloading")
        if "This file is password protected" in self.html:
            self.fail("This file is password protected")
        if "An Error Occurred" in self.html:
            self.fail("A server error occured.")
        if "This file was deleted" in self.html:
            self.offline()
 300:
                self.wantReconnect = True
            self.setWait(wait)
            self.logDebug("Waiting %d seconds." % wait)
            self.wait()
            tm = re.search(self.WAIT_TM_PATTERN, self.html)
            tm_hash = re.search(self.WAIT_TM_HASH_PATTERN, self.html)
            if tm and tm_hash:
                tm = tm.group(1)
                tm_hash = tm_hash.group(1)
                self.html = self.load(url, post={"tm": tm, "tm_hash": tm_hash})
                self.handleErrors()
                break
            else:
                self.html = self.load(url)
                self.handleErrors()
                waitSearch = re.search(self.WAIT_TIME_PATTERN, self.html)
    def handleErrors(self):
        if "This file is available for premium users only." in self.html:
            self.fail("need premium account for file")
        if "The file that you're trying to download is larger than" in self.html:
            self.fail("need premium account for file")
        if "Free users may only download 1 file at a time" in self.html:
            self.fail("only 1 file at a time for free users")
        if "Free user can not download files" in self.html:
            self.fail("need premium account for file")
        if "Download session in progress" in self.html:
            self.fail("already downloading")
        if "This file is password protected" in self.html:
            self.fail("This file is password protected")
        if "An Error Occurred" in self.html:
            self.fail("A server error occured.")
        if "This file was deleted" in self.html:
            self.offline()