@@ -34,18 +34,41 @@ def github_api_request(self, endpoint):
3434
3535 token = self .get_github_token ()
3636 if token :
37- headers ['Authorization' ] = f'token { token } '
37+ headers ['Authorization' ] = f'Bearer { token } '
3838
3939 try :
4040 req = urllib .request .Request (url , headers = headers )
4141 with urllib .request .urlopen (req , timeout = 10 ) as response :
4242 return json .loads (response .read ().decode ())
4343 except urllib .error .HTTPError as e :
44+ # Try to get detailed error message from response body
45+ error_msg = None
46+ try :
47+ error_body = e .read ().decode ()
48+ error_data = json .loads (error_body )
49+ error_msg = error_data .get ('message' , '' )
50+ except Exception :
51+ pass
52+
4453 if e .code == 403 :
45- print (f"⚠️ GitHub API rate limit reached. Set GITHUB_TOKEN env var for higher limits." )
54+ if error_msg :
55+ print (f"⚠️ GitHub API error (403): { error_msg } " )
56+ if 'rate limit' in error_msg .lower ():
57+ print (f"💡 Set GITHUB_TOKEN env var for higher rate limits." )
58+ elif 'credentials' in error_msg .lower () or 'token' in error_msg .lower ():
59+ print (f"💡 Check that GITHUB_TOKEN has the correct permissions." )
60+ else :
61+ print (f"⚠️ GitHub API access denied (403). Set GITHUB_TOKEN env var with proper permissions." )
62+ return None
63+ elif e .code == 404 :
64+ print (f"⚠️ GitHub API endpoint not found (404): { endpoint } " )
65+ return None
66+ else :
67+ if error_msg :
68+ print (f"⚠️ GitHub API error { e .code } : { error_msg } " )
69+ else :
70+ print (f"⚠️ GitHub API error { e .code } : { e .reason } " )
4671 return None
47- print (f"⚠️ GitHub API error { e .code } : { e .reason } " )
48- return None
4972 except Exception as e :
5073 print (f"⚠️ Error fetching from GitHub API: { e } " )
5174 return None
@@ -151,12 +174,28 @@ def collect_stats(self):
151174 """Collect all statistics"""
152175 print ("🔍 Collecting repository statistics...\n " )
153176
177+ # Try to load previous stats for fallback
178+ previous_github_stats = {}
179+ if self .stats_file .exists ():
180+ try :
181+ with open (self .stats_file , 'r' ) as f :
182+ previous_data = json .load (f )
183+ previous_github_stats = previous_data .get ('github' , {})
184+ except Exception :
185+ pass
186+
154187 stats = {
155188 "timestamp" : datetime .now (UTC ).strftime ('%Y-%m-%dT%H:%M:%S.%fZ' ),
156189 "local" : self .get_local_git_stats (),
157190 "github" : self .get_github_stats ()
158191 }
159192
193+ # If GitHub stats are empty but we have previous stats, preserve them
194+ if not stats ["github" ] and previous_github_stats :
195+ print ("ℹ️ Using cached GitHub stats from previous successful fetch" )
196+ stats ["github" ] = previous_github_stats
197+ stats ["github_cached" ] = True
198+
160199 return stats
161200
162201 def save_stats (self , stats ):
@@ -226,6 +265,8 @@ def display_stats(self, stats):
226265 # GitHub stats
227266 if stats .get ("github" ):
228267 print ("\n 🌐 GITHUB STATISTICS:" )
268+ if stats .get ("github_cached" ):
269+ print (" ℹ️ (Using cached data from previous successful fetch)" )
229270 print ("-" * 80 )
230271 gh = stats ["github" ]
231272 print (f" ⭐ Stars: { gh .get ('stars' , 'N/A' )} " )
@@ -251,7 +292,12 @@ def generate_stats_markdown(self, stats):
251292 md = f"""# Repository Statistics - Real-Time
252293
253294**Last Updated:** { stats ['timestamp' ]}
254-
295+ """
296+
297+ if stats .get ("github_cached" ):
298+ md += "\n > ℹ️ **Note:** GitHub metrics below are cached from previous successful fetch (current API call failed)\n "
299+
300+ md += """
255301## 📊 Current Statistics
256302
257303"""
0 commit comments