# coding: utf-8

from os import utime as setfiletime
from os.path import join as path_join, isdir, basename, dirname
from shutil import copy2 as copy_file
try:
    from urllib2 import urlopen
except ImportError:
    from urllib.request import urlopen, Request
import calendar
import datetime
import os
import re
import time

import osext.filesystem as fs


def get_last_modified_time(url, headers=None):
# TODO Handle timezones correctly
    if headers:
        url = Request(url, None, headers)

    req = urlopen(url)
    req.get_method = lambda: 'HEAD'
    last_modified = None

    for line in str(req.info()).split('\n'):
        if 'last-modified' in line.lower():
            last_modified = line.split(': ')[1].strip()
            last_modified = time.strptime(last_modified.replace(' GMT', ''),
                                          '%a, %d %b %Y %H:%M:%S')
            break

    if not last_modified:
        return None

    return last_modified


def dl(url, local_file, cache=True, headers=None):
    last_modified = get_last_modified_time(url, headers=headers)
    filetime = None

    if last_modified:
        filetime = calendar.timegm(last_modified)

    cache_dir = path_join(os.environ['HOME'], '.cache', 'httpext')
    cut = len(dirname(url)) + 1
    cached_fn = '%05d%s' % (cut, re.sub('^https?\://', '', url).replace('/', '2'))
    cached_file = path_join(cache_dir, cached_fn)

    if cache:
        if not isdir(cache_dir):
            os.makedirs(cache_dir)

        if fs.isfile(cached_file) and \
                fs.has_same_time(cached_file, filetime):
            original_fn = cached_fn[int(cached_fn[0:5]) - 2:]
            copy_file(cached_file, path_join(dirname(local_file), original_fn))

            return

    if headers:
        url = Request(url, None, headers)

    req = urlopen(url)

    with open(local_file, 'wb') as f:
        f.write(req.read())
        f.close()

        if filetime:
            setfiletime(local_file, (filetime, filetime))

        if cache:
            copy_file(local_file, cached_file)


def append_url_contents(url, local_file):
    with open(local_file, 'ab') as f:
        f.write(urlopen(url).read())

@@ -285,16 +285,31 @@
 atetime%0A
+import logging%0A
 import o
@@ -1019,24 +1019,66 @@
 ders=None):%0A
+    logger = logging.getLogger('httpext')%0A
     last_mod
@@ -1483,32 +1483,140 @@
     
-if not isdir(cache_dir):
+logger.debug('cache = True')%0A%0A        if not isdir(cache_dir):%0A            logger.debug('Creating cache directory %25s' %25 (cache_dir))
 %0A   
@@ -1761,57 +1761,81 @@
     
-original_fn = cached_fn%5Bint(cached_fn%5B0:5%5D) - 2:%5D
+logger.debug('Has cached file %25s and file has same time' %25 (cached_file))
 %0A   
@@ -1901,19 +1901,18 @@
 e), 
-origin
+loc
 al_f
-n
+ile
 ))%0A%0A

Add some logging