[docs]classElSalvador(CountryTestBase):location:str="El Salvador"units:str="tests performed"source_label:str="Government of El Salvador"source_url_ref:str="https://covid19.gob.sv/"source_url:str="https://e.infogram.com/"regex:dict={"title":r"\'PRUEBAS REALIZADAS\'\, \'CASOS POSITIVOS\'","element":r"window\.infographicData=({.*})",}rename_columns:dict={"CASOS POSITIVOS":"positive","PRUEBAS REALIZADAS":"Daily change in cumulative total",}
[docs]defread(self)->pd.DataFrame:"""Read data from source"""data_id=self._get_data_id_from_source(self.source_url_ref)df=self._load_data(data_id)returndf
[docs]def_get_data_id_from_source(self,source_url:str)->str:"""Get Data ID from source"""soup=get_soup(source_url)data_id=soup.find(class_="infogram-embed")["data-id"]returndata_id
[docs]def_load_data(self,data_id:str)->pd.DataFrame:"""Load data from source"""url=f"{self.source_url}{data_id}"soup=get_soup(url)match=re.search(self.regex["element"],str(soup))ifnotmatch:raiseValueError("Website Structure Changed, please update the script")data=json.loads(match.group(1))data=data["elements"]["content"]["content"]["entities"]data=[data[idx]foridxindataifre.search(self.regex["title"],str(data[idx].values()))][0]data_list=data["props"]["chartData"]["data"]df=pd.DataFrame()forframeindata_list:col=frame.pop(0)col[0]="Date"df=df.append(pd.DataFrame(frame,columns=col),ignore_index=True)returndf
[docs]defpipeline(self,df:pd.DataFrame)->pd.DataFrame:"""Pipeline for data processing"""return(df.pipe(self.pipe_rename_columns).pipe(self.pipe_date).pipe(self.pipe_metadata).pipe(self.pipe_merge).pipe(self.pipe_positive).pipe(self.pipe_numeric).pipe(self.pipe_pr))
[docs]defexport(self):"""Export data to csv"""df=self.read().pipe(self.pipeline)# self.export_datafile(df, float_format="%.5f")df.to_csv(self.get_output_path(),index=False)