GitHub API: Repositories Contributed To

后端 未结 10 1770
耶瑟儿~
耶瑟儿~ 2020-12-07 09:45

Is there a way to get access to the data in the “Repositories contributed to” module on GitHub profile pages via the GitHub API? Ideally the entire list, not just the top fi

10条回答
  •  旧时难觅i
    2020-12-07 10:18

    I wrote a selenium python script to do this

    """
    Get all your repos contributed to for the past year.
    
    This uses Selenium and Chrome to login to github as your user, go through 
    your contributions page, and grab the repo from each day's contribution page.
    
    Requires python3, selenium, and Chrome with chromedriver installed.
    
    Change the username variable, and run like this:
    
    GITHUB_PASS="mypassword" python3 github_contributions.py
    """
    
    import os
    import sys
    import time
    from pprint import pprint as pp
    from urllib.parse import urlsplit
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    username = 'jessejoe'
    password = os.environ['GITHUB_PASS']
    
    repos = []
    driver = webdriver.Chrome()
    driver.get('https://github.com/login')
    
    driver.find_element_by_id('login_field').send_keys(username)
    password_elem = driver.find_element_by_id('password')
    password_elem.send_keys(password)
    password_elem.submit()
    
    # Wait indefinitely for 2-factor code
    if 'two-factor' in driver.current_url:
        print('2-factor code required, go enter it')
    while 'two-factor' in driver.current_url:
        time.sleep(1)
    
    driver.get('https://github.com/{}'.format(username))
    
    # Get all days that aren't colored gray (no contributions)
    contrib_days = driver.find_elements_by_xpath(
        "//*[@class='day' and @fill!='#eeeeee']")
    
    for day in contrib_days:
        day.click()
        # Wait until done loading
        WebDriverWait(driver, 10).until(
            lambda driver: 'loading' not in driver.find_element_by_css_selector('.contribution-activity').get_attribute('class'))
    
        # Get all contribution URLs
        contribs = driver.find_elements_by_css_selector('.contribution-activity a')
        for contrib in contribs:
            url = contrib.get_attribute('href')
            # Only care about repo owner and name from URL
            repo_path = urlsplit(url).path
            repo = '/'.join(repo_path.split('/')[0:3])
            if repo not in repos:
                repos.append(repo)
        # Have to click something else to remove pop-up on current day
        driver.find_element_by_css_selector('.vcard-fullname').click()
    
    driver.quit()
    pp(repos)
    

    It uses python and selenium to automate a Chrome browser to login to github, go to your contributions page, click each day and grab the repo name from any contributions. Since this page only shows 1 year's worth of activity, that's all you can get with this script.

提交回复
热议问题