# Path: guicavane/Gettext.py
# TRANS_NAME = "messages"
# def configure_gettext():
# 
# Path: guicavane/Paths.py
# HOSTS_IMAGES_DIR = os.path.join(IMAGES_DIR, "Downloaders")
# 
# SEP = os.sep
# 
# Path: guicavane/Utils/UrlOpen.py
# class UrlOpen(object):
#     """ An url opener with cookies support. """
# 
#     def __init__(self, use_cache=True):
#         self.cookiejar = cookielib.LWPCookieJar(filename=COOKIES_FILE)
#         if os.path.exists(COOKIES_FILE):
#             try:
#                 self.cookiejar.load()
#             except:
#                 # Avoid corrupted cookies
#                 pass
# 
#         self.opener = self.build_opener()
#         self.set_timeout(DEFAULT_REQUEST_TIMEOUT)
#         self.use_cache = use_cache
# 
#     @retry
#     @save_cookies
#     def __call__(self, url, data=None, filename=None, handle=False, cache=True):
#         cache_key = url + str(data)
#         cache_data = cached(cache_key)
# 
#         if self.use_cache and cache and cache_data:
#             return cache_data
# 
#         if data:
#             request = urllib2.Request(url, urllib.urlencode(data), HEADERS)
#         else:
#             request = urllib2.Request(url, headers=HEADERS)
# 
#         rc = self.opener.open(request)
# 
#         # return file handler only
#         if handle:
#             return rc
# 
#         if filename:
#             local = open(filename, "wb")
#         else:
#             local = StringIO()
# 
#         while True:
#             buffer = rc.read(1024)
# 
#             if buffer == '':
#                 break
# 
#             local.write(buffer)
# 
#         if filename:
#             local.close()
#             return
# 
#         local.seek(0)
# 
#         data = local.read()
# 
#         if cache:
#             # just cache if not a file
#             cached(cache_key, data)
# 
#         return data
# 
#     def build_opener(self):
#         """ Setup cookies in urllib2. """
# 
#         cookie_handler = urllib2.HTTPCookieProcessor(self.cookiejar)
#         custom_handler = CustomHTTPHandler()
#         return urllib2.build_opener(cookie_handler, custom_handler)
# 
#     def set_timeout(self, value):
#         """ Sets the max request timeout. """
# 
#         socket.setdefaulttimeout(value)
# 
#     def add_cookies(self, cookies):
#         """ Add new cookies. """
# 
#         for cookie in cookies:
#             self.cookiejar.set_cookie(cookie)
# 
#     def add_headers(self, headers):
#         """ Add new headers. headers argument has to be a diccionary. """
# 
#         base_headers = dict(self.opener.addheaders)
#         base_headers.update(headers)
#         self.opener.addheaders = base_headers.items()
# 
#     def check_cookie(self, domain):
#         for cookie in self.cookiejar:
#             if cookie.domain[1:] == domain and not cookie.is_expired():
#                 return True
#         return False
# 
# class DownloadError(Exception):
#     """ Indicates a downloading error. """
# 
# Path: guicavane/Utils/Debug.py
# def tmp_dump(data, what="Data"):
#     """
#     Create a dump file of data in the temp directory, with a random name
#     """
#     with NamedTemporaryFile(delete=False) as fd:
#         name = fd.name
#         fd.write(data)
#     log.debug("Dumped '%s' in '%s'" % (what, name))
# 
# Path: guicavane/Utils/Log.py
# def console(name=None, logger=None, format=None):
#     handler = logging.StreamHandler()
# 
#     if "-v" in sys.argv:
#         level = logging.DEBUG
#     else:
#         level = logging.WARN
# 
#     return _get_logger(name, logger, handler, level, format)


import re
import time
import gobject
from guicavane.Gettext import gettext
from guicavane.Paths import HOSTS_IMAGES_DIR, SEP
from guicavane.Utils.UrlOpen import UrlOpen, DownloadError
from Base import BaseDownloader
from guicavane.Utils.Debug import tmp_dump
from guicavane.Utils.Log import console
    icon_path = HOSTS_IMAGES_DIR + SEP + "megaupload.png"
    accept_ranges = True

    def __init__(self, gui_manager, url):
        BaseDownloader.__init__(self, URL_OPEN, gui_manager, url)

        self.gui_manager = gui_manager
        self.url = url
        self.stop_downloading = False

    def process_url(self, play_callback, file_path):
        """ Start the download process. """

        self.play_callback = play_callback
        self.file_path = file_path

        self.gui_manager.background_task(self.get_megalink,
                    self._on_megalink_finish, unfreeze=False)

    def wait_time(self):
        """ Waits the necesary time to start the download. """

        for i in range(self.account.wait_time, 0, -1):
            gobject.idle_add(self.gui_manager.set_status_message,
                gettext("Please wait %d second%s...") % (i, "s" * (i > 1)))
            time.sleep(1)

    def start_download(self, (is_error, result)):
        """ Starts the actually download loop and opens up the player. """
        if is_error:

