# Path: bormeparser/exceptions.py
# class BormeDoesntExistException(BormeparserException):
#     pass
# 
# Path: bormeparser/parser.py
# def parse(data, seccion):
#     module = importlib.import_module(DEFAULT_PARSER[seccion][0])
#     parser = getattr(module, DEFAULT_PARSER[seccion][1])
#     if os.path.isfile(data):
#         borme = parser(data).parse()
#     elif data.startswith('http'):
#         # TODO
#         # req = requests.get(data)
#         # content = req.text
#         borme = parser(data).parse()
#     else:
#         raise IOError(data)
# 
#     return borme
# 
# Path: bormeparser/seccion.py
# class SECCION:
#     A = 'A'
#     B = 'B'
#     C = 'C'
# 
#     # TODO: No detecta tipo C, viene el texto comprimido
#     @staticmethod
#     def from_borme(seccion, subseccion):
#         if seccion in ('SECCIÓN PRIMERA', 'SECCIÓN PRIMERA'):
#             if subseccion == 'Actos inscritos':
#                 return SECCION.A
#             elif subseccion == 'Otros actos publicados en el Registro Mercantil':
#                 return SECCION.B
#             else:
#                 raise ValueError('InvalidSeccion: %s %s' % (seccion, subseccion))
#         else:
#             raise ValueError('InvalidSeccion: %s %s' % (seccion, subseccion))


import datetime
import os
import requests
import time
import logging
from lxml import etree
from threading import Thread
from .exceptions import BormeDoesntExistException
from .parser import parse as parse_borme
from .seccion import SECCION
from queue import Queue

    return BORME_AB_PDF_URL.format(protocol=protocol, year=date.year, month=date.month, day=date.day,
                                   seccion=seccion, nbo=nbo, provincia=provincia.code)

"""
# Needs research
def get_url_borme_c(date, some_number, format='xml'):
    if format == 'xml':
        return BORME_C_XML_URL.format(protocol=protoco, year=date.year, anuncio=some_number)
    elif format in ('htm', 'html'):
        return BORME_C_HTM_URL.format(protocol=protocol, year=date.year, anuncio=some_number)
    elif format == 'pdf':
        return BORME_C_PDF_URL.format(protocol=protocol, year=date.year, month=date.month, day=month.day, anuncio=some_number)
    else:
        raise ValueError('format must be "xml", "htm" or "pdf"')
"""

def get_nbo_from_xml(source):
    """ Devuelve el Número de Boletín Oficial (nbo)
        source: url o path
        Nota: Requiere conexión a Internet si source es una URL
    """
    if source.startswith('http'):
        req = requests.get(source)
        content = req.text.encode(req.encoding)
        tree = etree.fromstring(content).getroottree()
    else:
        tree = etree.parse(source)

    if tree.getroot().tag != 'sumario':

# Path: bormeparser/exceptions.py
# class BormeDoesntExistException(BormeparserException):
#     pass
# 
# Path: bormeparser/parser.py
# def parse(data, seccion):
#     module = importlib.import_module(DEFAULT_PARSER[seccion][0])
#     parser = getattr(module, DEFAULT_PARSER[seccion][1])
#     if os.path.isfile(data):
#         borme = parser(data).parse()
#     elif data.startswith('http'):
#         # TODO
#         # req = requests.get(data)
#         # content = req.text
#         borme = parser(data).parse()
#     else:
#         raise IOError(data)
# 
#     return borme
# 
# Path: bormeparser/seccion.py
# class SECCION:
#     A = 'A'
#     B = 'B'
#     C = 'C'
# 
#     # TODO: No detecta tipo C, viene el texto comprimido
#     @staticmethod
#     def from_borme(seccion, subseccion):
#         if seccion in ('SECCIÓN PRIMERA', 'SECCIÓN PRIMERA'):
#             if subseccion == 'Actos inscritos':
#                 return SECCION.A
#             elif subseccion == 'Otros actos publicados en el Registro Mercantil':
#                 return SECCION.B
#             else:
#                 raise ValueError('InvalidSeccion: %s %s' % (seccion, subseccion))
#         else:
#             raise ValueError('InvalidSeccion: %s %s' % (seccion, subseccion))


# Path: bormeparser/download.py
import datetime
import os
import requests
import time
import logging
from lxml import etree
from threading import Thread
from .exceptions import BormeDoesntExistException
from .parser import parse as parse_borme
from .seccion import SECCION
from queue import Queue


    return BORME_AB_PDF_URL.format(protocol=protocol, year=date.year, month=date.month, day=date.day,
                                   seccion=seccion, nbo=nbo, provincia=provincia.code)

"""
# Needs research
def get_url_borme_c(date, some_number, format='xml'):
    if format == 'xml':
        return BORME_C_XML_URL.format(protocol=protoco, year=date.year, anuncio=some_number)
    elif format in ('htm', 'html'):
        return BORME_C_HTM_URL.format(protocol=protocol, year=date.year, anuncio=some_number)
    elif format == 'pdf':
        return BORME_C_PDF_URL.format(protocol=protocol, year=date.year, month=date.month, day=month.day, anuncio=some_number)
    else:
        raise ValueError('format must be "xml", "htm" or "pdf"')
"""

def get_nbo_from_xml(source):
    """ Devuelve el Número de Boletín Oficial (nbo)
        source: url o path
        Nota: Requiere conexión a Internet si source es una URL
    """
    if source.startswith('http'):
        req = requests.get(source)
        content = req.text.encode(req.encoding)
        tree = etree.fromstring(content).getroottree()
    else:
        tree = etree.parse(source)

    if tree.getroot().tag != 'sumario':

        raise BormeDoesntExistException