Coder Social home page Coder Social logo

hexo-gitcalendar's Introduction

这实现了一个 GitHub 贡献热力图 API,用于获取某个 GitHub 用户的贡献热力图数据。主要包括以下几个部分:

import 导入了需要用到的 Python 库:requests 用于发起 HTTP 请求,re 用于使用正则表达式进行字符串匹配,BaseHTTPRequestHandler 用于处理 HTTP 请求和响应,json 用于将 Python 对象序列化为 JSON 格式。

list_split 函数用于将一个列表按照固定大小分割成若干个子列表,以便于后续将数据转换为前端需要的格式。

getdata 函数接受一个参数 name,表示需要获取的 GitHub 用户名。该函数使用 requests 库发起一个 GET 请求,获取该用户的 GitHub 主页。使用正则表达式匹配该页面中的日期和贡献数数据,并将其存储在两个列表 datadate 和 datacount 中。然后将贡献数数据列表中的每个元素转换为整数类型,并求和得到该用户的总贡献数。最后,使用列表解析式将日期和贡献数数据组成字典,并按照一周为单位分割成若干个子列表。将总贡献数和贡献热力图数据列表作为字典返回。

handler 类继承了 BaseHTTPRequestHandler 类,用于处理 HTTP GET 请求。当接收到 GET 请求时,从请求路径中获取 GitHub 用户名,调用 getdata 函数获取该用户的贡献热力图数据,并将其序列化为 JSON 格式返回。返回的响应头中包括 Access-Control-Allow-Origin 表示允许跨域请求。

  • 使用 f-string 格式化字符串

f-string 是 Python 3.6 引入的新功能,它可以让你在字符串中嵌入表达式。可以将 getdata() 中的字符串拼接用 f-string 进行优化。

  • 使用列表推导式和 zip 函数

可以使用列表推导式和 zip 函数将 getdata() 中的两个正则表达式匹配结果合并到一个列表中。

  • 使用 sum 函数和 map 函数

可以使用 sum 函数和 map 函数计算 contributions 的总和。

  • 使用列表切片进行分组

可以使用列表切片将 datalist 列表按照每周七天进行分组。

hexo-gitcalendar's People

Contributors

yy2z avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.