def extract_report(report):
  report_path = path_for(report, report['file_type'])
  real_report_path = os.path.abspath(os.path.expandvars(os.path.join(utils.data_dir(), report_path)))

  text_path = "%s.txt" % os.path.splitext(report_path)[0]
  real_text_path = os.path.abspath(os.path.expandvars(os.path.join(utils.data_dir(), text_path)))

  if os.path.exists(real_text_path):
    # This report has already had its text extracted
    return text_path

  file_type_lower = report['file_type'].lower()
  if file_type_lower == "pdf":
    if utils.check_pdf_decryption(real_report_path):
      real_decrypted_path = real_report_path[:-4] + ".decrypted.pdf"
      if os.path.isfile(real_decrypted_path) or utils.decrypt_pdf(real_report_path, real_decrypted_path):
        utils.text_from_pdf(real_decrypted_path, real_text_path)
      return text_path
    else:
      utils.text_from_pdf(real_report_path, real_text_path)
      return text_path
  elif file_type_lower == "doc":
    utils.text_from_doc(real_report_path, real_text_path)
    return text_path
  elif file_type_lower == "docx":
    utils.text_from_docx(real_report_path, real_text_path)
    return text_path
  elif file_type_lower in FILE_EXTENSIONS_HTML:
    utils.text_from_html(real_report_path, real_text_path)
    return text_path
  else:
    logging.warn("Unknown file type, don't know how to extract text!")
    return None
Example #2
0
def extract_metadata(report):
    report_path = path_for(report, report['file_type'])

    file_type_lower = report['file_type'].lower()
    if file_type_lower == "pdf":
        real_report_path = os.path.abspath(
            os.path.expandvars(os.path.join(utils.data_dir(), report_path)))
        if utils.check_pdf_decryption(real_report_path):
            real_decrypted_path = real_report_path[:-4] + ".decrypted.pdf"
            decrypted_path = report_path[:-4] + ".decrypted.pdf"
            if os.path.isfile(real_decrypted_path) or utils.decrypt_pdf(
                    real_report_path, real_decrypted_path):
                metadata = utils.metadata_from_pdf(decrypted_path)
            else:
                metadata = None
        else:
            metadata = utils.metadata_from_pdf(report_path)
        if metadata:
            report['pdf'] = metadata
            return metadata
    elif file_type_lower == "doc":
        metadata = utils.metadata_from_doc(report_path)
        if metadata:
            report['doc'] = metadata
            return metadata
    elif file_type_lower == "docx":
        metadata = utils.metadata_from_docx(report_path)
        if metadata:
            report['docx'] = metadata
            return metadata
    elif file_type_lower in FILE_EXTENSIONS_HTML:
        return None
    else:
        logging.warn("Unknown file type, don't know how to extract metadata!")
        return None
def extract_metadata(report):
  report_path = path_for(report, report['file_type'])

  file_type_lower = report['file_type'].lower()
  if file_type_lower == "pdf":
    real_report_path = os.path.abspath(os.path.expandvars(os.path.join(utils.data_dir(), report_path)))
    if utils.check_pdf_decryption(real_report_path):
      real_decrypted_path = real_report_path[:-4] + ".decrypted.pdf"
      decrypted_path = report_path[:-4] + ".decrypted.pdf"
      if os.path.isfile(real_decrypted_path) or utils.decrypt_pdf(real_report_path, real_decrypted_path):
        metadata = utils.metadata_from_pdf(decrypted_path)
      else:
        metadata = None
    else:
      metadata = utils.metadata_from_pdf(report_path)
    if metadata:
      report['pdf'] = metadata
      return metadata
  elif file_type_lower == "doc":
    metadata = utils.metadata_from_doc(report_path)
    if metadata:
      report['doc'] = metadata
      return metadata
  elif file_type_lower == "docx":
    metadata = utils.metadata_from_docx(report_path)
    if metadata:
      report['docx'] = metadata
      return metadata
  elif file_type_lower in FILE_EXTENSIONS_HTML:
    return None
  else:
    logging.warn("Unknown file type, don't know how to extract metadata!")
    return None
Example #4
0
def extract_report(report):
    report_path = path_for(report, report['file_type'])
    real_report_path = os.path.abspath(
        os.path.expandvars(os.path.join(utils.data_dir(), report_path)))

    text_path = "%s.txt" % os.path.splitext(report_path)[0]
    real_text_path = os.path.abspath(
        os.path.expandvars(os.path.join(utils.data_dir(), text_path)))

    if os.path.exists(real_text_path):
        # This report has already had its text extracted
        return text_path

    file_type_lower = report['file_type'].lower()
    if file_type_lower == "pdf":
        if utils.check_pdf_decryption(real_report_path):
            real_decrypted_path = real_report_path[:-4] + ".decrypted.pdf"
            if os.path.isfile(real_decrypted_path) or utils.decrypt_pdf(
                    real_report_path, real_decrypted_path):
                utils.text_from_pdf(real_decrypted_path, real_text_path)
            return text_path
        else:
            utils.text_from_pdf(real_report_path, real_text_path)
            return text_path
    elif file_type_lower == "doc":
        utils.text_from_doc(real_report_path, real_text_path)
        return text_path
    elif file_type_lower == "docx":
        utils.text_from_docx(real_report_path, real_text_path)
        return text_path
    elif file_type_lower in FILE_EXTENSIONS_HTML:
        utils.text_from_html(real_report_path, real_text_path)
        return text_path
    else:
        logging.warn("Unknown file type, don't know how to extract text!")
        return None