77def list_split (items , n ):
88 return [items [i :i + n ] for i in range (0 , len (items ), n )]
99def getdata (name ):
10- 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 )
1127 data = gitpage .text
28+
29+ # 2023-11-22 更新正则 https://github.com/Zfour/python_github_calendar_api/issues/18
1230 datadatereg = re .compile (r'data-date="(.*?)" id="contribution-day-component' )
13- datacountreg = re .compile (r'position-absolute">(.*?) contribution' )
31+ datacountreg = re .compile (r'<tool-tip .*?class="sr-only position-absolute">(.*?) contribution' )
32+
1433 datadate = datadatereg .findall (data )
1534 datacount = datacountreg .findall (data )
1635 datacount = list (map (int , [0 if i == "No" else i for i in datacount ]))
1736
37+ # 检查datadate和datacount是否为空
38+ if not datadate or not datacount :
39+ # 处理空数据情况
40+ return {"total" : 0 , "contributions" : []}
41+
1842 # 将datadate和datacount按照字典序排序
1943 sorted_data = sorted (zip (datadate , datacount ))
2044 datadate , datacount = zip (* sorted_data )
@@ -32,8 +56,12 @@ def getdata(name):
3256 return returndata
3357class handler (BaseHTTPRequestHandler ):
3458 def do_GET (self ):
59+ # 2024-03-15 规范接口的传参方式 https://github.com/Zfour/python_github_calendar_api/issues/20#issuecomment-1999115747
3560 path = self .path
36- user = path .split ('?' )[1 ][:- 1 ]
61+ spl = path .split ('?' )[1 :]
62+ for kv in spl :
63+ key ,user = kv .split ("=" )
64+ if key == "user" : break
3765 data = getdata (user )
3866 self .send_response (200 )
3967 self .send_header ('Access-Control-Allow-Origin' , '*' )
0 commit comments