Spaces:
Runtime error

File size: 14,130 Bytes
30be4c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""
Comprehensive Tamil Nadu Speed Camera Scraper
Uses smaller grid cells to capture ALL cameras like the map interface does
"""

import requests
import json
from typing import Dict, List, Any, Optional, Set
import time
from datetime import datetime


class ComprehensiveTNCameraScraper:
    """
    Improved scraper that uses smaller viewport bounds to get all cameras
    The map shows more cameras because it requests data for smaller areas at a time
    """
    
    def __init__(self):
        self.base_url = "https://www.scdb.info"
        self.api_endpoint = f"{self.base_url}/karte/"
        self.session = requests.Session()
        
        self.session.headers.update({
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
            'Accept': '*/*',
            'Accept-Language': 'en-US,en;q=0.9',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Origin': 'https://www.scdb.info',
            'Referer': 'https://www.scdb.info/en/karte/',
            'X-Requested-With': 'XMLHttpRequest',
        })
        
        self.all_cameras = {}  # Use dict to avoid duplicates by ID
    
    def get_cameras_by_bounds(
        self, 
        lat_min: float, 
        lat_max: float, 
        lng_min: float, 
        lng_max: float,
        show_progress: bool = True
    ) -> Optional[List[Dict[str, Any]]]:
        """Fetch cameras within geographical bounds"""
        
        form_data = {
            'xhr': '1',
            'action': 'all',
            'latMax': str(lat_max),
            'lngMax': str(lng_max),
            'latMin': str(lat_min),
            'lngMin': str(lng_min),
        }
        
        try:
            if show_progress:
                print(f"  Fetching: Lat[{lat_min:.3f}-{lat_max:.3f}] Lng[{lng_min:.3f}-{lng_max:.3f}]", end="")
            
            response = self.session.post(
                self.api_endpoint,
                data=form_data,
                timeout=15
            )
            
            response.raise_for_status()
            data = response.json()
            
            # Extract result array
            cameras = data.get('result', data) if isinstance(data, dict) else data
            
            if isinstance(cameras, list):
                if show_progress:
                    print(f" β†’ Found {len(cameras)} cameras")
                return cameras
            
            if show_progress:
                print(" β†’ No data")
            return []
            
        except Exception as e:
            if show_progress:
                print(f" β†’ Error: {e}")
            return []
    
    def scrape_chennai_comprehensive(self, grid_size: float = 0.02) -> Dict[int, Dict[str, Any]]:
        """
        Scrape Chennai area using a fine grid to capture all cameras
        
        Args:
            grid_size: Size of each grid cell in degrees (smaller = more comprehensive)
                      0.02 degrees β‰ˆ 2.2 km (recommended)
                      0.01 degrees β‰ˆ 1.1 km (very detailed, more requests)
        
        Chennai bounds: Lat 12.85-13.25, Lng 80.1-80.35
        """
        print("="*70)
        print(f"COMPREHENSIVE CHENNAI SCRAPER (Grid size: {grid_size}Β° β‰ˆ {grid_size*111:.1f} km)")
        print("="*70)
        
        # Chennai bounds (extended slightly for suburbs)
        lat_start, lat_end = 12.20, 13.60
        lng_start, lng_end = 79.60, 80.35
        
        total_cells = int((lat_end - lat_start) / grid_size) * int((lng_end - lng_start) / grid_size)
        current_cell = 0
        
        print(f"\nTotal grid cells to scan: {total_cells}")
        print("-"*70)
        
        lat = lat_start
        while lat < lat_end:
            lng = lng_start
            while lng < lng_end:
                current_cell += 1
                print(f"[{current_cell}/{total_cells}] ", end="")
                
                cameras = self.get_cameras_by_bounds(
                    lat_min=lat,
                    lat_max=lat + grid_size,
                    lng_min=lng,
                    lng_max=lng + grid_size
                )
                
                if cameras:
                    for camera in cameras:
                        if isinstance(camera, dict):
                            cam_id = camera.get('id')
                            if cam_id:
                                self.all_cameras[cam_id] = camera
                
                lng += grid_size
                time.sleep(0.5)  # Be respectful to server
            
            lat += grid_size
        
        print("\n" + "="*70)
        print(f"βœ“ Scraping complete! Found {len(self.all_cameras)} unique cameras")
        print("="*70)
        
        return self.all_cameras
    
    def scrape_tamil_nadu_comprehensive(self, grid_size: float = 0.1) -> Dict[int, Dict[str, Any]]:
        """
        Scrape entire Tamil Nadu using a grid
        
        Args:
            grid_size: Size of grid cells (0.1 = ~11 km, good for state-wide)
        
        Tamil Nadu bounds: Lat 8.0-13.5, Lng 76.2-80.35
        """
        print("="*70)
        print(f"COMPREHENSIVE TAMIL NADU SCRAPER (Grid size: {grid_size}Β° β‰ˆ {grid_size*111:.0f} km)")
        print("="*70)
        
        lat_start, lat_end = 8.0, 13.5
        lng_start, lng_end = 76.2, 80.35
        
        total_cells = int((lat_end - lat_start) / grid_size) * int((lng_end - lng_start) / grid_size)
        current_cell = 0
        
        print(f"\nTotal grid cells to scan: {total_cells}")
        print("⚠️  This will take approximately {:.0f} minutes".format(total_cells * 0.5 / 60))
        print("-"*70)
        
        lat = lat_start
        while lat < lat_end:
            lng = lng_start
            while lng < lng_end:
                current_cell += 1
                print(f"[{current_cell}/{total_cells}] ", end="")
                
                cameras = self.get_cameras_by_bounds(
                    lat_min=lat,
                    lat_max=lat + grid_size,
                    lng_min=lng,
                    lng_max=lng + grid_size
                )
                
                if cameras:
                    for camera in cameras:
                        if isinstance(camera, dict):
                            cam_id = camera.get('id')
                            if cam_id:
                                self.all_cameras[cam_id] = camera
                
                lng += grid_size
                time.sleep(0.5)
            
            lat += grid_size
        
        print("\n" + "="*70)
        print(f"βœ“ Scraping complete! Found {len(self.all_cameras)} unique cameras")
        print("="*70)
        
        return self.all_cameras
    
    def extract_coordinates(self, cameras: Dict[int, Dict[str, Any]]) -> List[Dict[str, Any]]:
        """Extract clean coordinate data"""
        extracted = []
        
        for cam_id, camera in cameras.items():
            camera_data = {
                'id': camera.get('id'),
                'latitude': camera.get('lat') or camera.get('breitengrad_dezimal'),
                'longitude': camera.get('lng') or camera.get('laengengrad_dezimal'),
                'city': camera.get('ort', ''),
                'state': camera.get('bundesland', '') or 'Tamil Nadu',
                'street': camera.get('strasse', ''),
                'postal_code': camera.get('plz', ''),
                'country': camera.get('land', 'IND'),
                'type': camera.get('type', ''),
                'camera_type': camera.get('art', ''),
                'speed_limit': camera.get('vmax', ''),
                'direction': camera.get('richtung', ''),
                'status': camera.get('status', ''),
                'rotatable': bool(camera.get('drehbar', 0)),
                'gps_status': camera.get('gps_status', ''),
            }
            
            if camera_data['latitude'] and camera_data['longitude']:
                extracted.append(camera_data)
        
        return extracted
    
    def save_all_formats(self, prefix: str = "tn"):
        """Save data in multiple formats"""
        
        cameras_list = list(self.all_cameras.values())
        coordinates = self.extract_coordinates(self.all_cameras)
        
        # 1. Detailed JSON with metadata
        detailed_output = {
            'metadata': {
                'total_cameras': len(coordinates),
                'region': 'Chennai, Tamil Nadu' if 'chennai' in prefix else 'Tamil Nadu',
                'country': 'India',
                'extracted_at': datetime.now().isoformat(),
                'source': 'scdb.info',
                'scraping_method': 'comprehensive_grid'
            },
            'cameras': coordinates
        }
        
        with open(f"{prefix}_cameras_detailed.json", 'w', encoding='utf-8') as f:
            json.dump(detailed_output, f, indent=2, ensure_ascii=False)
        print(f"βœ“ Saved: {prefix}_cameras_detailed.json")
        
        # 2. Simple coordinates
        simple_coords = [
            {
                'id': cam['id'],
                'lat': cam['latitude'],
                'lng': cam['longitude'],
                'location': f"{cam['street']}, {cam['city']}".strip(', '),
                'type': cam['type']
            }
            for cam in coordinates
        ]
        
        with open(f"{prefix}_cameras_simple.json", 'w', encoding='utf-8') as f:
            json.dump(simple_coords, f, indent=2, ensure_ascii=False)
        print(f"βœ“ Saved: {prefix}_cameras_simple.json")
        
        # 3. GeoJSON
        features = [
            {
                'type': 'Feature',
                'geometry': {
                    'type': 'Point',
                    'coordinates': [cam['longitude'], cam['latitude']]
                },
                'properties': {
                    'id': cam['id'],
                    'name': cam['street'] or 'Unknown',
                    'city': cam['city'],
                    'type': cam['type'],
                    'speed_limit': cam['speed_limit'],
                    'status': cam['status']
                }
            }
            for cam in coordinates
        ]
        
        geojson = {
            'type': 'FeatureCollection',
            'features': features
        }
        
        with open(f"{prefix}_cameras.geojson", 'w', encoding='utf-8') as f:
            json.dump(geojson, f, indent=2, ensure_ascii=False)
        print(f"βœ“ Saved: {prefix}_cameras.geojson")
        
        # 4. CSV
        import csv
        
        fields = ['id', 'latitude', 'longitude', 'street', 'city', 'postal_code', 
                  'type', 'camera_type', 'speed_limit', 'direction', 'status']
        
        with open(f"{prefix}_cameras.csv", 'w', newline='', encoding='utf-8') as f:
            writer = csv.DictWriter(f, fieldnames=fields)
            writer.writeheader()
            for cam in coordinates:
                row = {field: cam.get(field, '') for field in fields}
                writer.writerow(row)
        print(f"βœ“ Saved: {prefix}_cameras.csv")
    
    def print_statistics(self):
        """Print statistics about scraped cameras"""
        
        if not self.all_cameras:
            print("No cameras to analyze")
            return
        
        coordinates = self.extract_coordinates(self.all_cameras)
        
        print("\n" + "="*70)
        print("STATISTICS")
        print("="*70)
        
        print(f"\nπŸ“Š Total Cameras: {len(coordinates)}")
        
        # By type
        types = {}
        for cam in coordinates:
            cam_type = cam['type']
            types[cam_type] = types.get(cam_type, 0) + 1
        
        print("\nπŸŽ₯ Camera Types:")
        for cam_type, count in sorted(types.items(), key=lambda x: x[1], reverse=True):
            print(f"   {cam_type}: {count}")
        
        # By city
        cities = {}
        for cam in coordinates:
            city = cam['city']
            if city:
                cities[city] = cities.get(city, 0) + 1
        
        print("\nπŸ™οΈ  Top Cities:")
        for city, count in sorted(cities.items(), key=lambda x: x[1], reverse=True)[:10]:
            print(f"   {city}: {count}")
        
        # By status
        statuses = {}
        for cam in coordinates:
            status = cam['status']
            statuses[status] = statuses.get(status, 0) + 1
        
        print("\nβœ… Camera Status:")
        status_names = {'A': 'Active', 'L': 'Empty', 'Z': 'Destroyed'}
        for status, count in statuses.items():
            status_name = status_names.get(status, status)
            print(f"   {status_name}: {count}")


def main():
    """Main scraper function"""
    
    scraper = ComprehensiveTNCameraScraper()
    
    print("\n🎯 COMPREHENSIVE CAMERA SCRAPER")
    print("Choose an option:")
    print("1. Chennai only (0.02Β° grid β‰ˆ 2.2 km, ~5-10 minutes)")
    print("2. Chennai detailed (0.01Β° grid β‰ˆ 1.1 km, ~20-30 minutes, most comprehensive)")
    print("3. Entire Tamil Nadu (0.1Β° grid β‰ˆ 11 km, ~30-45 minutes)")
    
    # For automatic execution, run Chennai comprehensive
    print("\nπŸš€ Running: Chennai Comprehensive (0.02Β° grid)\n")
    
    cameras = scraper.scrape_chennai_comprehensive(grid_size=0.02)
    
    if cameras:
        scraper.print_statistics()
        LOCATION_PREFIX = "chennai"
        print("\nπŸ’Ύ Saving files...")
        scraper.save_all_formats(prefix=f"{LOCATION_PREFIX}_complete")
        
        print("\n" + "="*70)
        print("βœ… SCRAPING COMPLETE!")
        print("="*70)
        print(f"\nTotal cameras found: {len(cameras)}")
        print("\nFiles created:")
        print(f"  β€’ {LOCATION_PREFIX}_complete_cameras_detailed.json")
        print(f"  β€’ {LOCATION_PREFIX}_complete_cameras_simple.json")
        print(f"  β€’ {LOCATION_PREFIX}_complete_cameras.geojson")
        print(f"  β€’ {LOCATION_PREFIX}_complete_cameras.csv")
    else:
        print("\n❌ No cameras found!")


if __name__ == "__main__":
    main()