# -*- coding: utf-8 -*-
"""
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License,
    or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    See the GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program; if not, see .
    @author: zoidberg
"""
import re
from module.plugins.internal.SimpleHoster import SimpleHoster, parseFileInfo
from module.plugins.internal.CaptchaService import SolveMedia
from module.network.RequestFactory import getURL
def replace_eval(js_expr):
    return js_expr.replace(r'eval("', '').replace(r"\'", r"'").replace(r'\"', r'"')
def checkHTMLHeader(url):
    try:
        for i in range(3):
            header = getURL(url, just_header=True)
            for line in header.splitlines():
                line = line.lower()
                if 'location' in line:
                    url = line.split(':', 1)[1].strip()
                    if 'error.php?errno=320' in url:
                        return url, 1
                    if not url.startswith('http://'): url = 'http://www.mediafire.com' + url
                    break
                elif 'content-disposition' in line:
                    return url, 2
            else:
                break
    except:
        return url, 3
    return url, 0
def getInfo(urls):
    for url in urls:
        location, status = checkHTMLHeader(url)
        if status:
            file_info = (url, 0, status, url)
        else:
            file_info = parseFileInfo(MediafireCom, url, getURL(url, decode=True))
        yield file_info
class MediafireCom(SimpleHoster):
    __name__ = "MediafireCom"
    __type__ = "hoster"
    __pattern__ = r"http://(?:\w*\.)*mediafire\.com/(file/|(view/?|download.php)?\?)(\w{11}|\w{15})($|/)"
    __version__ = "0.79"
    __description__ = """Mediafire.com plugin - free only"""
    __author_name__ = ("zoidberg", "stickell")
    __author_mail__ = ("zoidberg@mujmail.cz", "l.stickell@yahoo.it")
    DOWNLOAD_LINK_PATTERN = r'
]*(?:z-index:(?P
\d+))?[^>]*>\s*'
    PASSWORD_PATTERN = r''
    FILE_INFO_PATTERN = r"oFileSharePopup\.ald\('(?P[^']*)','(?P[^']*)','(?P[^']*)','','(?P[^']*)'\)"
    FILE_OFFLINE_PATTERN = r'class="error_msg_title"> Invalid or Deleted File.