File size: 26,559 Bytes
61d29fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
#!/usr/bin/env python3
"""
OpenStates Document Downloader

Downloads bill documents and versions from the local PostgreSQL OpenStates database.
Supports incremental reruns and recovery from failures.

Directory Structure:
    /mnt/d/openstates_documents/  (or configurable base path)
    β”œβ”€β”€ versions/
    β”‚   β”œβ”€β”€ AL/
    β”‚   β”‚   β”œβ”€β”€ 2025/
    β”‚   β”‚   β”‚   β”œβ”€β”€ www.legislature.state.al.us_...pdf
    β”‚   β”‚   β”‚   └── ...
    β”‚   β”‚   └── 2026/
    β”‚   β”œβ”€β”€ CA/
    β”‚   β”‚   β”œβ”€β”€ 2025/
    β”‚   β”‚   └── 2026/
    β”‚   └── ...
    β”œβ”€β”€ documents/
    β”‚   β”œβ”€β”€ MA/
    β”‚   β”‚   β”œβ”€β”€ 2025/
    β”‚   β”‚   β”‚   β”œβ”€β”€ malegislature.gov_...pdf
    β”‚   β”‚   β”‚   └── ...
    β”‚   β”‚   └── 2026/
    β”‚   └── ...
    └── download_log.json

Database: PostgreSQL OpenStates data at localhost:5433
Total Documents: ~4.5 million (3.5M versions + 1M documents)

Usage:
    # Download everything
    python download_documents.py

    # Custom database connection
    python download_documents.py --db-url "postgresql://user:pass@localhost:5433/openstates"

    # Custom base directory
    python download_documents.py --base-dir /mnt/d/openstates_docs

    # Download only specific years
    python download_documents.py --years 2024,2025

    # Download only documents (not versions)
    python download_documents.py --type documents

    # Limit number of files to download
    python download_documents.py --limit 1000

    # Resume interrupted download
    python download_documents.py --resume

    # Dry run
    python download_documents.py --dry-run
"""

import argparse
import json
import psycopg2
import requests
import re
import sys
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from datetime import datetime
from loguru import logger
import time
from tqdm import tqdm
from urllib.parse import urlparse


