@@ -80,18 +80,22 @@ def write_file(file_path: str, lines: list[str]) -> None:
8080 with open (file_path , 'w' , encoding = 'utf-8' ) as file :
8181 file .writelines (lines )
8282
83- def update_downloads_shield (readme_path : str , downloads : int ) -> None :
83+ def update_downloads_shield (readme_path : str , downloads : int ) -> bool :
8484 import re
8585 lines = read_file (readme_path )
8686 shield_re = r'(?i)(<img[^>]+src="https://img.shields.io/badge/Downloads-)([\d,\.km]+)(-[a-f\d]{6})'
8787 downloads_str = f'{ format_total (downloads ).lower ()} '
88+ shield_updated = False
8889 for idx , line in enumerate (lines ):
8990 shield_match = re .search (shield_re , line )
9091 if shield_match :
9192 new_line = re .sub (shield_match .group (2 ), downloads_str , line )
92- lines [idx ] = new_line
93- print (f'»»» { new_line .strip ()} \n ' )
94- write_file (readme_path , lines )
93+ if new_line != line :
94+ lines [idx ] = new_line
95+ shield_updated = True
96+ print (f'»»» { new_line .strip ()} \n ' )
97+ if shield_updated : write_file (readme_path , lines )
98+ return shield_updated
9599
96100def main () -> None :
97101 grand_total_dls = 0
@@ -102,7 +106,7 @@ def main() -> None:
102106 print ('-' * 45 )
103107 print (f"{ 'TOTAL DOWNLOADS' :30} { grand_total_dls :,} \n " )
104108 print (f'Updating { README_PATH } ...\n ' )
105- update_downloads_shield (README_PATH , grand_total_dls )
106- print ('Done! ' )
109+ shield_updated = update_downloads_shield (README_PATH , grand_total_dls )
110+ print ('Shield updated!' if shield_updated else 'No update needed. ' )
107111
108112if __name__ == '__main__' : main ()
0 commit comments