11
11
from datetime import datetime , timezone
12
12
import humanize
13
13
from itertools import count
14
- import aiosqlite
15
14
import diskcache
16
- import json
17
15
import pathlib
18
16
from typing import Optional , List , Dict
19
17
import argparse
@@ -77,7 +75,9 @@ def get(self, key, default=None, retry=False):
77
75
cache = DateTimeCache (CACHE_DIR )
78
76
79
77
80
- async def get_org_members (session : aiohttp .ClientSession , org : str ) -> List [Dict ]:
78
+ async def get_org_members (
79
+ session : aiohttp .ClientSession , org : str , debug : bool
80
+ ) -> List [Dict ]:
81
81
"""Fetch all members of a GitHub organization with caching.
82
82
83
83
Parameters
@@ -106,7 +106,8 @@ async def get_org_members(session: aiohttp.ClientSession, org: str) -> List[Dict
106
106
# Try to get from cache with retry
107
107
cached_data = cache .get (cache_key , retry = True )
108
108
if cached_data is not None :
109
- print (f"[cyan]Cache hit for { org } members[/cyan]" )
109
+ if debug :
110
+ print (f"[cyan]Cache hit for { org } members[/cyan]" )
110
111
return cached_data
111
112
112
113
print (f"[yellow]Cache miss for { org } members - fetching from API[/yellow]" )
@@ -139,25 +140,30 @@ async def get_org_members(session: aiohttp.ClientSession, org: str) -> List[Dict
139
140
140
141
141
142
async def get_user_activity (
142
- session : aiohttp .ClientSession , username : str
143
+ session : aiohttp .ClientSession , username : str , debug : bool
143
144
) -> Optional [datetime ]:
144
145
"""Fetch the last public activity date for a GitHub user."""
145
146
cache_key = f"user_activity_{ username } "
146
147
147
148
# Try to get from cache
148
149
cached_data = cache .get (cache_key )
149
150
if cached_data is not None :
150
- print (f"[cyan]Cache hit for { username } activity[/cyan]" )
151
+ if debug :
152
+ print (f"[cyan]Cache hit for { username } activity[/cyan]" )
151
153
return cached_data
152
-
153
- print (f"[yellow]Cache miss for { username } activity - fetching from API[/yellow]" )
154
+ if debug :
155
+ print (
156
+ f"[yellow]Cache miss for { username } activity - fetching from API[/yellow]"
157
+ )
154
158
155
159
try :
156
- print (f"Getting activity for { username } " )
160
+ if debug :
161
+ print (f"[blue]Getting activity for { username } [/blue]" )
157
162
url = f"https://api.github.com/users/{ username } /events/public"
158
163
async with session .get (url , headers = headers ) as response :
159
164
if response .status == 200 :
160
- print (f"Got activity for { username } " )
165
+ if debug :
166
+ print (f"Got activity for { username } " )
161
167
events = await response .json ()
162
168
if events :
163
169
last_activity = datetime .fromisoformat (
@@ -170,7 +176,8 @@ async def get_user_activity(
170
176
print (f"[green]Cached activity for { username } [/green]" )
171
177
return last_activity
172
178
else :
173
- print (f"[yellow]No activity found for { username } [/yellow]" )
179
+ if debug :
180
+ print (f"[yellow]No activity found for { username } [/yellow]" )
174
181
cache [cache_key ] = None # Using __setitem__ instead of set()
175
182
else :
176
183
print (
@@ -205,7 +212,7 @@ def clear_cache() -> None:
205
212
print (f"[red]Error clearing cache: { str (e )} [/red]" )
206
213
207
214
208
- async def main ():
215
+ async def main (debug : bool ):
209
216
"""Main execution function."""
210
217
# Show cache status
211
218
print (f"[blue]Cache directory: { CACHE_DIR } (size: { get_cache_size ()} )[/blue]" )
@@ -236,7 +243,7 @@ async def main():
236
243
# Get all members from all orgs
237
244
all_members = {}
238
245
for org in orgs :
239
- members = await get_org_members (session , org )
246
+ members = await get_org_members (session , org , debug )
240
247
for member in members :
241
248
if member ["login" ] not in all_members :
242
249
all_members [member ["login" ]] = []
@@ -245,7 +252,7 @@ async def main():
245
252
# Get activity for each user
246
253
tasks = []
247
254
for username in all_members :
248
- task = get_user_activity (session , username )
255
+ task = get_user_activity (session , username , debug )
249
256
tasks .append ((username , task ))
250
257
251
258
results = await asyncio .gather (* (task for _ , task in tasks ))
@@ -258,14 +265,16 @@ async def main():
258
265
username ,
259
266
datetime .fromisoformat (last_activity ["__datetime__" ])
260
267
if last_activity is not None
261
- else datetime . fromtimestamp ( 0 ). replace ( tzinfo = timezone . utc ) ,
268
+ else None ,
262
269
all_members [username ],
263
270
)
264
271
)
265
272
266
273
for username , last_activity , user_orgs in sorted (
267
274
user_activities ,
268
- key = lambda x : x [1 ] if x [1 ] is not None else datetime .fromtimestamp (0 ),
275
+ key = lambda x : (x [1 ], x [0 ])
276
+ if x [1 ] is not None
277
+ else (datetime .fromtimestamp (0 ).replace (tzinfo = timezone .utc ), x [0 ]),
269
278
reverse = True ,
270
279
):
271
280
last_activity_ago = (
@@ -290,4 +299,4 @@ async def main():
290
299
if args .clear_cache :
291
300
clear_cache ()
292
301
293
- asyncio .run (main ())
302
+ asyncio .run (main (args . debug ))
0 commit comments