11from github import Github
2- import github ,requests_cache ,re ,os ,kaggle ,json ,numpy as np
2+ import traceback , github ,requests_cache ,re ,os ,kaggle ,json ,numpy as np
33
44GITHUB_TOKEN = os .getenv ("GIST_TOKEN" )
55GIST_ID = "c9112c25c5acd400b90741efa81aa411"
@@ -81,20 +81,41 @@ def unformat_bytes(string):
8181
8282 for dsn in dataset_names :
8383 print (f'Processing { dsn } ...' )
84+ # Old Kaggle Api <1.7
8485 try :
8586 user = dsn .split ("/" )[0 ]
8687 dataset = vars (next ((d for d in usernames [user ] if vars (d )['ref' ] == dsn )))
8788 downloads .append (int (dataset ['downloadCount' ]))
8889 views .append (int (dataset ['viewCount' ]))
8990 sizes .append (int (dataset ['totalBytes' ]))
9091 print (f'{ dsn } done.' )
91-
92- except Exception as e :
93- print (f'{ e } when reading { dsn } ' )
92+
93+ # New Kaggle Api >=1.7
94+ except KeyError :
95+ try :
96+ user = dsn .split ("/" )[0 ]
97+ dataset = next ((d for d in usernames [user ] if d .ref == dsn ))
98+ downloads .append (int (dataset .download_count ))
99+ views .append (int (dataset .view_count ))
100+ sizes .append (int (dataset .total_bytes ))
101+ print (f'{ dsn } done.' )
102+
103+ except Exception :
104+ traceback .print_exc ()
105+ print (f'Error when reading { dsn } ' )
106+ print (f'Continuing with 0 values...' )
107+ downloads .append (0 )
108+ views .append (0 )
109+ sizes .append (0 )
110+
111+ except Exception :
112+ traceback .print_exc ()
113+ print (f'Error when reading { dsn } ' )
94114 print (f'Continuing with 0 values...' )
95115 downloads .append (0 )
96116 views .append (0 )
97117 sizes .append (0 )
118+
98119
99120 views = np .array (views )
100121 downloads = np .array (downloads )
@@ -121,7 +142,7 @@ def unformat_bytes(string):
121142 'size' : ds_size ,
122143 'views' : ds_views ,
123144 'downloads' : ds_downs ,
124- }
145+ }
125146 json_dump [filename ] = kaggle_stats
126147 total_bytes += int (np .sum (downloads * size_in_bytes ))
127148 total_size += int (np .sum (size_in_bytes ))
0 commit comments