# Path: utils.py
# def fetch_page(url, decode=True, last_modified=None, extra_headers={}, method='GET', body=None):
#     """
#         @url - URL of resource to fetch
#         @decode - if True will try to decode as UTF8
#         @last_modified - datetime - if specified will try a conditional GET
#         @extra_headers - dict - headers to pass to the server
#         @method - string - HTTP verb
#         @body - string - HTTP request body if available. Used for POST/PUT
# 
#         @return - string - the contents from this URL. If None then we probably hit 304 Not Modified
#     """
# 
#     (proto, host_path) = url.split('//')
#     (host_port, path) = host_path.split('/', 1)
#     path = '/' + path
# 
#     if url.startswith('https'):
#         conn = httplib.HTTPSConnection(host_port)
#     else:
#         conn = httplib.HTTPConnection(host_port)
# 
#     # some servers, notably logilab.org returns 404 if not a browser
#     # GitHub also requires a valid UA string
#     headers = {
#         'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0.5) Gecko/20120601 Firefox/10.0.5',
#     }
# 
#     # workaround for https://code.google.com/p/support/issues/detail?id=660
#     real_method = method
#     if (method == "HEAD") and ((url.find('googlecode.com') > -1) or (url.find('search.maven.org') > -1)):
#         real_method = "GET"
#         headers['Range'] = 'bytes=0-9'
# 
#     # add additional headers
#     for h in extra_headers.keys():
#         headers[h] = extra_headers[h]
# 
#     if last_modified:
#         # If-Modified-Since: Thu, 28 Jun 2012 12:02:45 GMT
#         headers['If-Modified-Since'] = last_modified.strftime('%a, %d %b %Y %H:%M:%S GMT')
# 
# #    print "DEBUG fetch_page - before send", method, path, headers
# 
#     conn.request(real_method, path, body=body, headers=headers)
#     response = conn.getresponse()
# 
# #    print "DEBUG fetch_page - after send", response.getheaders(), response.status
# 
#     if (response.status == 404):
#         raise Exception("404 - %s not found" % url)
# 
#     if response.status in [301, 302]:
#         location = response.getheader('Location')
#         logger.info("URL Redirect %d from %s to %s" % (response.status, url, location))
#         return fetch_page(location, decode, last_modified, extra_headers, method)
# 
#     # not modified
#     if response.status == 304:
#         print "DEBUG: 304 %s" % url
#         return None
# 
#     if (method == "HEAD"):
#         if response.status == 200:
#             return response.getheader('Content-Length')
#         elif response.status == 206: # partial content
#             return response.getheader('Content-Range').strip().split(' ')[1].split('/')[1]
# 
# 
#     if decode:
#         return response.read().decode('UTF-8', 'replace')
#     else:
#         return response.read()


