Skip to content

Commit

Permalink
add auto update star
Browse files Browse the repository at this point in the history
  • Loading branch information
杨嘉颖 committed Apr 4, 2023
1 parent 3cf46cb commit c891569
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .idea/awesome-open-aigc.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions auto_update_star.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pandas as pd
import requests
from bs4 import BeautifulSoup

# 获取给定URL的Star数
def get_star_count(url):
api_url = url.replace("github.com", "api.github.com/repos")
response = requests.get(api_url)
if response.ok:
return response.json()[0]["stargazers_count"]
else:
return None

def do_auto_update_star():

# 读取Markdown文件并解析为HTML
with open("./README.md", "r") as f:
html = "".join(f.readlines())
soup = BeautifulSoup(html, "html.parser")

# 提取所有表格
tables = soup.find_all("table")

# 处理每个表格
for table in tables:
# 将HTML表格解析为dataframe
df = pd.read_html(str(table))[0]

# 提取所有github地址
github_urls = df[df.columns[-2]].str.extract("(https://github.com/[^)]+)")[0].tolist()

# 获取每个GitHub项目的Star数量
star_counts = [get_star_count(url) for url in github_urls]

# 将Star数量添加为新列
df["Stars"] = star_counts

# 将结果写回HTML表格
html_table = df.to_html(index=False)
new_table = BeautifulSoup(html_table, "html.parser")
table.replace_with(new_table)

# 将HTML保存回Markdown文件
with open("output.md", "w") as f:
f.write(str(soup))


if __name__ == '__main__':
do_auto_update_star()
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests==2.26.0
BeautifulSoup==4.1.3

0 comments on commit c891569

Please sign in to comment.