|
| 1 | +import logging |
| 2 | +from pathlib import Path |
| 3 | +from typing import Dict, List |
| 4 | + |
| 5 | +from .. import utils |
| 6 | +from ..cache import Cache |
| 7 | +from ..platforms.muckrock import process_muckrock |
| 8 | + |
| 9 | +# from ..utils import MetadataDict |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class Site: |
| 15 | + """Scrape file metadata for the California Highway Patrol. |
| 16 | +
|
| 17 | + Attributes: |
| 18 | + name (str): The official name of the agency |
| 19 | + """ |
| 20 | + |
| 21 | + name = "California Highway Patrol" |
| 22 | + |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + data_dir: Path = utils.CLEAN_DATA_DIR, |
| 26 | + cache_dir: Path = utils.CLEAN_CACHE_DIR, |
| 27 | + ): |
| 28 | + """Initialize a new instance. |
| 29 | +
|
| 30 | + Args: |
| 31 | + data_dir (Path): The directory where downstream processed files/data will be saved |
| 32 | + cache_dir (Path): The directory where files will be cached |
| 33 | + """ |
| 34 | + self.site_slug = "ca_california_highway_patrol" |
| 35 | + self.base_url = "https://www.muckrock.com/foi/california-52/sb1421-records-86009" # Embargoed |
| 36 | + # Initial disclosure page (aka where they start complying with law) contains list of "detail"/child pages with links to the SB16/SB1421/AB748 videos and files |
| 37 | + # along with additional index pages |
| 38 | + self.data_dir = data_dir |
| 39 | + self.cache_dir = cache_dir |
| 40 | + self.subpages_dir = cache_dir / (self.site_slug + "/subpages") |
| 41 | + self.cache = Cache(cache_dir) |
| 42 | + for localdir in [self.cache_dir, self.data_dir, self.subpages_dir]: |
| 43 | + utils.create_directory(localdir) |
| 44 | + |
| 45 | + def scrape_meta(self, throttle: int = 2) -> Path: |
| 46 | + """Gather metadata on downloadable files (videos, etc.). |
| 47 | +
|
| 48 | + Args: |
| 49 | + throttle (int): Number of seconds to wait between requests. Defaults to 0. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + Path: Local path of JSON file containing metadata on downloadable files |
| 53 | + """ |
| 54 | + to_be_scraped: Dict = { |
| 55 | + "https://www.muckrock.com/foi/california-52/sb1421-records-86009": True, |
| 56 | + "https://www.muckrock.com/foi/california-52/sb1421-records-2022-122698": True, |
| 57 | + "https://www.muckrock.com/foi/california-52/2023-sb1421sb16-request-california-highway-patrol-138613": True, |
| 58 | + } |
| 59 | + |
| 60 | + metadata: List = [] |
| 61 | + |
| 62 | + subpages_dir = self.subpages_dir |
| 63 | + |
| 64 | + api_key = utils.get_credentials("MUCKROCK_CRP") |
| 65 | + |
| 66 | + for start_url in to_be_scraped: |
| 67 | + force = to_be_scraped[start_url] |
| 68 | + local_metadata = process_muckrock(subpages_dir, start_url, api_key, force) |
| 69 | + metadata.extend(local_metadata) |
| 70 | + |
| 71 | + json_filename = self.data_dir / (self.site_slug + ".json") |
| 72 | + self.cache.write_json(json_filename, metadata) |
| 73 | + |
| 74 | + return json_filename |
0 commit comments