|
| 1 | +import os |
| 2 | +import time |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from bs4 import BeautifulSoup |
| 6 | +from dotenv import load_dotenv |
| 7 | + |
| 8 | +from .. import utils |
| 9 | +from ..cache import Cache |
| 10 | + |
| 11 | + |
| 12 | +class Site: |
| 13 | + """Scrape file metadata and download files for the City of Lake County Sheriff. |
| 14 | +
|
| 15 | + Attributes: |
| 16 | + name (str): The official name of the agency |
| 17 | + """ |
| 18 | + |
| 19 | + name = "Lake County Sheriff" |
| 20 | + |
| 21 | + def __init__( |
| 22 | + self, |
| 23 | + data_dir: Path = utils.CLEAN_DATA_DIR, |
| 24 | + cache_dir: Path = utils.CLEAN_CACHE_DIR, |
| 25 | + ): |
| 26 | + """Initialize a new instance. |
| 27 | +
|
| 28 | + Args: |
| 29 | + data_dir (Path): The directory where downstream processed files/data will be saved |
| 30 | + cache_dir (Path): The directory where files will be cached |
| 31 | + """ |
| 32 | + self.base_url = "https://www.lakesheriff.com/969/Use-of-Force" |
| 33 | + self.zenrows_api_url = "https://api.zenrows.com/v1/" |
| 34 | + self.data_dir = data_dir |
| 35 | + self.cache_dir = cache_dir |
| 36 | + self.cache = Cache(cache_dir) |
| 37 | + dotenv_path = "env/.env" |
| 38 | + load_dotenv(dotenv_path=dotenv_path) |
| 39 | + self.params = { |
| 40 | + "apikey": os.getenv("ZENROWS_KEY"), |
| 41 | + "url": "", # Target website URL |
| 42 | + # Add any other ZenRows parameters here (optional) |
| 43 | + } |
| 44 | + |
| 45 | + @property |
| 46 | + def agency_slug(self) -> str: |
| 47 | + """Construct the agency slug.""" |
| 48 | + # Use module path to construct agency slug, which we'll use downstream |
| 49 | + mod = Path(__file__) |
| 50 | + state_postal = mod.parent.stem |
| 51 | + return f"{state_postal}_{mod.stem}" # ca_lake_county_sheriff |
| 52 | + |
| 53 | + def scrape_meta(self, throttle=0): |
| 54 | + # construct a local filename relative to the cache directory - agency slug + page url (ca_lake_county_sheriff/Use-of-Force.html) |
| 55 | + # download the page (if not already cached) |
| 56 | + # save the index page url to cache (sensible name) |
| 57 | + base_name = f"{self.base_url.split('/')[-1]}.html" |
| 58 | + filename = f"{self.agency_slug}/{base_name}" |
| 59 | + self.params["url"] = self.base_url |
| 60 | + self.cache.download(filename, self.zenrows_api_url, params=self.params) |
| 61 | + metadata = [] |
| 62 | + child_pages = [] |
| 63 | + html = self.cache.read(filename) |
| 64 | + soup = BeautifulSoup(html, "html.parser") |
| 65 | + body = soup.find("table", class_="fr-alternate-rows") |
| 66 | + child_links = body.find_all("a") |
| 67 | + for link in child_links: |
| 68 | + tr_tag = link.find_parent("tr") |
| 69 | + td_tag = tr_tag.find_all("td") |
| 70 | + child_page_data = dict() |
| 71 | + child_page_data["date"] = td_tag[0].text |
| 72 | + child_page_data["location"] = td_tag[1].get_text(separator=", ") |
| 73 | + child_page_data["name"] = td_tag[2].text |
| 74 | + child_page_data["incident_type"] = td_tag[3].abbr.text |
| 75 | + child_page_data["case_number"] = link.text |
| 76 | + child_file_name = ( |
| 77 | + f'{self.agency_slug}/{child_page_data["case_number"]}.html' |
| 78 | + ) |
| 79 | + if link["href"]: |
| 80 | + link_url = f"https://www.lakesheriff.com{link['href']}" |
| 81 | + self.params["url"] = link_url |
| 82 | + self.cache.download( |
| 83 | + child_file_name, self.zenrows_api_url, params=self.params |
| 84 | + ) |
| 85 | + child_page_data["page_filename"] = child_file_name |
| 86 | + child_pages.append(child_page_data) |
| 87 | + time.sleep(throttle) |
| 88 | + for child_page in child_pages: |
| 89 | + html = self.cache.read(child_page["page_filename"]) |
| 90 | + soup = BeautifulSoup(html, "html.parser") |
| 91 | + body = soup.find(attrs={"data-cprole": "mainContentContainer"}) |
| 92 | + links = body.find_all("a") |
| 93 | + for link in links: |
| 94 | + link_href = link.get("href", None) |
| 95 | + if link_href: |
| 96 | + if "youtu" in link_href: |
| 97 | + payload = { |
| 98 | + "asset_url": link_href, |
| 99 | + "case_id": child_page["case_number"], |
| 100 | + "name": link.text, |
| 101 | + "title": link.text, |
| 102 | + "parent_page": str(child_page["page_filename"]), |
| 103 | + "details": { |
| 104 | + "date": child_page["date"], |
| 105 | + "location": child_page["location"], |
| 106 | + "name": child_page["name"], |
| 107 | + "incident_type": child_page["incident_type"], |
| 108 | + }, |
| 109 | + } |
| 110 | + metadata.append(payload) |
| 111 | + elif "DocumentCenter" in link_href: |
| 112 | + payload = { |
| 113 | + "asset_url": f"https://www.lakesheriff.com{link_href}", |
| 114 | + "case_id": child_page["case_number"], |
| 115 | + "name": link.text, |
| 116 | + "title": link.text, |
| 117 | + "parent_page": str(child_page["page_filename"]), |
| 118 | + "details": { |
| 119 | + "date": child_page["date"], |
| 120 | + "location": child_page["location"], |
| 121 | + "name": child_page["name"], |
| 122 | + "incident_type": child_page["incident_type"], |
| 123 | + }, |
| 124 | + } |
| 125 | + metadata.append(payload) |
| 126 | + elif "gallery" in link_href: |
| 127 | + gallery_id = link_href.split("=")[-1] |
| 128 | + galley_link = f"https://www.lakesheriff.com/SlideShow.aspx?AID={gallery_id}&AN=Sheriff%20-%20Use%20of%20Force%20-%20Case%2014110123" |
| 129 | + self.params["url"] = galley_link |
| 130 | + images_file_name = ( |
| 131 | + f"{self.agency_slug}/images_{gallery_id}.html" |
| 132 | + ) |
| 133 | + self.cache.download( |
| 134 | + images_file_name, self.zenrows_api_url, params=self.params |
| 135 | + ) |
| 136 | + html = self.cache.read(images_file_name) |
| 137 | + soup = BeautifulSoup(html, "html.parser") |
| 138 | + body = soup.find("div", class_="slides") |
| 139 | + a_tags = body.find_all("a") |
| 140 | + for a_tag in a_tags: |
| 141 | + img_tag = a_tag.find("img") |
| 142 | + # Get the 'src' and 'alt' attributes |
| 143 | + image_src = img_tag.get("src") |
| 144 | + image_alt = img_tag.get("alt") |
| 145 | + payload = { |
| 146 | + "asset_url": f"https://www.lakesheriff.com{image_src}", |
| 147 | + "case_id": child_page["case_number"], |
| 148 | + "name": image_alt, |
| 149 | + "title": link.text, |
| 150 | + "parent_page": str(child_page["page_filename"]), |
| 151 | + "details": { |
| 152 | + "date": child_page["date"], |
| 153 | + "location": child_page["location"], |
| 154 | + "name": child_page["name"], |
| 155 | + "incident_type": child_page["incident_type"], |
| 156 | + }, |
| 157 | + } |
| 158 | + metadata.append(payload) |
| 159 | + |
| 160 | + outfile = self.data_dir.joinpath(f"{self.agency_slug}.json") |
| 161 | + self.cache.write_json(outfile, metadata) |
| 162 | + return outfile |
0 commit comments