Created
May 28, 2022 11:22
-
-
Save fishyer/1e7c879b874ec7f01d7bef9b087ed5b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import uvicorn as uvicorn | |
| from fastapi import FastAPI | |
| import requests | |
| from bs4 import BeautifulSoup | |
| app = FastAPI() # 必须实例化该类,启动的时候调用 | |
| git_token = "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" | |
| s = requests.session() | |
| s.keep_alive = False | |
| headers = {"Authorization": "token " + git_token} | |
| def get_github_followers(uname): | |
| status_code = 200 | |
| message = "success" | |
| followers = 0 | |
| response = s.get(f"https://api.github.com/users/{uname}", headers = headers) | |
| # print(response.text) | |
| # print(response.headers) | |
| json_response = json.loads(response.text) | |
| message = json_response.get("message") | |
| print(message) | |
| if message == None: | |
| status_code = 200 | |
| message = "success" | |
| followers = json_response.get("followers") | |
| else: | |
| status_code = -400 | |
| followers = -1 | |
| return status_code, message, followers | |
| def get_github_repoInfo_count(uname, query): | |
| status_code = 200 | |
| message = "success" | |
| count = 0 | |
| response = s.get(f"https://api.github.com/users/{uname}/repos", headers = headers) | |
| # print(response.headers) | |
| json_response = json.loads(response.text) | |
| try: | |
| message = json_response.get("message") | |
| print(message) | |
| except: | |
| message = None | |
| if message == None: | |
| status_code = 200 | |
| message = "success" | |
| # print(len(list(json_response))) | |
| for repoJson in json_response: | |
| # query: | |
| count += repoJson.get(query) | |
| else: | |
| status_code = -400 | |
| count = -1 | |
| return status_code, message, count | |
| # 请求根目录 | |
| @app.get('/') | |
| def index(): | |
| return {'message': '欢迎来到FastApi 服务!'} | |
| @app.get('/api/fanscount/github/uname/{uname}/followers') | |
| def github_followers(uname: str): | |
| status_code, message, followers = get_github_followers(uname) | |
| return {'status' : status_code, "message" : message, "data" : { "value" : followers}} | |
| @app.get('/api/fanscount/github/uname/{uname}/stars') | |
| def github_stars(uname: str): | |
| status_code, message, stars = get_github_repoInfo_count(uname, "stargazers_count") | |
| return {'status' : status_code, "message" : message, "data" : { "value" : stars}} | |
| @app.get('/api/fanscount/github/uname/{uname}/forks') | |
| def github_stars(uname: str): | |
| status_code, message, forks = get_github_repoInfo_count(uname, "forks_count") | |
| return {'status' : status_code, "message" : message, "data" : { "value" : forks}} | |
| if __name__ == '__main__': | |
| uvicorn.run(app=app, host="0.0.0.0", port=40001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment