-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss_generator.py
More file actions
269 lines (225 loc) · 10.7 KB
/
rss_generator.py
File metadata and controls
269 lines (225 loc) · 10.7 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
#!/usr/bin/env python3
import os
import sqlite3
import csv
from typing import List, Dict, Optional, Tuple
from datetime import datetime, timezone, timedelta
import argparse
from urllib.parse import urlparse, parse_qs
# Mapping from document_type to URL segment for constructing LEAP URLs
TYPE_SEGMENT_MAP = {
"Monitoring Returns": "return",
"Annual Environmental Report": "return",
"Requests for Approval and Site Reports": "return",
"Site Updates/Notifications": "return",
"Site Closure and Surrender": "return",
"Site Visit": "sitevisit",
"Non Compliance": "non-compliance",
"Incident": "incident",
"Complaint": "complaint",
"Compliance Investigation": "investigation",
"EPA Initiated Correspondence": "epa-correspondence",
}
class RSSGenerator:
def __init__(self, db_path: str = "epa_ireland.db"):
"""Initialize the RSS generator with a database connection.
Args:
db_path: Path to the SQLite database file
"""
self.db_path = db_path
self.conn = sqlite3.connect(db_path)
self.conn.row_factory = sqlite3.Row
def close(self):
"""Close the database connection."""
if hasattr(self, 'conn') and self.conn:
self.conn.close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def generate_rss_feed(self, items: List[Dict[str, str]], output_path: str,
title: str, description: str, link: str = "") -> None:
"""Generate an RSS feed from a list of items.
Args:
items: List of dictionaries containing feed items
output_path: Path to save the RSS feed
title: Feed title
description: Feed description
link: Feed link (URL)
"""
if not link:
link = "https://github.com/EPA-Ireland-Updates-Unofficial/epa_ireland_scraper"
rss = f'''<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>{title}</title>
<link>{link}</link>
<description>{description}</description>
<lastBuildDate>{datetime.now(timezone.utc).strftime('%a, %d %b %Y %H:%M:%S +0000')}</lastBuildDate>
<pubDate>{datetime.now(timezone.utc).strftime('%a, %d %b %Y %H:%M:%S +0000')}</pubDate>
'''
for item in items:
rss += f''' <item>
<title>{item.get('title', 'Untitled').replace('&', '&')}</title>
<link>{item.get('link', '').replace('&', '&')}</link>
<description>{item.get('description', '').replace('&', '&')}</description>
<pubDate>{item.get('pubDate', '')}</pubDate>
<guid isPermaLink="false">{item.get('guid', item.get('link', '')).replace('&', '&')}</guid>
</item>
'''
rss += '''</channel>
</rss>'''
# Ensure output directory exists
os.makedirs(os.path.dirname(os.path.abspath(output_path)), exist_ok=True)
with open(output_path, 'w', encoding='utf-8') as f:
f.write(rss)
print(f"Generated RSS feed: {output_path}")
def _get_most_recent_csv(self, csv_dir: str) -> Optional[str]:
"""Find the most recent CSV file in the directory (searches recursively in year/month subdirectories)."""
if not os.path.exists(csv_dir):
return None
csv_files = []
# Search recursively for CSV files in year/month subdirectories
for root, dirs, files in os.walk(csv_dir):
for file in files:
if file.endswith('.csv'):
csv_files.append(os.path.join(root, file))
if not csv_files:
return None
# Sort by modification time (newest first)
csv_files.sort(key=os.path.getmtime, reverse=True)
return csv_files[0]
def generate_daily_documents_rss(self, output_dir: str = "output",
days_back: int = 1) -> str:
"""Generate RSS feed from the most recent CSV file.
Args:
output_dir: Directory to save the RSS feed
days_back: Unused, kept for backward compatibility
Returns:
Path to the generated RSS file
"""
csv_dir = os.path.join("output", "csv", "daily")
latest_csv = self._get_most_recent_csv(csv_dir)
if not latest_csv:
print("No CSV files found in", csv_dir)
return ""
items = []
try:
with open(latest_csv, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
# Try to get the most relevant fields from the CSV
doc_type = row.get('document_type', '')
title = row.get('title', row.get('document_id', 'Untitled'))
raw_url = (row.get('document_url') or '').rstrip('/')
# Prefer leap_url from CSV; fallback to document_url
url = row.get('leap_url') or raw_url
doc_date = row.get('document_date', '')
try:
pub_date = datetime.fromisoformat(doc_date).strftime('%a, %d %b %Y %H:%M:%S +0000')
except (ValueError, TypeError):
pub_date = ''
items.append({
'title': f"{doc_type}: {title}" if doc_type else title,
'link': url,
'description': f"Type: {doc_type}<br>Date: {doc_date}" if doc_date else f"Type: {doc_type}",
'pubDate': pub_date,
'guid': url or str(hash(str(row)))
})
except Exception as e:
print(f"Error reading CSV file {latest_csv}: {e}")
return ""
output_path = os.path.join(output_dir, "daily.xml")
self.generate_rss_feed(
items=items,
output_path=output_path,
title="EPA Ireland - Recent Documents",
description=f"Recent documents from EPA Ireland (from {os.path.basename(latest_csv)})"
)
return output_path
def generate_csv_listing_rss(self, csv_dir: str = "output/csv/daily",
output_dir: str = "output",
days: int = 10) -> str:
"""Generate RSS feed listing recent CSV files from the last N calendar days.
Args:
csv_dir: Base directory containing YYYY/MM/ subdirectories with CSV files
output_dir: Directory to save the RSS feed
days: Number of calendar days of CSV files to include
Returns:
Path to the generated RSS file
"""
if not os.path.exists(csv_dir):
print(f"CSV directory not found: {csv_dir}")
return ""
# Get all CSV files
csv_files = []
try:
# Get list of dates for the last 'days' calendar days
today = datetime.now(timezone.utc).date()
date_objects = [(today - timedelta(days=i)) for i in range(days)]
# Check which of these dates have corresponding CSV files
for date_obj in date_objects:
year = date_obj.strftime('%Y')
month = date_obj.strftime('%m')
date_str = date_obj.strftime('%Y-%m-%d')
# Build the expected file path
file_name = f"{date_str}.csv"
file_path = os.path.join(csv_dir, year, month, file_name)
if os.path.exists(file_path):
mtime = os.path.getmtime(file_path)
# Store both the full path and the display path (relative to the repo root)
display_path = os.path.join("output", "csv", "daily", year, month, file_name)
csv_files.append((file_path, mtime, file_name, display_path, date_str))
# Sort by date (newest first)
csv_files.sort(key=lambda x: x[4], reverse=True)
items = []
for file_path, mtime, file_name, display_path, date_str in csv_files:
file_date = datetime.fromtimestamp(mtime, timezone.utc)
pub_date = file_date.strftime('%a, %d %b %Y %H:%M:%S +0000')
# Create a GitHub URL
github_url = f"https://github.com/EPA-Ireland-Updates-Unofficial/epa_ireland_scraper/blob/main/{display_path}"
items.append({
'title': f"CSV: {file_name}",
'link': github_url,
'description': f"CSV file for {date_str}",
'pubDate': pub_date,
'guid': display_path
})
output_path = os.path.join(output_dir, "rsstwitter.xml")
self.generate_rss_feed(
items=items,
output_path=output_path,
title="EPA Ireland - Recent CSV Files",
description=f"CSV files from the last {days} calendar days"
)
return output_path
except Exception as e:
print(f"Error generating CSV listing RSS: {e}")
return ""
def main():
"""Command-line interface for RSS generation."""
parser = argparse.ArgumentParser(description='Generate RSS feeds for EPA Ireland data')
parser.add_argument('--db', default='epa_ireland.db', help='Path to SQLite database')
parser.add_argument('--output-dir', default='output', help='Output directory for RSS feeds')
parser.add_argument('--days', type=int, default=1, help='Number of days of documents to include in daily feed')
parser.add_argument('--csv-dir', default='output/csv/daily', help='Directory containing CSV files')
parser.add_argument('--csv-days', type=int, default=10, help='Number of days of CSV files to include')
args = parser.parse_args()
with RSSGenerator(args.db) as rss_gen:
# Generate documents RSS
docs_path = rss_gen.generate_daily_documents_rss(
output_dir=args.output_dir,
days_back=args.days
)
# Generate CSV listing RSS
csv_path = rss_gen.generate_csv_listing_rss(
csv_dir=args.csv_dir,
output_dir=args.output_dir,
days=args.csv_days
)
if docs_path or csv_path:
print("RSS generation complete!")
else:
print("No RSS feeds were generated.")
if __name__ == '__main__':
main()