From 8e8bf4d6e57a22c4a351c5d1b9ab547b185e23cf Mon Sep 17 00:00:00 2001 From: ljh-cppu <88241319+ljh-cppu@users.noreply.github.com> Date: Thu, 19 Jan 2023 22:28:14 +0800 Subject: [PATCH 1/5] Update index.py --- api/index.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/api/index.py b/api/index.py index 63deaa4..1903469 100644 --- a/api/index.py +++ b/api/index.py @@ -6,14 +6,19 @@ def list_split(items, n): return [items[i:i + n] for i in range(0, len(items), n)] +def no_to_0(x): + return int(x) if x.isdigit() else 0 def getdata(name): gitpage = requests.get("https://github.com/" + name) data = gitpage.text + print(data) datadatereg = re.compile(r'data-date="(.*?)" data-level') - datacountreg = re.compile(r'data-count="(.*?)" data-date') + datacountreg = re.compile(r'\">(.*?) contribution') datadate = datadatereg.findall(data) datacount = datacountreg.findall(data) - datacount = list(map(int, datacount)) + print(datacount) + datacount = list(map(no_to_0, datacount)) + print(datacount,datadate,len(datacount),len(datadate)) contributions = sum(datacount) datalist = [] for index, item in enumerate(datadate): From 5b1aae07ae3c2027ba9672b230b8ff0197d85dc4 Mon Sep 17 00:00:00 2001 From: ljh-cppu <88241319+ljh-cppu@users.noreply.github.com> Date: Thu, 28 Dec 2023 20:04:04 +0800 Subject: [PATCH 2/5] Update index.py --- api/index.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/api/index.py b/api/index.py index 1903469..ea585ee 100644 --- a/api/index.py +++ b/api/index.py @@ -6,19 +6,19 @@ def list_split(items, n): return [items[i:i + n] for i in range(0, len(items), n)] -def no_to_0(x): - return int(x) if x.isdigit() else 0 def getdata(name): gitpage = requests.get("https://github.com/" + name) data = gitpage.text - print(data) datadatereg = re.compile(r'data-date="(.*?)" data-level') - datacountreg = re.compile(r'\">(.*?) contribution') + datacountreg = re.compile(r'(.*?) contribution') datadate = datadatereg.findall(data) datacount = datacountreg.findall(data) - print(datacount) - datacount = list(map(no_to_0, datacount)) - print(datacount,datadate,len(datacount),len(datadate)) + datacount = list(map(int, [0 if i == "No" else i for i in datacount])) + + # 将datadate和datacount按照字典序排序 + sorted_data = sorted(zip(datadate, datacount)) + datadate, datacount = zip(*sorted_data) + contributions = sum(datacount) datalist = [] for index, item in enumerate(datadate): From 7e8f758462adb3130852e8c4553640a6ecc8ec56 Mon Sep 17 00:00:00 2001 From: ljh-cppu <88241319+ljh-cppu@users.noreply.github.com> Date: Thu, 28 Dec 2023 21:49:59 +0800 Subject: [PATCH 3/5] Update index.py --- api/index.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/api/index.py b/api/index.py index ea585ee..f73bc61 100644 --- a/api/index.py +++ b/api/index.py @@ -1,24 +1,18 @@ -# -*- coding: UTF-8 -*- -import requests -import re -from http.server import BaseHTTPRequestHandler -import json - def list_split(items, n): return [items[i:i + n] for i in range(0, len(items), n)] +def no_to_0(x): + return int(x) if x.isdigit() else 0 def getdata(name): gitpage = requests.get("https://github.com/" + name) data = gitpage.text - datadatereg = re.compile(r'data-date="(.*?)" data-level') - datacountreg = re.compile(r'(.*?) contribution') + print(data) + datadatereg = re.compile(r'data-date="(.*?)".*?data-level') + datacountreg = re.compile(r'\">(.*?) contribution') datadate = datadatereg.findall(data) datacount = datacountreg.findall(data) - datacount = list(map(int, [0 if i == "No" else i for i in datacount])) - - # 将datadate和datacount按照字典序排序 - sorted_data = sorted(zip(datadate, datacount)) - datadate, datacount = zip(*sorted_data) - + print(datacount) + datacount = list(map(no_to_0, datacount)) + print(datacount,datadate,len(datacount),len(datadate)) contributions = sum(datacount) datalist = [] for index, item in enumerate(datadate): From 8256bbd37d904bf588b820af9239b461596f6e14 Mon Sep 17 00:00:00 2001 From: ljh-cppu <88241319+ljh-cppu@users.noreply.github.com> Date: Thu, 28 Dec 2023 21:55:17 +0800 Subject: [PATCH 4/5] Update index.py --- api/index.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api/index.py b/api/index.py index f73bc61..1a40036 100644 --- a/api/index.py +++ b/api/index.py @@ -1,3 +1,8 @@ +# -*- coding: UTF-8 -*- +import requests +import re +from http.server import BaseHTTPRequestHandler +import json def list_split(items, n): return [items[i:i + n] for i in range(0, len(items), n)] def no_to_0(x): From bad833d5260a384f1208c5f3e92ccaeaa3ad6cf0 Mon Sep 17 00:00:00 2001 From: ljh-cppu <88241319+ljh-cppu@users.noreply.github.com> Date: Sat, 13 Apr 2024 16:09:32 +0800 Subject: [PATCH 5/5] Update index.py --- api/index.py | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/api/index.py b/api/index.py index 1a40036..3286872 100644 --- a/api/index.py +++ b/api/index.py @@ -3,21 +3,46 @@ import re from http.server import BaseHTTPRequestHandler import json + def list_split(items, n): return [items[i:i + n] for i in range(0, len(items), n)] -def no_to_0(x): - return int(x) if x.isdigit() else 0 def getdata(name): - gitpage = requests.get("https://github.com/" + name) + + # 2024-03-29 定义 headers 请求头 + # 请见 https://github.com/yuhengwei2001/python_github_calendar_api/commit/0f37cfc003f09e99a1892602d8bc2b38137899d2#diff-b014e93fcab9bae29f453d7a616da5eac2f02947f32d02a1a1bf200eeaab5a39L11 + headers = { + 'Referer': 'https://github.com/'+ name, + 'Sec-Ch-Ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Microsoft Edge";v="122"', + 'Sec-Ch-Ua-Mobile': '?0', + 'Sec-Ch-Ua-Platform': '"Windows"', + 'Sec-Fetch-Dest': 'empty', + 'Sec-Fetch-Mode': 'cors', + 'Sec-Fetch-Site': 'same-origin', + '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', + 'X-Requested-With': 'XMLHttpRequest' + } + # 发送请求时添加 headers 请求头 + # gitpage = requests.get("https://github.com/" + name) + gitpage = requests.get("https://github.com/" + name + "?action=show&controller=profiles&tab=contributions&user_id="+ name, headers=headers) data = gitpage.text - print(data) - datadatereg = re.compile(r'data-date="(.*?)".*?data-level') - datacountreg = re.compile(r'\">(.*?) contribution') + + # 2023-11-22 更新正则 https://github.com/Zfour/python_github_calendar_api/issues/18 + datadatereg = re.compile(r'data-date="(.*?)" id="contribution-day-component') + datacountreg = re.compile(r'(.*?) contribution') + datadate = datadatereg.findall(data) datacount = datacountreg.findall(data) - print(datacount) - datacount = list(map(no_to_0, datacount)) - print(datacount,datadate,len(datacount),len(datadate)) + datacount = list(map(int, [0 if i == "No" else i for i in datacount])) + + # 检查datadate和datacount是否为空 + if not datadate or not datacount: + # 处理空数据情况 + return {"total": 0, "contributions": []} + + # 将datadate和datacount按照字典序排序 + sorted_data = sorted(zip(datadate, datacount)) + datadate, datacount = zip(*sorted_data) + contributions = sum(datacount) datalist = [] for index, item in enumerate(datadate): @@ -31,8 +56,12 @@ def getdata(name): return returndata class handler(BaseHTTPRequestHandler): def do_GET(self): + # 2024-03-15 规范接口的传参方式 https://github.com/Zfour/python_github_calendar_api/issues/20#issuecomment-1999115747 path = self.path - user = path.split('?')[1] + spl=path.split('?')[1:] + for kv in spl: + key,user=kv.split("=") + if key=="user": break data = getdata(user) self.send_response(200) self.send_header('Access-Control-Allow-Origin', '*')