# 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
# 
# Path: guicavane/Paths.py
# HOSTS_IMAGES_DIR = os.path.join(IMAGES_DIR, "Downloaders")
# 
# SEP = os.sep


import re
import time
import gobject
from guicavane.Utils.UrlOpen import UrlOpen
from guicavane.Paths import HOSTS_IMAGES_DIR, SEP
from CaptchaWindow import CaptchaWindow, CAPTCHA_IMAGE_PATH
from Base import BaseDownloader
#!/usr/bin/env python
# coding: utf-8

"""
Hotfile Downloader.
"""




RECAPTCHA_CHALLENGE_URL = "http://api.recaptcha.net/challenge?k="
RECAPTCHA_IMAGE_URL = "http://www.google.com/recaptcha/api/image?c="

TM_RE = re.compile(r'name=tm value=(.*?)>')
TMHASH_RE = re.compile(r'name=tmhash value=(.*?)>')
WAIT_RE = re.compile(r'<input type=hidden name=wait value=(\d*?)>')
WAITHASH_RE = re.compile(r'name=waithash value=(.*?)>')
UPIDHASH_RE = re.compile(r'name=upidhash value=(.*?)>')
CAPTCHA_ID_RE = re.compile(r'src="http://api.recaptcha.net/challenge\?k=(.*?)">')
CAPTCHA_ID2_RE = re.compile(r"challenge : '(.*?)',")
FILE_URL_RE = re.compile(r'href="(.*?)" class="click_download"')


# 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
# 
# Path: guicavane/Paths.py
# HOSTS_IMAGES_DIR = os.path.join(IMAGES_DIR, "Downloaders")
# 
# SEP = os.sep


# Path: guicavane/Downloaders/Hotfile.py
import re
import time
import gobject
from guicavane.Utils.UrlOpen import UrlOpen
from guicavane.Paths import HOSTS_IMAGES_DIR, SEP
from CaptchaWindow import CaptchaWindow, CAPTCHA_IMAGE_PATH
from Base import BaseDownloader

#!/usr/bin/env python
# coding: utf-8

"""
Hotfile Downloader.
"""




RECAPTCHA_CHALLENGE_URL = "http://api.recaptcha.net/challenge?k="
RECAPTCHA_IMAGE_URL = "http://www.google.com/recaptcha/api/image?c="

TM_RE = re.compile(r'name=tm value=(.*?)>')
TMHASH_RE = re.compile(r'name=tmhash value=(.*?)>')
WAIT_RE = re.compile(r'<input type=hidden name=wait value=(\d*?)>')
WAITHASH_RE = re.compile(r'name=waithash value=(.*?)>')
UPIDHASH_RE = re.compile(r'name=upidhash value=(.*?)>')
CAPTCHA_ID_RE = re.compile(r'src="http://api.recaptcha.net/challenge\?k=(.*?)">')
CAPTCHA_ID2_RE = re.compile(r"challenge : '(.*?)',")
FILE_URL_RE = re.compile(r'href="(.*?)" class="click_download"')


MAIN_URL_OPEN = UrlOpen()