class OpenStatesDocumentDownloader:
    """Download OpenStates documents from PostgreSQL database"""
    
    def __init__(
        self,
        base_dir: Path,
        db_url: str = "postgresql://postgres:password@localhost:5433/openstates",
        resume: bool = False
    ):
        """
        Initialize OpenStates document downloader
        
        Args:
            base_dir: Base directory for downloads
            db_url: PostgreSQL connection URL
            resume: Resume interrupted downloads
        """
        self.base_dir = Path(base_dir)
        self.versions_dir = self.base_dir / "versions"
        self.documents_dir = self.base_dir / "documents"
        self.log_file = self.base_dir / "download_log.json"
        self.db_url = db_url
        self.resume = resume
        
        # Create directories
        self.base_dir.mkdir(parents=True, exist_ok=True)
        self.versions_dir.mkdir(exist_ok=True)
        self.documents_dir.mkdir(exist_ok=True)
        
        # Load download log
        self.download_log = self._load_log()
        
        # Session for HTTP requests
        self.session = requests.Session()
        self.session.headers.update({
            'User-Agent': 'Mozilla/5.0 (OpenStates Document Downloader/1.0)'
        })
        
        # Database connection
        self.conn = None
    
    def _load_log(self) -> Dict:
        """Load download log"""
        if self.log_file.exists():
            with open(self.log_file) as f:
                return json.load(f)
        return {
            'started': datetime.now().isoformat(),
            'last_updated': None,
            'completed_files': {},
            'failed_files': {},
            'stats': {
                'versions': {'downloaded': 0, 'failed': 0, 'skipped': 0},
                'documents': {'downloaded': 0, 'failed': 0, 'skipped': 0}
            }
        }
    
    def _save_log(self):
        """Save download log"""
        self.download_log['last_updated'] = datetime.now().isoformat()
        with open(self.log_file, 'w') as f:
            json.dump(self.download_log, f, indent=2)
    
    def _connect_db(self):
        """Connect to PostgreSQL database"""
        if not self.conn or self.conn.closed:
            logger.info(f"πŸ”Œ Connecting to database...")
            self.conn = psycopg2.connect(self.db_url)
    
    def _close_db(self):
        """Close database connection"""
        if self.conn and not self.conn.closed:
            self.conn.close()
    
    def _sanitize_filename(self, text: str) -> str:
        """
        Convert text to safe filename in snake_case
        
        Args:
            text: Text to sanitize
        
        Returns:
            Safe filename string
        """
        # Remove/replace unsafe characters
        text = str(text).lower()
        # Replace spaces and special chars with underscores
        text = re.sub(r'[^\w\s-]', '', text)
        text = re.sub(r'[\s-]+', '_', text)
        # Remove leading/trailing underscores
        text = text.strip('_')
        # Limit length
        if len(text) > 200:
            text = text[:200]
        return text or 'unknown'
    
    def _url_to_filename(self, url: str) -> str:
        """
        Convert URL to safe filename
        
        Removes http://, https://, and converts special characters to underscores
        to create a unique filename based on the URL.
        
        Args:
            url: Document URL
        
        Returns:
            Safe filename string (without extension)
        """
        # Remove protocol
        filename = url.replace('https://', '').replace('http://', '')
        
        # Remove trailing slashes
        filename = filename.rstrip('/')
        
        # Replace special characters with underscores
        # Keep alphanumeric, dots, and hyphens
        filename = re.sub(r'[^\w\.-]', '_', filename)
        
        # Replace multiple underscores with single
        filename = re.sub(r'_+', '_', filename)
        
        # Remove leading/trailing underscores
        filename = filename.strip('_')
        
        # Limit length (leave room for extension)
        if len(filename) > 200:
            # Keep start and end of URL for uniqueness
            filename = filename[:100] + '_' + filename[-99:]
        
        return filename or 'document'
    
    def _get_file_extension(self, url: str, media_type: Optional[str]) -> str:
        """
        Determine file extension from URL or media type
        
        Args:
            url: Document URL
            media_type: MIME type
        
        Returns:
            File extension with dot (e.g., '.pdf')
        """
        # Try to get from URL first
        parsed = urlparse(url)
        path = parsed.path.lower()
        
        # Common extensions
        for ext in ['.pdf', '.html', '.htm', '.doc', '.docx', '.txt', '.xml']:
            if path.endswith(ext):
                return ext
        
        # Try media type
        if media_type:
            media_map = {
                'application/pdf': '.pdf',
                'text/html': '.html',
                'text/plain': '.txt',
                'application/msword': '.doc',
                'application/vnd.openxmlformats-officedocument.wordprocessingml.document': '.docx',
                'application/xml': '.xml',
                'text/xml': '.xml',
            }
            return media_map.get(media_type.lower(), '.pdf')
        
        # Default to PDF (most common for legislative documents)
        return '.pdf'
    
    def _is_downloaded(self, url: str, dest_path: Path) -> bool:
        """Check if file is already downloaded"""
        if not self.resume:
            return False
        
        if not dest_path.exists():
            return False
        
        # Check if in completed log
        if url in self.download_log['completed_files']:
            file_info = self.download_log['completed_files'][url]
            if dest_path.stat().st_size == file_info.get('size', 0):
                return True
        
        return False
    
    def _download_file(
        self,
        url: str,
        dest_path: Path,
        description: str,
        doc_type: str
    ) -> bool:
        """
        Download a single file
        
        Args:
            url: URL to download
            dest_path: Destination file path
            description: Description for progress bar
            doc_type: 'versions' or 'documents' for stats tracking
        
        Returns:
            True if successful, False otherwise
        """
        if self._is_downloaded(url, dest_path):
            self.download_log['stats'][doc_type]['skipped'] += 1
            return True
        
        try:
            # Download with timeout
            response = self.session.get(url, stream=True, timeout=30, allow_redirects=True)
            response.raise_for_status()
            
            # Create parent directory
            dest_path.parent.mkdir(parents=True, exist_ok=True)
            
            # Get total size
            total_size = int(response.headers.get('content-length', 0))
            
            # Download
            with open(dest_path, 'wb') as f:
                if total_size > 0:
                    with tqdm(
                        total=total_size,
                        unit='B',
                        unit_scale=True,
                        desc=description,
                        leave=False
                    ) as pbar:
                        for chunk in response.iter_content(chunk_size=8192):
                            if chunk:
                                f.write(chunk)
                                pbar.update(len(chunk))
                else:
                    # No content-length header
                    for chunk in response.iter_content(chunk_size=8192):
                        if chunk:
                            f.write(chunk)
            
            # Log success
            file_size = dest_path.stat().st_size
            self.download_log['completed_files'][url] = {
                'path': str(dest_path),
                'size': file_size,
                'downloaded_at': datetime.now().isoformat(),
            }
            self.download_log['stats'][doc_type]['downloaded'] += 1
            
            # Save log periodically (every 100 downloads)
            if self.download_log['stats'][doc_type]['downloaded'] % 100 == 0:
                self._save_log()
            
            return True
            
        except requests.exceptions.HTTPError as e:
            # Handle 403/404 as "not available"
            if e.response.status_code in [403, 404]:
                self.download_log['failed_files'][url] = {
                    'error': f"HTTP {e.response.status_code}: Not available",
                    'failed_at': datetime.now().isoformat(),
                }
                self.download_log['stats'][doc_type]['failed'] += 1
                return False
            else:
                logger.warning(f"⚠️  HTTP {e.response.status_code} for {url[:60]}...")
                self.download_log['failed_files'][url] = {
                    'error': str(e),
                    'failed_at': datetime.now().isoformat(),
                }
                self.download_log['stats'][doc_type]['failed'] += 1
                return False
                
        except Exception as e:
            logger.debug(f"Failed: {url[:60]}... - {e}")
            self.download_log['failed_files'][url] = {
                'error': str(e),
                'failed_at': datetime.now().isoformat(),
            }
            self.download_log['stats'][doc_type]['failed'] += 1
            return False
    
    def download_bill_versions(
        self,
        years: Optional[List[int]] = None,
        states: Optional[List[str]] = None,
        limit: Optional[int] = None,
        dry_run: bool = False
    ) -> Tuple[int, int, int]:
        """
        Download bill version documents
        
        Args:
            years: Optional list of years to download
            states: Optional list of state codes (e.g., ['CA', 'TX']) to download
            limit: Optional limit on number of files
            dry_run: If True, only show what would be downloaded
        
        Returns:
            Tuple of (successful, skipped, failed) counts
        """
        self._connect_db()
        cur = self.conn.cursor()
        
        # Build query
        query = """
            SELECT 
                bv.id,
                bv.note,
                bl.url,
                bl.media_type,
                b.identifier as bill_id,
                EXTRACT(YEAR FROM b.created_at)::int as year,
                ls.jurisdiction_id,
                SUBSTRING(ls.jurisdiction_id FROM 'ocd-jurisdiction/country:us/state:([a-z]{2})') as state
            FROM opencivicdata_billversion bv
            JOIN opencivicdata_billversionlink bl ON bl.version_id = bv.id
            JOIN opencivicdata_bill b ON bv.bill_id = b.id
            JOIN opencivicdata_legislativesession ls ON b.legislative_session_id = ls.id
            WHERE bl.url IS NOT NULL
        """
        
        params = []
        if years:
            query += " AND EXTRACT(YEAR FROM b.created_at) = ANY(%s)"
            params.append(years)
        
        if states:
            query += " AND SUBSTRING(ls.jurisdiction_id FROM 'ocd-jurisdiction/country:us/state:([a-z]{2})') = ANY(%s)"
            params.append([s.lower() for s in states])
        
        query += " ORDER BY state, year DESC, b.identifier"
        
        if limit:
            query += f" LIMIT {limit}"
        
        logger.info(f"πŸ“₯ Querying bill versions from database...")
        cur.execute(query, params)
        
        results = cur.fetchall()
        total = len(results)
        
        logger.info(f"πŸ“Š Found {total:,} bill version documents")
        
        if dry_run:
            logger.info("πŸ” DRY RUN - Sample documents that would be downloaded:")
            for row in results[:10]:
                version_id, note, url, media_type, bill_id, year, jurisdiction_id, state = row
                state_code = (state or 'unknown').upper()
                filename = self._url_to_filename(url)
                ext = self._get_file_extension(url, media_type)
                # Avoid double extensions
                if not filename.lower().endswith(ext.lower()):
                    filename = f"{filename}{ext}"
                print(f"  {state_code}/{year}/{filename}")
            if total > 10:
                print(f"  ... and {total - 10:,} more")
            return 0, 0, 0
        
        # Download files
        successful = 0
        failed = 0
        skipped = 0
        
        logger.info(f"⬇️  Downloading {total:,} bill versions...")
        
        for i, row in enumerate(results, 1):
            version_id, note, url, media_type, bill_id, year, jurisdiction_id, state = row
            
            # Create filename from URL to avoid overlaps across states
            filename = self._url_to_filename(url)
            ext = self._get_file_extension(url, media_type)
            
            # Avoid double extensions if URL already contains extension
            if not filename.lower().endswith(ext.lower()):
                filename = f"{filename}{ext}"
            
            # Create state/year directory structure
            state_code = (state or 'unknown').upper()
            state_dir = self.versions_dir / state_code / str(year)
            dest_path = state_dir / filename
            
            # Check if already downloaded
            if self._is_downloaded(url, dest_path):
                skipped += 1
                if i % 1000 == 0:
                    logger.info(f"  Progress: {i:,}/{total:,} ({i/total*100:.1f}%) - βœ… {successful:,} | ⏭️ {skipped:,} | ❌ {failed:,}")
                continue
            
            # Download
            description = f"[{i}/{total}] {bill_id} ({year})"
            success = self._download_file(url, dest_path, description, 'versions')
            
            if success:
                successful += 1
            else:
                failed += 1
            
            # Progress update every 1000 files
            if i % 1000 == 0:
                logger.info(f"  Progress: {i:,}/{total:,} ({i/total*100:.1f}%) - βœ… {successful:,} | ⏭️ {skipped:,} | ❌ {failed:,}")
                self._save_log()
            
            # No rate limiting - download as fast as possible
            # time.sleep(0.1)  # Removed for speed
        
        # Final save
        self._save_log()
        
        cur.close()
        return successful, skipped, failed
    
    def download_bill_documents(
        self,
        years: Optional[List[int]] = None,
        states: Optional[List[str]] = None,
        limit: Optional[int] = None,
        dry_run: bool = False
    ) -> Tuple[int, int, int]:
        """
        Download bill documents (fiscal notes, committee statements, etc.)
        
        Args:
            years: Optional list of years to download
            states: Optional list of state codes (e.g., ['CA', 'TX']) to download
            limit: Optional limit on number of files
            dry_run: If True, only show what would be downloaded
        
        Returns:
            Tuple of (successful, skipped, failed) counts
        """
        self._connect_db()
        cur = self.conn.cursor()
        
        # Build query
        query = """
            SELECT 
                bd.id,
                bd.note,
                bdl.url,
                bdl.media_type,
                b.identifier as bill_id,
                EXTRACT(YEAR FROM b.created_at)::int as year,
                ls.jurisdiction_id,
                SUBSTRING(ls.jurisdiction_id FROM 'ocd-jurisdiction/country:us/state:([a-z]{2})') as state
            FROM opencivicdata_billdocument bd
            JOIN opencivicdata_billdocumentlink bdl ON bdl.document_id = bd.id
            JOIN opencivicdata_bill b ON bd.bill_id = b.id
            JOIN opencivicdata_legislativesession ls ON b.legislative_session_id = ls.id
            WHERE bdl.url IS NOT NULL
        """
        
        params = []
        if years:
            query += " AND EXTRACT(YEAR FROM b.created_at) = ANY(%s)"
            params.append(years)
        
        if states:
            query += " AND SUBSTRING(ls.jurisdiction_id FROM 'ocd-jurisdiction/country:us/state:([a-z]{2})') = ANY(%s)"
            params.append([s.lower() for s in states])
        
        query += " ORDER BY state, year DESC, b.identifier"
        
        if limit:
            query += f" LIMIT {limit}"
        
        logger.info(f"πŸ“₯ Querying bill documents from database...")
        cur.execute(query, params)
        
        results = cur.fetchall()
        total = len(results)
        
        logger.info(f"πŸ“Š Found {total:,} bill documents")
        
        if dry_run:
            logger.info("πŸ” DRY RUN - Sample documents that would be downloaded:")
            for row in results[:10]:
                doc_id, note, url, media_type, bill_id, year, jurisdiction_id, state = row
                state_code = (state or 'unknown').upper()
                filename = self._url_to_filename(url)
                ext = self._get_file_extension(url, media_type)
                # Avoid double extensions
                if not filename.lower().endswith(ext.lower()):
                    filename = f"{filename}{ext}"
                print(f"  {state_code}/{year}/{filename}")
            if total > 10:
                print(f"  ... and {total - 10:,} more")
            return 0, 0, 0
        
        # Download files
        successful = 0
        failed = 0
        skipped = 0
        
        logger.info(f"⬇️  Downloading {total:,} bill documents...")
        
        for i, row in enumerate(results, 1):
            doc_id, note, url, media_type, bill_id, year, jurisdiction_id, state = row
            
            # Create filename from URL to avoid overlaps across states
            filename = self._url_to_filename(url)
            ext = self._get_file_extension(url, media_type)
            
            # Avoid double extensions if URL already contains extension
            if not filename.lower().endswith(ext.lower()):
                filename = f"{filename}{ext}"
            
            # Create state/year directory structure
            state_code = (state or 'unknown').upper()
            state_dir = self.documents_dir / state_code / str(year)
            dest_path = state_dir / filename
            
            # Check if already downloaded
            if self._is_downloaded(url, dest_path):
                skipped += 1
                if i % 1000 == 0:
                    logger.info(f"  Progress: {i:,}/{total:,} ({i/total*100:.1f}%) - βœ… {successful:,} | ⏭️ {skipped:,} | ❌ {failed:,}")
                continue
            
            # Download
            description = f"[{i}/{total}] {bill_id} ({year})"
            success = self._download_file(url, dest_path, description, 'documents')
            
            if success:
                successful += 1
            else:
                failed += 1
            
            # Progress update every 1000 files
            if i % 1000 == 0:
                logger.info(f"  Progress: {i:,}/{total:,} ({i/total*100:.1f}%) - βœ… {successful:,} | ⏭️ {skipped:,} | ❌ {failed:,}")
                self._save_log()
            
            # No rate limiting - download as fast as possible
            # time.sleep(0.1)  # Removed for speed
        
        # Final save
        self._save_log()
        
        cur.close()
        return successful, skipped, failed


