Created
May 23, 2017 04:30
-
-
Save eteq/63af3e653b2d48f8e4f5deab622f8bbb to your computer and use it in GitHub Desktop.
Script to get all the gravatars in a git repo
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
#!/usr/bin/env python | |
import os | |
import hashlib | |
import argparse | |
import subprocess | |
from urllib import request, error | |
start_dir = os.path.abspath('.') | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-w', '--where', default=start_dir) | |
parser.add_argument('-o', '--output', default='avatars') | |
args = parser.parse_args() | |
if os.path.exists(args.output): | |
raise ValueError('output dir already exists') | |
res = subprocess.check_output('git shortlog -sne', shell=True, cwd=args.where) | |
nameaddr = [line.split('\t')[-1] for line in res.decode().strip().split('\n')] | |
nametoaddr = {} | |
for na in nameaddr: | |
n, a = na.split('<') | |
n = n.strip() | |
a = a.strip() | |
if a.endswith('>'): | |
a = a[:-1] | |
nametoaddr[n] = a | |
os.mkdir(args.output) | |
for n, a in nametoaddr.items(): | |
hsh = hashlib.md5(a.encode()).hexdigest() | |
gravatar_url = 'http://www.gravatar.com/avatar/' + hsh + '?d=404' | |
ofn = os.path.join(args.output, n+'.jpg') | |
print('Writing', ofn, 'from', gravatar_url) | |
try: | |
request.urlretrieve(gravatar_url, ofn) | |
except error.HTTPError as e: | |
print('Failed due to:', e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment