import os
import logging
import requests
from tempfile import NamedTemporaryFile

from aleph.core import archive, celery
from aleph.model import clear_session
from aleph.model.metadata import Metadata
from aleph.ingest.ingestor import Ingestor

log = logging.getLogger(__name__)

# dispatch
#   URLIngestor / URLCrawler
#   RarFileIngestor
#   ZipFileIngestor
#   OCRIngestor
#   PDFIngestor
#   WordIngestor
#   HTMLIngestor
#   TableIngestor
#
# https://bugzilla.redhat.com/show_bug.cgi?id=191060#c1
# https://github.com/deanmalmgren/textract/blob/master/textract/parsers/pptx_parser.py
# https://github.com/chardet/chardet
# http://poppler.freedesktop.org/
# http://www.unixuser.org/~euske/python/pdfminer/index.html
# https://mstamy2.github.io/PyPDF2/#documentation
# http://pybrary.net/pyPdf/pythondoc-pyPdf.pdf.html
#
# https://svn.apache.org/viewvc/httpd/httpd/branches/2.2.x/docs/conf/mime.types?view=annotate


@celery.task()
def ingest_url(source_id, metadata, url):
    clear_session()
    meta = Metadata(data=metadata)
    try:
        with NamedTemporaryFile() as fh:
            log.info("Ingesting URL: %r", url)
            res = requests.get(url, stream=True)
            meta.source_url = res.url
            for chunk in res.iter_content(chunk_size=1024):
                if chunk:
                    fh.write(chunk)
            fh.flush()

            if not meta.has('file_name'):
                meta.file_name = meta.file_name
            meta.headers = res.headers
            ingest_file(source_id, meta, fh.name)
    except Exception as ex:
        log.exception(ex)


def ingest_file(source_id, meta, file_name, move=False):
    if not os.path.isfile(file_name):
        raise ValueError("No such file: %r", file_name)
    if not meta.has('source_path'):
        meta.source_path = file_name
    meta = archive.archive_file(file_name, meta, move=move)
    ingest.delay(source_id, meta.data)


@celery.task()
def ingest(source_id, metadata):
    clear_session()
    meta = Metadata(data=metadata)
    try:
        Ingestor.dispatch(source_id, meta)
    except Exception as ex:
        log.exception(ex)

@@ -277,174 +277,8 @@
 _)%0A%0A
-# dispatch%0A#   URLIngestor / URLCrawler%0A#   RarFileIngestor%0A#   ZipFileIngestor%0A#   OCRIngestor%0A#   PDFIngestor%0A#   WordIngestor%0A#   HTMLIngestor%0A#   TableIngestor%0A#%0A
 # ht
@@ -653,10 +653,8 @@
 tml%0A
-#%0A
 # ht
@@ -853,37 +853,24 @@
 a=metadata)%0A
-    try:%0A    
     with Nam
@@ -898,28 +898,24 @@
 fh:%0A        
- 
-   
 log.info(%22In
@@ -937,20 +937,16 @@
 %22, url)%0A
-    
         
@@ -986,28 +986,24 @@
 ue)%0A        
-    
 meta.source_
@@ -1024,20 +1024,16 @@
         
-   
- 
 for chun
@@ -1084,20 +1084,16 @@
         
-    
 if chunk
@@ -1110,20 +1110,16 @@
         
-  
-  
 fh.write
@@ -1134,20 +1134,16 @@
         
-    
 fh.flush
@@ -1154,20 +1154,16 @@
         
- 
-   
 if not m
@@ -1180,28 +1180,24 @@
 ile_name'):%0A
-    
             
@@ -1232,28 +1232,24 @@
 ame%0A        
-    
 meta.headers
@@ -1271,20 +1271,16 @@
         
-   
- 
 ingest_f
@@ -1313,62 +1313,8 @@
 me)%0A
-    except Exception as ex:%0A        log.exception(ex)%0A
 %0A%0Ade
@@ -1743,21 +1743,8 @@
 ta)%0A
-    try:%0A    
     
@@ -1782,58 +1782,4 @@
 ta)%0A
-    except Exception as ex:%0A        log.exception(ex)%0A

handle exceptions