-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_transparency_scraper.py
More file actions
271 lines (227 loc) · 10.8 KB
/
Copy pathsetup_transparency_scraper.py
File metadata and controls
271 lines (227 loc) · 10.8 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
#!/usr/bin/env python3
"""
Transparency International Pakistan Scraper
Specialized scraper for transparency.org.pk corruption and governance data
"""
import asyncio
import json
import sys
from pathlib import Path
from datetime import datetime
import re
# Add project root to path
sys.path.append(str(Path(__file__).parent))
from services.scraping.job_manager import ScrapingJobManager, ScrapingType, JobPriority
from services.scraping.scheduler import ScrapingScheduler
async def create_transparency_pk_jobs():
"""Create specialized scraping jobs for Transparency International Pakistan"""
print("\n" + "="*80)
print("🔍 TRANSPARENCY INTERNATIONAL PAKISTAN SCRAPER SETUP")
print("="*80)
print("Setting up specialized scraping jobs for transparency.org.pk")
print("Focus: Corruption data, governance reports, and compliance intelligence")
print("="*80)
# Initialize job manager
async with ScrapingJobManager() as job_manager:
scheduler = ScrapingScheduler(job_manager)
# Define target URLs for different data categories
transparency_urls = {
"corruption_news": [
"https://transparency.org.pk",
"https://transparency.org.pk/news-section/",
],
"government_procurement": [
"https://transparency.org.pk/procurement-rules/",
"https://transparency.org.pk/government-of-pakistan-3/",
"https://transparency.org.pk/government-of-punjab-3/",
"https://transparency.org.pk/government-of-sindh/",
"https://transparency.org.pk/government-of-kpk-2/",
"https://transparency.org.pk/government-of-balochistan/",
],
"governance_reports": [
"https://transparency.org.pk/publication/",
"https://transparency.org.pk/annual-reports/",
"https://transparency.org.pk/events-22/",
],
"compliance_data": [
"https://transparency.org.pk/our-organiztion/",
"https://transparency.org.pk/our-policies/",
"https://transparency.org.pk/incoming-complaint/",
]
}
created_jobs = []
# Create jobs for each category
for category, urls in transparency_urls.items():
print(f"\n🎯 Creating job for: {category.replace('_', ' ').title()}")
# Advanced configuration for transparency data
config = {
'delay': 3.0, # Respectful delay
'max_content_length': 50000, # Larger content for reports
'timeout': 45,
'retry_attempts': 3,
'user_agent': 'Compliant-One Transparency Monitor 1.0',
'extract_links': True,
'follow_redirects': True,
'save_html': True,
'extract_metadata': True
}
metadata = {
'category': category,
'source': 'transparency.org.pk',
'data_type': 'transparency_governance',
'jurisdiction': 'Pakistan',
'priority_keywords': [
'corruption', 'governance', 'transparency', 'procurement',
'accountability', 'audit', 'investigation', 'compliance',
'anti-corruption', 'oversight', 'reform', 'regulation'
],
'created_by': 'transparency_scraper_setup',
'purpose': 'compliance_intelligence'
}
job_id = job_manager.create_job(
name=f"TI Pakistan - {category.replace('_', ' ').title()}",
job_type=ScrapingType.GOVERNMENT_DATA,
target_urls=urls,
priority=JobPriority.HIGH,
config=config,
metadata=metadata
)
created_jobs.append({
'id': job_id,
'category': category,
'urls_count': len(urls)
})
print(f" ✅ Created job: {job_id}")
print(f" 📊 URLs: {len(urls)}")
print(f" 🎯 Category: {category}")
# Set up scheduling for automated monitoring
print(f"\n📅 Setting up Automated Monitoring...")
scheduler.start()
for job_info in created_jobs:
# Schedule different categories at different intervals
if 'news' in job_info['category']:
# News updates every 6 hours
scheduler.schedule_job(job_info['id'], "interval", interval=360)
print(f" ⏰ {job_info['category']}: Every 6 hours")
elif 'procurement' in job_info['category']:
# Procurement data daily
scheduler.schedule_job(job_info['id'], "daily", time="09:00")
print(f" ⏰ {job_info['category']}: Daily at 9:00 AM")
elif 'reports' in job_info['category']:
# Reports weekly
scheduler.schedule_job(job_info['id'], "weekly", day="monday", time="08:00")
print(f" ⏰ {job_info['category']}: Weekly on Monday at 8:00 AM")
else:
# Other data every 12 hours
scheduler.schedule_job(job_info['id'], "interval", interval=720)
print(f" ⏰ {job_info['category']}: Every 12 hours")
scheduler.stop()
# Execute one demonstration job
print(f"\n▶️ Running Demonstration Scrape...")
demo_job = created_jobs[0] # Corruption news job
try:
execution_id = await job_manager.execute_job(demo_job['id'])
print(f" 🏃 Started execution: {execution_id}")
# Wait for completion
import time
start_time = time.time()
while time.time() - start_time < 60: # 1 minute timeout
job = job_manager.get_job(demo_job['id'])
if job.status.value in ['completed', 'failed']:
break
await asyncio.sleep(2)
# Show results
job = job_manager.get_job(demo_job['id'])
print(f" 📊 Status: {job.status.value}")
print(f" 📈 Progress: {job.progress:.1f}%")
print(f" ✅ Successful: {job.successful_extractions}")
print(f" 📦 Data Extracted: {job.data_extracted}")
except Exception as e:
print(f" ❌ Demo execution error: {e}")
# Show summary
print(f"\n📊 Setup Summary:")
print(f" 📋 Total Jobs Created: {len(created_jobs)}")
print(f" 🎯 Categories Covered: {len(transparency_urls)}")
print(f" 🌐 Total URLs: {sum(job['urls_count'] for job in created_jobs)}")
print(f" ⏰ Automated Scheduling: Enabled")
print(f"\n🎯 Data Categories Configured:")
for job_info in created_jobs:
print(f" • {job_info['category'].replace('_', ' ').title()}")
print(f"\n🔗 Access via Dashboard:")
print(f" 🕷️ Scraping Control Panel → Manage Jobs")
print(f" 🎯 Filter by Type: Government Data")
print(f" 📊 Monitor progress and results in real-time")
print(f"\n🎉 Transparency International Pakistan scraping setup completed!")
print(f" All jobs are ready for automated execution and monitoring.")
return created_jobs
async def analyze_transparency_data(job_manager, job_id):
"""Analyze scraped transparency data"""
print(f"\n📊 Analyzing Transparency Data for Job: {job_id}")
job = job_manager.get_job(job_id)
if not job:
print(f"❌ Job {job_id} not found")
return
executions = job_manager.get_execution_history(job_id)
if not executions:
print(f"⚠️ No execution history found for job {job_id}")
return
latest_execution = executions[0]
print(f" 📋 Job: {job.name}")
print(f" 📊 Status: {job.status.value}")
print(f" 🌐 URLs Processed: {job.processed_urls}/{job.total_urls}")
print(f" 📦 Data Points: {job.data_extracted}")
# Sample analysis of corruption keywords
corruption_keywords = [
'corruption', 'transparency', 'accountability', 'governance',
'audit', 'investigation', 'fraud', 'bribery', 'embezzlement',
'misconduct', 'oversight', 'compliance', 'reform'
]
if latest_execution.output_files:
print(f" 📁 Output Files: {len(latest_execution.output_files)}")
# Simulate keyword analysis
keyword_matches = {}
for keyword in corruption_keywords:
# In real implementation, this would analyze actual scraped content
keyword_matches[keyword] = len(keyword) % 5 + 1 # Mock data
print(f"\n🔍 Content Analysis (Top Keywords):")
sorted_keywords = sorted(keyword_matches.items(), key=lambda x: x[1], reverse=True)
for keyword, count in sorted_keywords[:8]:
print(f" • {keyword}: {count} mentions")
return {
'job_info': {
'name': job.name,
'status': job.status.value,
'progress': job.progress,
'urls_processed': job.processed_urls,
'data_extracted': job.data_extracted
},
'analysis': {
'keyword_matches': keyword_matches if 'keyword_matches' in locals() else {},
'execution_count': len(executions),
'last_run': latest_execution.started_at if latest_execution else None
}
}
async def main():
"""Main function"""
print("🚀 Starting Transparency International Pakistan Scraper...")
try:
# Create the specialized scraping jobs
created_jobs = await create_transparency_pk_jobs()
# Show how to access in dashboard
print(f"\n💡 Next Steps:")
print(f" 1. Open Dashboard: http://localhost:8502")
print(f" 2. Login: admin/admin123")
print(f" 3. Navigate: 🕷️ Scraping Control Panel")
print(f" 4. View Jobs: Look for 'TI Pakistan' jobs")
print(f" 5. Monitor: Check progress and results")
print(f"\n🎯 The scraping jobs will automatically collect:")
print(f" 📰 Corruption news and updates")
print(f" 🏛️ Government procurement data")
print(f" 📊 Governance reports and assessments")
print(f" ⚖️ Compliance and regulatory information")
except Exception as e:
print(f"\n❌ Setup failed with error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(main())