Skip to content

Instantly share code, notes, and snippets.

@joeyparrish
Last active November 11, 2020 01:18
Show Gist options
  • Save joeyparrish/64acb427082853e502ee5389d1f92b58 to your computer and use it in GitHub Desktop.
Save joeyparrish/64acb427082853e502ee5389d1f92b58 to your computer and use it in GitHub Desktop.
Shaka T-Shirt Raffle Code
#!/bin/bash
# Find all contributors to date. These users will get increased odds of winning
# in the raffle.
# These repos will be checked to list all contributors.
REPOS="shaka-player shaka-packager shaka-streamer shaka-player-embedded"
API_URL="https://api.github.com/repos/"
# 4 pages of data covers all PRs to date in all repos. Some pages will be empty
# for some repos, and that's fine.
for i in 1 2 3 4; do
for repo in $REPOS; do
# Make API requests as the shaka-bot account, using a pre-generated token.
curl -u shaka-bot:$(cat token) \
"$API_URL/google/$repo/pulls?state=all&per_page=100&page=$i" \
> prs.$repo.$i.json
done
done
# Pull the GitHub usernames from all PRs, normalize to lowercase, and output a
# list of unique contributor usernames to a text file.
jq -r -s '.[][].user.login' prs.*.json | \
tr A-Z a-z | \
sort -u > contributors-nov-1.txt
#!/usr/bin/env python3
"""Select winners from the Shaka T-Shirt Raffle."""
import csv
import random
# Get a set of all past contributors, which was pre-computed using the GitHub
# API.
with open('contributors-nov-1.txt') as f:
contributors = set(f.read().split('\n'))
entrants = []
# Open a CSV download of the data from the Google Form for the raffle.
with open('entrants.csv') as f:
reader = csv.reader(f)
# Throw away the header row.
next(reader)
# Track usernames we have seen. No stuffing the ballot box!
seen = set()
for row in reader:
# Columns are timestamp, email, GitHub username.
_, email, username = row
# Usernames in the contributors list have already been normalized to
# lowercase. Do the same now for the CSV inputs.
username = username.lower()
# Some people managed to add spaces to the form, so strip the usernames of
# leading and trailing whitespace, too.
username = username.strip()
# Track usernames we have seen. No stuffing the ballot box!
if username in seen:
continue
seen.add(username)
# If the entrant is a contributor, they get 5x the chances to win.
chances = 5 if username in contributors else 1
for i in range(chances):
entrants.append((email, username, chances))
# Shuffle the list in place.
random.shuffle(entrants)
# Now the top entries are the winners. Past contributors got multiple entries,
# and they are therefore more likely to have an entry close to the top of the
# list. But now we should remove second and subsequent entries so that each
# person is in the list only once.
winners = []
for entry in entrants:
if entry not in winners:
winners.append(entry)
# Finally, a human process will screen the top entrants for eligibilty. Some
# may have to be skipped if, for example, US law would prohibit us from shipping
# to them for some reason. We don't know in advance how many times this could
# happen, so we will not limit the output to 20 entries.
for winner in winners:
print(winner)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment