Created
May 19, 2016 15:37
-
-
Save jessejoe/b1bb5e92d0e524cf1d0379f12fcf3171 to your computer and use it in GitHub Desktop.
Get github repos contributed to for the paste year.
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
""" | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment