[docs]classBarbados(CountryVaxBase):location:str="Barbados"source_url:str="https://gisbarbados.gov.bb/top-stories/"source_url_ref:str=Noneregex:dict={"title":r"COVID-19 Update","people_vaccinated":r"at least one dose is ([\d,\s]+)",# "people_vaccinated": r"COVID\-19, ([\d,\s]+) persons \(.*\) have received at least one dose","people_fully_vaccinated":r"fully? (?:vaccinated|vaccinated persons) is ([\d,\s]+)",# "people_fully_vaccinated": (# r"To date, ([\d,\s]+) individuals \– (?:[\d,\s]+) males and (?:[\d,\s]+) females \(.*\) \– have been fully"# r" vaccinated"# ),# "people_vaccinated": (# r"(\d+) persons \((?:[\d.]+) per cent of the eligible population\) have received at least one dose"# ),# "people_fully_vaccinated": (# r"(\d+) individuals – (?:[\d]+) males and (?:[\d]+) females \((?:[\d.]+) per cent of the total"# r" population or (?:[\d.]+) per cent of the eligible population\) are fully vaccinated"# ),}
[docs]defread(self)->pd.DataFrame:"""Read data from source"""soup=get_soup(self.source_url)df=self._parse_data(soup)returndf
[docs]def_parse_data(self,soup:BeautifulSoup)->pd.DataFrame:"""Parse data from soup"""# Get the article URLelem=soup.find("a",text=re.compile(self.regex["title"]))ifelem:link=elem["href"]else:raiseValueError("No COVID-19 update new was found!")ifnotlink:raiseValueError("Article not found, please update the script")self.source_url_ref=linksoup=get_soup(link)# Get the metricsmetrics=self._parse_metrics(soup)# Get the datedate=self._parse_date(soup)df=pd.DataFrame({"date":[date],**metrics,})returndf
[docs]def_parse_metrics(self,soup:BeautifulSoup)->int:"""Parse metrics from soup"""text=soup.get_text()text=re.sub(r"(\d),(\d)",r"\1\2",text)people_vaccinated=clean_count(re.search(self.regex["people_vaccinated"],text).group(1).replace(" ",""))people_fully_vaccinated=clean_count(re.search(self.regex["people_fully_vaccinated"],text).group(1).replace(" ",""))total_vaccinations=people_vaccinated+people_fully_vaccinateddf={"people_vaccinated":[people_vaccinated],"people_fully_vaccinated":[people_fully_vaccinated],"total_vaccinations":[total_vaccinations],}returndf
[docs]def_parse_date(self,soup:BeautifulSoup)->str:"""Parse date from soup"""date_str=soup.find(class_="published").textifnotdate_str:raiseValueError("Date not found, please update the script")returnclean_date(date_str,"%b %d, %Y",minus_days=1)
[docs]defpipe_vaccine(self,df:pd.DataFrame)->pd.DataFrame:"""Pipes vaccine names for main data."""returndf.assign(vaccine="Oxford/AstraZeneca, Pfizer/BioNTech, Sinopharm/Beijing")
[docs]defpipeline(self,df:pd.DataFrame)->pd.DataFrame:"""Pipeline for data processing"""returndf.pipe(self.pipe_metadata).pipe(self.pipe_vaccine)
[docs]defexport(self):"""Export data to csv"""df=self.read().pipe(self.pipeline)self.export_datafile(df,attach=True)