# Path: guicavane/Gettext.py
# TRANS_NAME = "messages"
# def configure_gettext():
# 
# Path: guicavane/Paths.py
# HOSTS_IMAGES_DIR = os.path.join(IMAGES_DIR, "Downloaders")
# 
# SEP = os.sep
# 
# Path: guicavane/Utils/UrlOpen.py
# class UrlOpen(object):
#     """ An url opener with cookies support. """
# 
#     def __init__(self, use_cache=True):
#         self.cookiejar = cookielib.LWPCookieJar(filename=COOKIES_FILE)
#         if os.path.exists(COOKIES_FILE):
#             try:
#                 self.cookiejar.load()
#             except:
#                 # Avoid corrupted cookies
#                 pass
# 
#         self.opener = self.build_opener()
#         self.set_timeout(DEFAULT_REQUEST_TIMEOUT)
#         self.use_cache = use_cache
# 
#     @retry
#     @save_cookies
#     def __call__(self, url, data=None, filename=None, handle=False, cache=True):
#         cache_key = url + str(data)
#         cache_data = cached(cache_key)
# 
#         if self.use_cache and cache and cache_data:
#             return cache_data
# 
#         if data:
#             request = urllib2.Request(url, urllib.urlencode(data), HEADERS)
#         else:
#             request = urllib2.Request(url, headers=HEADERS)
# 
#         rc = self.opener.open(request)
# 
#         # return file handler only
#         if handle:
#             return rc
# 
#         if filename:
#             local = open(filename, "wb")
#         else:
#             local = StringIO()
# 
#         while True:
#             buffer = rc.read(1024)
# 
#             if buffer == '':
#                 break
# 
#             local.write(buffer)
# 
#         if filename:
#             local.close()
#             return
# 
#         local.seek(0)
# 
#         data = local.read()
# 
#         if cache:
#             # just cache if not a file
#             cached(cache_key, data)
# 
#         return data
# 
#     def build_opener(self):
#         """ Setup cookies in urllib2. """
# 
#         cookie_handler = urllib2.HTTPCookieProcessor(self.cookiejar)
#         custom_handler = CustomHTTPHandler()
#         return urllib2.build_opener(cookie_handler, custom_handler)
# 
#     def set_timeout(self, value):
#         """ Sets the max request timeout. """
# 
#         socket.setdefaulttimeout(value)
# 
#     def add_cookies(self, cookies):
#         """ Add new cookies. """
# 
#         for cookie in cookies:
#             self.cookiejar.set_cookie(cookie)
# 
#     def add_headers(self, headers):
#         """ Add new headers. headers argument has to be a diccionary. """
# 
#         base_headers = dict(self.opener.addheaders)
#         base_headers.update(headers)
#         self.opener.addheaders = base_headers.items()
# 
#     def check_cookie(self, domain):
#         for cookie in self.cookiejar:
#             if cookie.domain[1:] == domain and not cookie.is_expired():
#                 return True
#         return False
# 
# class DownloadError(Exception):
#     """ Indicates a downloading error. """
# 
# Path: guicavane/Utils/Debug.py
# def tmp_dump(data, what="Data"):
#     """
#     Create a dump file of data in the temp directory, with a random name
#     """
#     with NamedTemporaryFile(delete=False) as fd:
#         name = fd.name
#         fd.write(data)
#     log.debug("Dumped '%s' in '%s'" % (what, name))
# 
# Path: guicavane/Utils/Log.py
# def console(name=None, logger=None, format=None):
#     handler = logging.StreamHandler()
# 
#     if "-v" in sys.argv:
#         level = logging.DEBUG
#     else:
#         level = logging.WARN
# 
#     return _get_logger(name, logger, handler, level, format)


# Path: guicavane/Downloaders/Megaupload.py
import re
import time
import gobject
from guicavane.Gettext import gettext
from guicavane.Paths import HOSTS_IMAGES_DIR, SEP
from guicavane.Utils.UrlOpen import UrlOpen, DownloadError
from Base import BaseDownloader
from guicavane.Utils.Debug import tmp_dump
from guicavane.Utils.Log import console

    icon_path = HOSTS_IMAGES_DIR + SEP + "megaupload.png"
    accept_ranges = True

    def __init__(self, gui_manager, url):
        BaseDownloader.__init__(self, URL_OPEN, gui_manager, url)

        self.gui_manager = gui_manager
        self.url = url
        self.stop_downloading = False

    def process_url(self, play_callback, file_path):
        """ Start the download process. """

        self.play_callback = play_callback
        self.file_path = file_path

        self.gui_manager.background_task(self.get_megalink,
                    self._on_megalink_finish, unfreeze=False)

    def wait_time(self):
        """ Waits the necesary time to start the download. """

        for i in range(self.account.wait_time, 0, -1):
            gobject.idle_add(self.gui_manager.set_status_message,
                gettext("Please wait %d second%s...") % (i, "s" * (i > 1)))
            time.sleep(1)

    def start_download(self, (is_error, result)):
        """ Starts the actually download loop and opens up the player. """
        if is_error:

            self.gui_manager.report_error(gettext("Error: %s") % result)