1212# "lxml",
1313# ]
1414# ///
15+ from __future__ import annotations
1516
17+ import argparse
18+ import logging
1619from typing import NamedTuple
1720
1821import httpx
@@ -33,27 +36,49 @@ def covid_stats(
3336) -> CovidData :
3437 xpath_str = '//div[@class = "maincounter-number"]/span/text()'
3538 try :
36- response = httpx .get (url , timeout = 10 ).raise_for_status ()
39+ response = httpx .get (url , timeout = 10 )
40+ response .raise_for_status ()
3741 except httpx .TimeoutException :
38- print (
42+ logging . error (
3943 "Request timed out. Please check your network connection "
4044 "or try again later."
4145 )
4246 return CovidData ("N/A" , "N/A" , "N/A" )
4347 except httpx .HTTPStatusError as e :
44- print (f"HTTP error occurred: { e } " )
48+ logging . error (f"HTTP error occurred: { e } " )
4549 return CovidData ("N/A" , "N/A" , "N/A" )
46- data = html .fromstring (response .content ).xpath (xpath_str )
50+ data : list [ str ] = html .fromstring (response .content ).xpath (xpath_str )
4751 if len (data ) != 3 :
48- print ("Unexpected data format. The page structure may have changed." )
49- data = "N/A" , "N/A" , "N/A"
52+ logging .warning ("Unexpected data format. The page structure may have changed." )
53+ return CovidData ("N/A" , "N/A" , "N/A" )
54+
5055 return CovidData (* data )
5156
57+ def main () -> None :
58+ """CLI entry point."""
59+ parser = argparse .ArgumentParser (
60+ description = "Fetch COVID-19 statistics from Worldometers (archived)."
61+ )
62+ parser .add_argument (
63+ "--url" ,
64+ type = str ,
65+ default = (
66+ "https://web.archive.org/web/20250825095350/"
67+ "https://www.worldometers.info/coronavirus/"
68+ ),
69+ help = "Custom archive URL (default: latest snapshot)." ,
70+ )
71+ # args = parser.parse_args()
72+ args , _ = parser .parse_known_args ()
5273
53- if __name__ == "__main__" :
74+ stats = covid_stats ( args . url )
5475 fmt = (
5576 "Total COVID-19 cases in the world: {}\n "
5677 "Total deaths due to COVID-19 in the world: {}\n "
5778 "Total COVID-19 patients recovered in the world: {}"
5879 )
59- print (fmt .format (* covid_stats ()))
80+ print (fmt .format (* stats ))
81+
82+
83+ if __name__ == "__main__" :
84+ main ()
0 commit comments