-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_processor.py
More file actions
275 lines (224 loc) · 11.1 KB
/
web_processor.py
File metadata and controls
275 lines (224 loc) · 11.1 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
"""
Web-based Weekly Circular PDF Processor
Adapted for browser/online deployment
"""
import io
import re
from pathlib import Path
from typing import Dict, List, Tuple
import PyPDF2
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen.canvas import Canvas
class WebWeeklyCircularProcessor:
def __init__(self, pdf_data: bytes, filename: str):
self.pdf_data = pdf_data
self.filename = filename
# Route keywords to search for
self.route_keywords = ['Cork', 'Tralee', 'Mallow', 'cork', 'tralee', 'mallow']
# Day patterns to look for
self.day_patterns = [
r'\b(MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY|SUNDAY)\b',
r'\b(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)\b',
r'\b(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\b',
r'\b(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)\s+\d{1,2}',
r'\b(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s+\d{1,2}',
r'\b(MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY|SUNDAY)\s+\d{1,2}'
]
# Compile regex patterns
self.day_regex = re.compile('|'.join(self.day_patterns), re.IGNORECASE)
self.route_regex = re.compile('|'.join(self.route_keywords), re.IGNORECASE)
def extract_week_ending_from_filename(self) -> str:
"""Extract week ending date from PDF filename"""
# Look for date pattern like "19.10.2025" or "19/10/2025"
date_patterns = [
r'(\d{1,2}[./]\d{1,2}[./]\d{4})', # DD.MM.YYYY or DD/MM/YYYY
r'(\d{1,2}[./]\d{1,2}[./]\d{2})', # DD.MM.YY or DD/MM/YY
]
for pattern in date_patterns:
match = re.search(pattern, self.filename)
if match:
date_str = match.group(1)
# Normalize to DD.MM.YYYY format
if '/' in date_str:
date_str = date_str.replace('/', '.')
return date_str
# If no date found, use current date
from datetime import datetime
return datetime.now().strftime("%d.%m.%Y")
def extract_text_from_page(self, page) -> str:
"""Extract text from a PDF page"""
try:
return page.extract_text()
except Exception as e:
print(f"Error extracting text from page: {e}")
return ""
def find_day_in_text(self, text: str) -> str:
"""Find day of the week in the text"""
try:
# Look for day patterns in the first few lines
lines = text.split('\n')[:10] # Check first 10 lines for better detection
for line in lines:
line = line.strip()
if line:
match = self.day_regex.search(line)
if match and match.group(1):
day = match.group(1).lower()
# Normalize day names
day_mapping = {
'mon': 'Monday', 'tue': 'Tuesday', 'wed': 'Wednesday',
'thu': 'Thursday', 'fri': 'Friday', 'sat': 'Saturday', 'sun': 'Sunday',
'monday': 'Monday', 'tuesday': 'Tuesday', 'wednesday': 'Wednesday',
'thursday': 'Thursday', 'friday': 'Friday', 'saturday': 'Saturday', 'sunday': 'Sunday'
}
return day_mapping.get(day.lower(), day.capitalize())
# If no day found, try looking for common patterns
text_lower = text.lower()
if any(word in text_lower for word in ['monday', 'mon']):
return "Monday"
elif any(word in text_lower for word in ['tuesday', 'tue']):
return "Tuesday"
elif any(word in text_lower for word in ['wednesday', 'wed']):
return "Wednesday"
elif any(word in text_lower for word in ['thursday', 'thu']):
return "Thursday"
elif any(word in text_lower for word in ['friday', 'fri']):
return "Friday"
elif any(word in text_lower for word in ['saturday', 'sat']):
return "Saturday"
elif any(word in text_lower for word in ['sunday', 'sun']):
return "Sunday"
return "Unknown"
except Exception as e:
print(f"Error in day detection: {e}")
return "Unknown"
def has_route_keywords(self, text: str) -> bool:
"""Check if page contains route keywords"""
return bool(self.route_regex.search(text))
def process_pdf(self) -> Dict:
"""Main processing function - returns results instead of saving files"""
print(f"Processing PDF: {self.filename}")
try:
pdf_reader = PyPDF2.PdfReader(io.BytesIO(self.pdf_data))
total_pages = len(pdf_reader.pages)
print(f"Total pages: {total_pages}")
# Group pages by day
pages_by_day: Dict[str, List[int]] = {}
route_relevant_pages: List[int] = []
current_day = "Unknown" # Track current day context
for page_num in range(total_pages):
page = pdf_reader.pages[page_num]
text = self.extract_text_from_page(page)
# Find day
day = self.find_day_in_text(text)
# If we found a day, update current context
if day != "Unknown":
current_day = day
# Use current day context for grouping
day_to_use = current_day
# Check for route keywords
if self.has_route_keywords(text):
route_relevant_pages.append(page_num)
print(f"Page {page_num + 1} ({day_to_use}): Contains route keywords")
# Group by day
if day_to_use not in pages_by_day:
pages_by_day[day_to_use] = []
pages_by_day[day_to_use].append(page_num)
# Create day PDFs in memory
day_pdfs = self.create_day_pdfs_in_memory(pdf_reader, pages_by_day)
# Create summary report
summary_report = self.create_summary_report_text(pages_by_day, route_relevant_pages)
return {
'success': True,
'total_pages': total_pages,
'pages_by_day': pages_by_day,
'route_relevant_pages': route_relevant_pages,
'day_pdfs': day_pdfs,
'summary_report': summary_report,
'week_ending': self.extract_week_ending_from_filename()
}
except Exception as e:
print(f"Error processing PDF: {e}")
return {
'success': False,
'error': str(e)
}
def create_day_pdfs_in_memory(self, pdf_reader, pages_by_day: Dict[str, List[int]]) -> Dict[str, bytes]:
"""Create separate PDF files for each day in memory"""
day_pdfs = {}
for day, page_numbers in pages_by_day.items():
if day == "Unknown":
continue
try:
pdf_writer = PyPDF2.PdfWriter()
for page_num in page_numbers:
pdf_writer.add_page(pdf_reader.pages[page_num])
# Write to bytes buffer
output_buffer = io.BytesIO()
pdf_writer.write(output_buffer)
day_pdfs[f"{day}_pages.pdf"] = output_buffer.getvalue()
print(f"Created {day} PDF in memory ({len(page_numbers)} pages)")
except Exception as e:
print(f"Error creating {day} PDF: {e}")
return day_pdfs
def create_summary_report_text(self, pages_by_day: Dict[str, List[int]], route_pages: List[int]) -> str:
"""Create a text summary report"""
report_lines = []
report_lines.append("WEEKLY CIRCULAR PROCESSING SUMMARY")
report_lines.append("=" * 40)
report_lines.append("")
report_lines.append(f"Original PDF: {self.filename}")
report_lines.append(f"Processing Date: {Path.cwd()}")
report_lines.append("")
report_lines.append("PAGES BY DAY:")
report_lines.append("-" * 20)
for day, pages in pages_by_day.items():
report_lines.append(f"{day}: {len(pages)} pages")
report_lines.append(f" Page numbers: {[p+1 for p in pages]}")
report_lines.append("")
report_lines.append("ROUTE-RELEVANT PAGES:")
report_lines.append("-" * 25)
report_lines.append(f"Pages containing Cork/Tralee/Mallow: {len(route_pages)}")
if route_pages:
report_lines.append(f"Page numbers: {[p+1 for p in route_pages]}")
report_lines.append("")
report_lines.append("KEYWORDS SEARCHED:")
report_lines.append("-" * 20)
report_lines.append("Cork, Tralee, Mallow")
return "\n".join(report_lines)
def process_multiple_pdfs(pdf_files_data: List[Tuple[bytes, str]]) -> Dict:
"""Process multiple PDF files and combine results"""
all_results = []
for pdf_data, filename in pdf_files_data:
processor = WebWeeklyCircularProcessor(pdf_data, filename)
result = processor.process_pdf()
all_results.append(result)
# Combine results
combined_pages_by_day = {}
combined_route_pages = []
all_day_pdfs = {}
all_summary_reports = []
for i, result in enumerate(all_results):
if result['success']:
# Combine pages by day
for day, pages in result['pages_by_day'].items():
if day not in combined_pages_by_day:
combined_pages_by_day[day] = []
combined_pages_by_day[day].extend(pages)
# Combine route pages
combined_route_pages.extend(result['route_relevant_pages'])
# Store day PDFs with filename prefix
for pdf_name, pdf_data in result['day_pdfs'].items():
prefixed_name = f"{result['week_ending']}_{pdf_name}"
all_day_pdfs[prefixed_name] = pdf_data
# Store summary reports
all_summary_reports.append(f"=== {result['week_ending']} ===\n{result['summary_report']}")
return {
'success': True,
'individual_results': all_results,
'combined_pages_by_day': combined_pages_by_day,
'combined_route_pages': combined_route_pages,
'all_day_pdfs': all_day_pdfs,
'combined_summary_report': "\n\n".join(all_summary_reports)
}