1- import time
1+ from time import sleep
22from typing import List
33
44PKGS = [
@@ -35,16 +35,16 @@ def get_downloads(pkg: str, max_retries: int = 5, get_delay: int = 2) -> int:
3535 with urlopen (url ) as resp :
3636 return sum (item ['downloads' ] for item in json .load (resp )['data' ])
3737 except HTTPError as err :
38- if err .code == 429 : # Rate limited
39- retry_delay = (idx + 1 ) * 2 # Exponential backoff
38+ if err .code == 429 : # rate limited
39+ retry_delay = (idx + 1 ) * 2 # exponentially back off
4040 print (f'{ pkg } : Rate limited. Retrying in { retry_delay } s...' )
41- time . sleep (retry_delay )
41+ sleep (retry_delay )
4242 else :
4343 print (f'{ pkg } : ERROR ({ err .code } )' )
4444 return 0
4545 except Exception as err :
4646 print (f'{ pkg } : Exception: { err } ' )
47- time . sleep (get_delay )
47+ sleep (get_delay )
4848 print (f'{ pkg } : Failed after { max_retries } retries' )
4949 return 0
5050
@@ -59,9 +59,8 @@ def write_file(file_path: str, lines: List[str]) -> None:
5959def update_downloads_shield (readme_path : str , downloads : int ) -> None :
6060 import re
6161 lines = read_file (readme_path )
62- shield_re = r'(<img[^>]+src="https://img.shields.io/badge/Downloads-)([\d\.kM]+)(-[a-f0-9]{6})'
63- formatted_downloads = format_total (downloads )
64- downloads_str = f'{ formatted_downloads .lower ()} '
62+ shield_re = r'(?i)(<img[^>]+src="https://img.shields.io/badge/Downloads-)([\d,\.km]+)(-[a-f\d]{6})'
63+ downloads_str = f'{ format_total (downloads ).lower ()} '
6564 for idx , line in enumerate (lines ):
6665 shield_match = re .search (shield_re , line )
6766 if shield_match :
@@ -76,7 +75,7 @@ def main() -> None:
7675 downloads = get_downloads (pkg )
7776 grand_total_dls += downloads
7877 print (f'{ pkg :30} { downloads :,} ' )
79- time . sleep (1 )
78+ sleep (1 )
8079 print ('-' * 45 )
8180 print (f"{ 'TOTAL DOWNLOADS' :20} { grand_total_dls :,} \n " )
8281 README_PATH = 'docs/README.md'
0 commit comments