1- from time import sleep
2- from typing import List
3-
41PKGS = [
52 'computer-languages' ,
63 'data-languages' ,
@@ -25,34 +22,39 @@ def format_total(num: int) -> str: # abbr ints to e.g. 1.5k, 2b
2522 else f'{ num / 1000 :.1f} K' if num >= 1000 \
2623 else str (num )
2724
28- def get_downloads (pkg : str , max_retries : int = 5 , get_delay : int = 2 ) -> int :
25+ def get_downloads (pkg : str , max_retries : int = 5 , get_delay : int = 1 ) -> int :
2926 import json
27+ from time import sleep
3028 from urllib .request import urlopen
3129 from urllib .error import HTTPError
30+
3231 url = STATS_API_URL .format (pkg = pkg )
32+
3333 for idx in range (max_retries ):
3434 try :
3535 with urlopen (url ) as resp :
36- return sum (item ['downloads' ] for item in json .load (resp )['data' ])
37- except HTTPError as err :
38- if err .code == 429 : # rate limited
39- retry_delay = (idx + 1 ) * 2 # exponentially back off
40- print (f'! { pkg } : Rate limited. Retrying in { retry_delay } s...' )
41- sleep (retry_delay )
42- else :
43- print (f'{ pkg } : ERROR ({ err .code } )' )
44- return 0
45- except Exception as err :
46- print (f'{ pkg } : Exception: { err } ' )
36+ result = sum (item ['downloads' ] for item in json .load (resp )['data' ])
4737 sleep (get_delay )
38+ return result
39+ except Exception as err :
40+ retry_delay = (idx + 1 ) * get_delay
41+ if isinstance (err , HTTPError ):
42+ if err .code == 429 :
43+ print (f'! { pkg } : Rate limited. Retrying in { retry_delay } s...' )
44+ else :
45+ print (f'{ pkg } : HTTP ERROR ({ err .code } ). Retrying in { retry_delay } s...' )
46+ else :
47+ print (f'{ pkg } : Exception: { err } . Retrying in { retry_delay } s...' )
48+ sleep (retry_delay )
49+
4850 print (f'{ pkg } : Failed after { max_retries } retries' )
4951 return 0
5052
51- def read_file (file_path : str ) -> List [str ]:
53+ def read_file (file_path : str ) -> list [str ]:
5254 with open (file_path , 'r' , encoding = 'utf-8' ) as file :
5355 return file .readlines ()
5456
55- def write_file (file_path : str , lines : List [str ]) -> None :
57+ def write_file (file_path : str , lines : list [str ]) -> None :
5658 with open (file_path , 'w' , encoding = 'utf-8' ) as file :
5759 file .writelines (lines )
5860
@@ -66,7 +68,7 @@ def update_downloads_shield(readme_path: str, downloads: int) -> None:
6668 if shield_match :
6769 new_line = re .sub (shield_match .group (2 ), downloads_str , line )
6870 lines [idx ] = new_line
69- print (f'>>> { new_line .strip ()} ' )
71+ print (f'»»» { new_line .strip ()} \n ' )
7072 write_file (readme_path , lines )
7173
7274def main () -> None :
@@ -75,11 +77,10 @@ def main() -> None:
7577 downloads = get_downloads (pkg )
7678 grand_total_dls += downloads
7779 print (f'{ pkg :30} { downloads :,} ' )
78- sleep (1 )
7980 print ('-' * 45 )
8081 print (f"{ 'TOTAL DOWNLOADS' :30} { grand_total_dls :,} \n " )
8182 README_PATH = 'docs/README.md'
82- print (f'Updating { README_PATH } ...' )
83+ print (f'Updating { README_PATH } ...\n ' )
8384 update_downloads_shield (README_PATH , grand_total_dls )
8485 print ('Done!' )
8586
0 commit comments