File size: 2,446 Bytes
a88785b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import pymupdf
from io import BytesIO
import docx2txt
from datetime import datetime
import zipfile
from xml.etree import ElementTree as ET


def extract_text_from_bytes(pdf_bytes):
    try:
        memory_stream = BytesIO(pdf_bytes)
        doc = pymupdf.open(stream=memory_stream, filetype="pdf")
        
        # Extract text
        text = ""
        for page in doc:
            text += page.get_text()
            
        # Get modification date
        metadata = doc.metadata
        mod_date = metadata.get("modDate", None)
        
        doc.close()
        return text, mod_date
    except Exception as e:
        return f"Error reading PDF: {str(e)}", None


def get_docx_last_modified(docx_stream):

    try:
        docx_stream.seek(0)  # Reset stream position to start
        with zipfile.ZipFile(docx_stream, 'r') as docx:
            if 'docProps/core.xml' in docx.namelist():
                with docx.open('docProps/core.xml') as f:
                    core_xml = f.read()
                
                root = ET.fromstring(core_xml)
                
                namespaces = {
                    'cp': 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties',
                    'dcterms': 'http://purl.org/dc/terms/'
                }
                
                modified_element = root.find('.//dcterms:modified', namespaces)
                
                if modified_element is not None:
                    modified_date_str = modified_element.text
                    return datetime.fromisoformat(modified_date_str.replace('Z', '+00:00'))
    except Exception as e:
        print(f"Error extracting DOCX metadata: {e}")
    return None


def extract_text_from_docx(docx_bytes):
    try:
        docx_stream = BytesIO(docx_bytes)
        text = docx2txt.process(docx_stream)
        
        metadata_stream = BytesIO(docx_bytes)
        mod_date = get_docx_last_modified(metadata_stream)
        
        return text, mod_date
    except Exception as e:
        return f"Error reading DOCX: {str(e)}", None


def parse_pdf_date(pdf_date: str) -> datetime:
    
    if not pdf_date or not pdf_date.startswith("D:"):
        return datetime.now()
    
    try:
        date_str = pdf_date[2:14]  
        return datetime.strptime(date_str, "%Y%m%d%H%M")
    except Exception:
        return datetime.now()