diff --git a/gitfiti.py b/gitfiti.py index ac1afcdb5..b15b75b1c 100755 --- a/gitfiti.py +++ b/gitfiti.py @@ -207,7 +207,7 @@ def load_images(img_names): return loaded_imgs -def get_calendar(username, base_url): +def retrieve_contributions_calendar(username, base_url): """retrieves the GitHub commit calendar data for a username""" base_url = base_url + 'users/' + username @@ -219,24 +219,23 @@ def get_calendar(username, base_url): print(e) raise SystemExit - return page.read().decode('utf-8').splitlines() + return page.read().decode('utf-8') -def find_max_commits(calendar): - """finds the highest number of commits in one day""" - output = set() - - for line in calendar: +def parse_contributions_calendar(contributions_calendar): + """Yield daily counts extracted from the contributions SVG.""" + for line in contributions_calendar.splitlines(): for day in line.split(): if 'data-count=' in day: commit = day.split('=')[1] commit = commit.strip('"') - output.add(int(commit)) + yield int(commit) - output = list(output) - output.sort() - output.reverse() - return output[0] + +def find_max_daily_commits(contributions_calendar): + """finds the highest number of commits in one day""" + daily_counts = parse_contributions_calendar(contributions_calendar) + return max(daily_counts) def calculate_multiplier(max_commits): @@ -338,11 +337,11 @@ def main(): git_base = ghe if ghe else GITHUB_BASE_URL - cal = get_calendar(username, git_base) + contributions_calendar = retrieve_contributions_calendar(username, git_base) - max_commits = find_max_commits(cal) + max_daily_commits = find_max_daily_commits(contributions_calendar) - m = calculate_multiplier(max_commits) + m = calculate_multiplier(max_daily_commits) repo = request_user_input( 'Enter the name of the repository to use by gitfiti: ') @@ -361,7 +360,7 @@ def main(): 'Enter the word "gitfiti" to exceed your max\n' '(this option generates WAY more commits)\n' 'Any other input will cause the default matching behavior' - ).format(max_commits)) + ).format(max_daily_commits)) match = request_user_input() match = m if (match == 'gitfiti') else 1 diff --git a/tests/test_find_max_daily_commits.py b/tests/test_find_max_daily_commits.py new file mode 100644 index 000000000..d8950db2e --- /dev/null +++ b/tests/test_find_max_daily_commits.py @@ -0,0 +1,89 @@ +from gitfiti import find_max_daily_commits, parse_contributions_calendar + + +CONTRIBUTIONS_CALENDAR_SVG = '''\ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Jun + Jul + Aug + Sep + Oct + Nov + Dec + Jan + Feb + Mar + Apr + May + + M + + W + + F + + + +''' + + +def test_parse_contributions_calendar(): + expected = [ + 0, 0, 0, 0, 6, 0, 0, + 0, 0, 0, 0, 0, 0, 6, + 84, 16, 4, 8, 0, 0, 0, + 0, 25, 66, 20, 10, 0, 0, + 33, 9, 0, 0, 7, + ] + + actual = parse_contributions_calendar(CONTRIBUTIONS_CALENDAR_SVG) + + assert list(actual) == expected + + +def test_find_max_daily_commits(): + assert find_max_daily_commits(CONTRIBUTIONS_CALENDAR_SVG) == 84