def main():
    """Main entry point"""
    parser = argparse.ArgumentParser(
        description='Download OpenStates documents from PostgreSQL database',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Download everything
  python download_documents.py

  # Download only 2024-2025 documents
  python download_documents.py --years 2024,2025

  # Download only bill documents (not versions)
  python download_documents.py --type documents

  # Download limited number for testing
  python download_documents.py --limit 100 --dry-run

  # Resume interrupted download
  python download_documents.py --resume

  # Custom database and directory
  python download_documents.py --db-url "postgresql://user:pass@host:port/db" --base-dir /mnt/d/docs
        """
    )
    
    parser.add_argument(
        '--base-dir',
        type=str,
        default='/mnt/d/openstates_documents',
        help='Base directory for downloads (default: /mnt/d/openstates_documents)'
    )
    
    parser.add_argument(
        '--db-url',
        type=str,
        default='postgresql://postgres:password@localhost:5433/openstates',
        help='PostgreSQL connection URL'
    )
    
    parser.add_argument(
        '--type',
        type=str,
        choices=['all', 'versions', 'documents'],
        default='all',
        help='Which document type to download (default: all)'
    )
    
    parser.add_argument(
        '--years',
        type=str,
        help='Comma-separated list of years (e.g., 2024,2025)'
    )
    
    parser.add_argument(
        '--states',
        type=str,
        help='Comma-separated list of state codes (e.g., CA,TX,NY) to download specific states only'
    )
    
    parser.add_argument(
        '--limit',
        type=int,
        help='Limit number of files to download (useful for testing)'
    )
    
    parser.add_argument(
        '--resume',
        action='store_true',
        help='Resume interrupted download'
    )
    
    parser.add_argument(
        '--dry-run',
        action='store_true',
        help='Show what would be downloaded without actually downloading'
    )
    
    args = parser.parse_args()
    
    # Parse years
    years = None
    if args.years:
        years = [int(y.strip()) for y in args.years.split(',')]
    
    # Parse states
    states = None
    if args.states:
        states = [s.strip().upper() for s in args.states.split(',')]
    
    # Configure logging
    logger.remove()
    logger.add(
        sys.stderr,
        format="<green>{time:HH:mm:ss}</green> | <level>{level:8}</level> | <level>{message}</level>",
        level="INFO"
    )
    
    # Create downloader
    downloader = OpenStatesDocumentDownloader(
        base_dir=Path(args.base_dir),
        db_url=args.db_url,
        resume=args.resume
    )
    
    logger.info("πŸ“„ OpenStates Document Downloader")
    logger.info(f"πŸ“ Base directory: {args.base_dir}")
    logger.info(f"πŸ”Œ Database: {args.db_url.split('@')[1] if '@' in args.db_url else args.db_url}")
    logger.info("")
    
    # Download based on type
    total_successful = 0
    total_skipped = 0
    total_failed = 0
    
    try:
        if args.type in ['all', 'versions']:
            logger.info("=" * 60)
            logger.info("πŸ“₯ Downloading Bill Versions")
            logger.info("=" * 60)
            s, sk, f = downloader.download_bill_versions(
                years=years,
                states=states,
                limit=args.limit,
                dry_run=args.dry_run
            )
            total_successful += s
            total_skipped += sk
            total_failed += f
        
        if args.type in ['all', 'documents']:
            logger.info("\n" + "=" * 60)
            logger.info("πŸ“₯ Downloading Bill Documents")
            logger.info("=" * 60)
            s, sk, f = downloader.download_bill_documents(
                years=years,
                states=states,
                limit=args.limit,
                dry_run=args.dry_run
            )
            total_successful += s
            total_skipped += sk
            total_failed += f
        
        # Summary
        if not args.dry_run:
            logger.info("\n" + "=" * 60)
            logger.info("πŸ“Š Download Summary:")
            logger.info(f"  βœ… Successful: {total_successful:,}")
            logger.info(f"  ⏭️  Skipped: {total_skipped:,}")
            logger.info(f"  ❌ Failed: {total_failed:,}")
            logger.info(f"  πŸ“ Base directory: {args.base_dir}")
            logger.info("=" * 60)
    
    finally:
        # Close database connection
        downloader._close_db()


if __name__ == '__main__':
    main()