import json
import httplib
import logging
from utils import fetch_page
from datetime import datetime
from xml.dom.minidom import parseString
    from pprint import pprint
        For example GD::Graph vs GDGraph.
    """

    # 'install_name' : 'cpan_name'
    mappings = {
        'Cwd' : 'PathTools',
        'Dist::Zilla::Plugin::KwaliteeTests' : 'Dist::Zilla::Plugin::Test::Kwalitee',
        'GD::Graph' : 'GDGraph',
        'GD::Text' : 'GDTextUtil',
        'List::Util' : 'Scalar-List-Utils',
        'LWP' : 'libwww-perl',
        'Template' : 'Template::Toolkit',
        'Term::ReadKey' : 'TermReadKey',
        'XBase' : 'DBD-XBase',
        'XML::LibXML::Common' : 'XML::LibXML',
    }

    if mappings.has_key(name):
        return mappings[name]
    else:
        return name

def get_author_from_html(package, version):
    """
        Try to parse the HTML page and get the author
        for an older version.
    """
    package = _other_name(package)
    package = package.replace('::', '-')


# Path: utils.py
# def fetch_page(url, decode=True, last_modified=None, extra_headers={}, method='GET', body=None):
#     """
#         @url - URL of resource to fetch
#         @decode - if True will try to decode as UTF8
#         @last_modified - datetime - if specified will try a conditional GET
#         @extra_headers - dict - headers to pass to the server
#         @method - string - HTTP verb
#         @body - string - HTTP request body if available. Used for POST/PUT
# 
#         @return - string - the contents from this URL. If None then we probably hit 304 Not Modified
#     """
# 
#     (proto, host_path) = url.split('//')
#     (host_port, path) = host_path.split('/', 1)
#     path = '/' + path
# 
#     if url.startswith('https'):
#         conn = httplib.HTTPSConnection(host_port)
#     else:
#         conn = httplib.HTTPConnection(host_port)
# 
#     # some servers, notably logilab.org returns 404 if not a browser
#     # GitHub also requires a valid UA string
#     headers = {
#         'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0.5) Gecko/20120601 Firefox/10.0.5',
#     }
# 
#     # workaround for https://code.google.com/p/support/issues/detail?id=660
#     real_method = method
#     if (method == "HEAD") and ((url.find('googlecode.com') > -1) or (url.find('search.maven.org') > -1)):
#         real_method = "GET"
#         headers['Range'] = 'bytes=0-9'
# 
#     # add additional headers
#     for h in extra_headers.keys():
#         headers[h] = extra_headers[h]
# 
#     if last_modified:
#         # If-Modified-Since: Thu, 28 Jun 2012 12:02:45 GMT
#         headers['If-Modified-Since'] = last_modified.strftime('%a, %d %b %Y %H:%M:%S GMT')
# 
# #    print "DEBUG fetch_page - before send", method, path, headers
# 
#     conn.request(real_method, path, body=body, headers=headers)
#     response = conn.getresponse()
# 
# #    print "DEBUG fetch_page - after send", response.getheaders(), response.status
# 
#     if (response.status == 404):
#         raise Exception("404 - %s not found" % url)
# 
#     if response.status in [301, 302]:
#         location = response.getheader('Location')
#         logger.info("URL Redirect %d from %s to %s" % (response.status, url, location))
#         return fetch_page(location, decode, last_modified, extra_headers, method)
# 
#     # not modified
#     if response.status == 304:
#         print "DEBUG: 304 %s" % url
#         return None
# 
#     if (method == "HEAD"):
#         if response.status == 200:
#             return response.getheader('Content-Length')
#         elif response.status == 206: # partial content
#             return response.getheader('Content-Range').strip().split(' ')[1].split('/')[1]
# 
# 
#     if decode:
#         return response.read().decode('UTF-8', 'replace')
#     else:
#         return response.read()


# Path: cpan.py
import json
import httplib
import logging
from utils import fetch_page
from datetime import datetime
from xml.dom.minidom import parseString
    from pprint import pprint

        For example GD::Graph vs GDGraph.
    """

    # 'install_name' : 'cpan_name'
    mappings = {
        'Cwd' : 'PathTools',
        'Dist::Zilla::Plugin::KwaliteeTests' : 'Dist::Zilla::Plugin::Test::Kwalitee',
        'GD::Graph' : 'GDGraph',
        'GD::Text' : 'GDTextUtil',
        'List::Util' : 'Scalar-List-Utils',
        'LWP' : 'libwww-perl',
        'Template' : 'Template::Toolkit',
        'Term::ReadKey' : 'TermReadKey',
        'XBase' : 'DBD-XBase',
        'XML::LibXML::Common' : 'XML::LibXML',
    }

    if mappings.has_key(name):
        return mappings[name]
    else:
        return name

def get_author_from_html(package, version):
    """
        Try to parse the HTML page and get the author
        for an older version.
    """
    package = _other_name(package)
    package = package.replace('::', '-')


    html = fetch_page('https://metacpan.org/release/%s' % package).split('\n')