33import  re 
44from  http .server  import  BaseHTTPRequestHandler 
55import  json 
6+ 
67def  list_split (items , n ):
78 return  [items [i :i  +  n ] for  i  in  range (0 , len (items ), n )]
8- def  no_to_0 (x ):
9-  return  int (x ) if  x .isdigit () else  0 
109def  getdata (name ):
11-  gitpage  =  requests .get ("https://github.com/"  +  name )
10+ 
11+  # 2024-03-29 定义 headers 请求头 
12+  # 请见 https://github.com/yuhengwei2001/python_github_calendar_api/commit/0f37cfc003f09e99a1892602d8bc2b38137899d2#diff-b014e93fcab9bae29f453d7a616da5eac2f02947f32d02a1a1bf200eeaab5a39L11 
13+  headers  =  {
14+  'Referer' : 'https://github.com/' +  name ,
15+  'Sec-Ch-Ua' : '"Chromium";v="122", "Not(A:Brand";v="24", "Microsoft Edge";v="122"' ,
16+  'Sec-Ch-Ua-Mobile' : '?0' ,
17+  'Sec-Ch-Ua-Platform' : '"Windows"' ,
18+  'Sec-Fetch-Dest' : 'empty' ,
19+  'Sec-Fetch-Mode' : 'cors' ,
20+  'Sec-Fetch-Site' : 'same-origin' ,
21+  'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0' ,
22+  'X-Requested-With' : 'XMLHttpRequest' 
23+  }
24+  # 发送请求时添加 headers 请求头 
25+  # gitpage = requests.get("https://github.com/" + name) 
26+  gitpage  =  requests .get ("https://github.com/"  +  name  +  "?action=show&controller=profiles&tab=contributions&user_id=" +  name , headers = headers )
1227 data  =  gitpage .text 
13-  print (data )
14-  datadatereg  =  re .compile (r'data-date="(.*?)".*?data-level' )
15-  datacountreg  =  re .compile (r'\">(.*?) contribution' )
28+  
29+  # 2023-11-22 更新正则 https://github.com/Zfour/python_github_calendar_api/issues/18 
30+  datadatereg  =  re .compile (r'data-date="(.*?)" id="contribution-day-component' )
31+  datacountreg  =  re .compile (r'<tool-tip .*?class="sr-only position-absolute">(.*?) contribution' )
32+  
1633 datadate  =  datadatereg .findall (data )
1734 datacount  =  datacountreg .findall (data )
18-  print (datacount )
19-  datacount  =  list (map (no_to_0 , datacount ))
20-  print (datacount ,datadate ,len (datacount ),len (datadate ))
35+  datacount  =  list (map (int , [0  if  i  ==  "No"  else  i  for  i  in  datacount ]))
36+ 
37+  # 检查datadate和datacount是否为空 
38+  if  not  datadate  or  not  datacount :
39+  # 处理空数据情况 
40+  return  {"total" : 0 , "contributions" : []}
41+  
42+  # 将datadate和datacount按照字典序排序 
43+  sorted_data  =  sorted (zip (datadate , datacount ))
44+  datadate , datacount  =  zip (* sorted_data )
45+  
2146 contributions  =  sum (datacount )
2247 datalist  =  []
2348 for  index , item  in  enumerate (datadate ):
@@ -31,8 +56,12 @@ def getdata(name):
3156 return  returndata 
3257class  handler (BaseHTTPRequestHandler ):
3358 def  do_GET (self ):
59+  # 2024-03-15 规范接口的传参方式 https://github.com/Zfour/python_github_calendar_api/issues/20#issuecomment-1999115747 
3460 path  =  self .path 
35-  user  =  path .split ('?' )[1 ]
61+  spl = path .split ('?' )[1 :]
62+  for  kv  in  spl :
63+  key ,user = kv .split ("=" )
64+  if  key == "user" : break 
3665 data  =  getdata (user )
3766 self .send_response (200 )
3867 self .send_header ('Access-Control-Allow-Origin' , '*' )
0 commit comments