Python을 사용하여 Github 리포지토리 데이터를 검색하는 방법
귀하의 조직에 Github 저장소가 너무 많아서 보고, 대시보드 또는 감사 목적으로 각 저장소의 내용을 쉽게 요약하고 기록할 수 있는 방법이 필요합니까? Github API를 사용하여 바로 그 작업을 수행하는 빠른 스크립트는 다음과 같습니다.
기능:
-
get_repo_info(소유자, 저장소):
- GitHub 저장소 소유자의 사용자 이름(소유자)과 저장소 이름(repo)을 가져옵니다.
- GitHub의 API에 요청을 보내 저장소 정보를 가져옵니다.
- 성공하면 저장소 정보를 JSON 객체로 반환하고, 오류가 있으면 None을 반환합니다.
-
get_collaborators(collaborators_url):
- 저장소의 공동작업자 목록에 대한 URL을 가져옵니다.
- 공동작업자 목록을 가져오기 위한 요청을 보냅니다.
- 공동작업자 사용자 이름 목록을 반환하거나, 오류가 발생하면 빈 목록을 반환합니다.
-
get_언어(언어_url):
- 저장소의 언어 데이터에 대한 URL을 가져옵니다.
- 저장소에서 사용되는 프로그래밍 언어를 검색하라는 요청을 보냅니다.
- 언어 목록을 반환하거나 오류가 있는 경우 빈 목록을 반환합니다.
-
get_open_issues(소유자, 저장소):
- 저장소 소유자의 사용자 이름(owner)과 저장소 이름(repo)을 가져옵니다.
- 저장소에 있는 미해결 문제 목록을 검색하라는 요청을 보냅니다.
- 미해결 이슈를 JSON 형식으로 반환하거나, 문제가 있는 경우 오류 메시지를 출력합니다.
-
get_repo_data(repo_url):
- 저장소 URL을 가져와 구문 분석하여 소유자 및 저장소 값을 가져온 다음 다른 함수를 호출하여 저장소에 대한 다양한 정보를 수집합니다.
- 이름, 소유자, 가시성, 공동작업자, 언어, 공개 이슈, 마지막 활동을 포함한 저장소 정보를 컴파일하여 구조화된 형식(사전)으로 반환합니다.
import json import requests from pymongo import MongoClient # MongoDB setup (replace with your actual connection details) client = MongoClient("mongodb://localhost:27017/") db = client["github_repos"] # Database name collection = db["repos"] # Collection name def get_repo_info(owner, repo): url = f"https://api.github.com/repos/{owner}/{repo}" headers = {"Accept": "application/vnd.github+json"} response = requests.get(url, headers=headers) if response.status_code == 200: return response.json() else: print(f"Error: {response.status_code}") return None def get_collaborators(collaborators_url): response = requests.get(collaborators_url) if response.status_code == 200: return [collaborator["login"] for collaborator in response.json()] else: return [] def get_languages(languages_url): response = requests.get(languages_url) if response.status_code == 200: return list(response.json().keys()) else: return [] def get_open_issues(owner, repo): url = f"https://api.github.com/repos/{owner}/{repo}/issues?state=open" headers = {"Accept": "application/vnd.github+json"} response = requests.get(url, headers=headers) if response.status_code == 200: return response.json() else: print(f"Error: {response.status_code}") return [] def get_repo_data(repo_url): owner, repo = repo_url.split("/")[-2:] repo_info = get_repo_info(owner, repo) if repo_info: data = { "Github URL": repo_url, "Project name": repo_info["name"], "Project owner": repo_info["owner"]["login"], "List users with access": get_collaborators(repo_info["collaborators_url"].split("{")[0]), # remove template part of URL "Programming languages used": get_languages(repo_info["languages_url"]), "Security/visibility level": repo_info["visibility"], "Summary": repo_info["description"], "Last maintained": repo_info["pushed_at"], "Last release": repo_info["default_branch"], "Open issues": get_open_issues(owner, repo), } # Insert the data into MongoDB collection.insert_one(data) print("Data inserted into MongoDB successfully.") return data else: return None # Example usage repo_url = "https://github.com/URL" repo_data = get_repo_data(repo_url) if repo_data: print(json.dumps(repo_data, indent=4))
위 내용은 Python을 사용하여 Github 리포지토리 데이터를 검색하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Linux 터미널에서 Python 버전을 보려고 할 때 Linux 터미널에서 Python 버전을 볼 때 권한 문제에 대한 솔루션 ... Python을 입력하십시오 ...

Fiddlerevery Where를 사용할 때 Man-in-the-Middle Reading에 Fiddlereverywhere를 사용할 때 감지되는 방법 ...

Python의 Pandas 라이브러리를 사용할 때는 구조가 다른 두 데이터 프레임 사이에서 전체 열을 복사하는 방법이 일반적인 문제입니다. 두 개의 dats가 있다고 가정 해

10 시간 이내에 컴퓨터 초보자 프로그래밍 기본 사항을 가르치는 방법은 무엇입니까? 컴퓨터 초보자에게 프로그래밍 지식을 가르치는 데 10 시간 밖에 걸리지 않는다면 무엇을 가르치기로 선택 하시겠습니까?

Uvicorn은 HTTP 요청을 어떻게 지속적으로 듣습니까? Uvicorn은 ASGI를 기반으로 한 가벼운 웹 서버입니다. 핵심 기능 중 하나는 HTTP 요청을 듣고 진행하는 것입니다 ...

Linux 터미널에서 Python 사용 ...

Investing.com의 크롤링 전략 이해 많은 사람들이 종종 Investing.com (https://cn.investing.com/news/latest-news)에서 뉴스 데이터를 크롤링하려고합니다.
