-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_ordins_new.py
More file actions
executable file
·129 lines (106 loc) · 3.85 KB
/
Copy pathparse_ordins_new.py
File metadata and controls
executable file
·129 lines (106 loc) · 3.85 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
import os
import re
import time
import pycurl
import sys
import subprocess
import glob
import logging
import sqlite3
import fitz # pip install PyMuPDF
from pdfminer.high_level import extract_text
from io import BytesIO # Ensure io module is correctly imported
from datetime import datetime
import unicodedata
from bs4 import BeautifulSoup
import sys
sys.dont_write_bytecode = True
# Record start time
start_time = time.time()
# Constants
CRED = '\033[91m'
COK = '\033[92m'
CWARN = '\033[93m'
CVIOLET = '\033[95m'
CEND = '\033[0m'
from incremental import db_path
Database = db_path()
Ordins = './ordins/'
# Logging setup (shared factory honoring the global ANC_DEBUG switch).
from incremental import setup_logger
if __name__ == '__main__':
logger = setup_logger('main_logger', 'parse-ordins-new-' + datetime.now().strftime("%Y-%m-%d") + '.log', mode='w')
SQLlogger = setup_logger('SQLlogger', 'sql-ordins-new-'+datetime.now().strftime("%Y-%m-%d")+'.log', mode='w')
else:
logger = logging.getLogger('main_logger')
SQLlogger = logging.getLogger('SQLlogger')
# Reuse parse_ordins_all's connection/cursor so that parsing (parse_pdf) and the
# refuzuri recompute below write through the SAME connection to the SAME DB.
# (Previously this script opened its own connection -> split-brain risk.)
from parse_ordins_all import parse_pdf, connection, db
from incremental import FileState
def clear_buffer(buffer):
buffer.seek(0)
buffer.truncate(0)
# PDF validity check
def is_valid_pdf(filepath):
try:
with fitz.open(filepath) as doc:
return True
except Exception:
return False
# Use the robust downloader script (handles WAF and skips SSL verification)
# Detect files present before running the downloader, run it, then compute newly added files.
os.makedirs(Ordins, exist_ok=True)
downloader = os.path.join(os.path.dirname(__file__), 'get_ordins_no_ssl.py')
if os.path.isfile(downloader):
print(f"Running downloader: {downloader}")
try:
subprocess.run([sys.executable, downloader], check=False)
except Exception as e:
print(f"Downloader failed: {e}")
else:
print(f"Downloader script not found: {downloader}")
# Determine which ordin PDFs still need parsing via the shared sidecar state.
# Robust to files fetched by a separate downloader run, and recoverable after a
# crash (only files marked below are skipped next time).
state = FileState('ordins')
candidates = sorted(glob.glob(os.path.join(Ordins, '*.pdf')))
new_files = state.new_files(candidates)
print(f"New files detected: {len(new_files)} (of {len(candidates)} total)")
# --- Recompute rejections (incrementally by ordinance list) ---
def recompute_refuzuri():
db.execute('''
UPDATE Dosar11
SET refuz=1
WHERE ordin IN (
SELECT ordin
FROM Dosar11
WHERE ordin IS NOT NULL
GROUP BY ordin
HAVING COUNT(*) = 1
)
''')
SQLlogger.info('Refuz set: ' + str(db.rowcount))
print(f"Total refuz set to 1 with uniq ordin = 1: {COK}{str(db.rowcount)}{CEND}")
db.execute('''
INSERT OR REPLACE INTO Refuz11 (id, ordin, depun, solutie)
SELECT id, ordin, depun, solutie
FROM Dosar11
WHERE refuz=1 AND ordin IS NOT NULL
''')
SQLlogger.info('Refuz11 rebuilt: ' + str(db.rowcount))
# Parsing is delegated to parse_ordins_all.parse_pdf
logger.info('Start parsing ordins at ' + datetime.now().strftime("%Y-%m-%d %M:%S"))
# Parse only new files; mark each in the sidecar state right after (crash-safe).
for filename in new_files:
parse_pdf(filename)
state.mark(filename)
recompute_refuzuri()
connection.close()
logger.info("Processing complete.")
# Calculate execution time
end_time = time.time()
execution_time = end_time - start_time
print(f"{'Parsing PDF time: '}{COK}{execution_time:.2f}{CEND} seconds")