Skip to content

Instantly share code, notes, and snippets.

@openp2pdesign
Forked from macagua/GourceGravatarExample.py
Last active December 10, 2021 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save openp2pdesign/15db406825a4b35783e2 to your computer and use it in GitHub Desktop.
Save openp2pdesign/15db406825a4b35783e2 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
#
# Based on:
# https://code.google.com/p/gource/wiki/GravatarExample
# https://gist.github.com/macagua/5c2f5e4e38df92aae7fe
# Usage with Gource: gource --user-image-dir .git/avatar/
# Get list of authors + email with git log
# git log --format='%aN|%aE' | sort -u
#
# Get list of authors + email with hg log (todo)
# hg log --template 'author: {author}\n'
#
import requests
import getpass
import os
import subprocess
import hashlib
from time import sleep
import sys
username = ""
password = ""
def md5_hex(text):
m = hashlib.md5()
m.update(text.encode('ascii', errors='ignore'))
return m.hexdigest()
def get_data(api_request):
global username
global password
r = requests.get(api_request, auth=(username, password))
data = r.json()
if "message" in data.keys():
# Countdown
# http://stackoverflow.com/questions/3249524/print-in-one-line-dynamically-python
for k in range(1, 60 * 15):
remaining = 60 * 15 - k
sys.stdout.write("\r%d seconds remaining " % remaining)
sys.stdout.flush()
sleep(1)
sys.stdout.write("\n")
# Another request
r = requests.get(api_request, auth=(username, password))
data = r.json()
else:
pass
# Return data
return data
if __name__ == "__main__":
global username
global password
# Clear screen
os.system('cls' if os.name == 'nt' else 'clear')
# Login to the GitHub API
username = raw_input("Enter your GitHub username: ")
password = getpass.getpass("Enter yor GitHub password: ")
# Configure the path of the git project
projectpath = "/Users/massimo/Documents/FabAcademy2015/FabAcademy2015-Opendot"
gitpath = os.path.join(projectpath, '.git')
output_dir = os.path.join(gitpath, 'avatar')
# Create the folder for storing the images. It's in the .git folder, so it won't be tracked by git
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Get the authors from the git log
gitlog = subprocess.check_output(
['git', 'log', '--pretty=format:%ae|%an'], cwd=projectpath)
authors = set(gitlog.decode('ascii', errors='ignore').splitlines())
print ""
print "USERS:"
print(authors)
# Check each author
for author in authors:
# Get e-mail and name from log
email, name = author.split('|')
print ""
print "Checking", name, email
# Try to find the user on GitHub with the e-mail
api_request = "https://api.github.com/search/users?utf8=%E2%9C%93&q=" + \
email + "+in%3Aemail&type=Users"
data = get_data(api_request)
# Check if the user was found
if "items" in data.keys():
if len(data["items"]) == 1:
url = data["items"][0]["avatar_url"]
print "Avatar url:", url
else:
# Try to find the user on GitHub with the name
api_request = "https://api.github.com/search/users?utf8=%E2%9C%93&q=" + \
name + "+in%3Aname&type=Users"
data = get_data(api_request)
# Check if the user was found
if "items" in data.keys():
if len(data["items"]) == 1:
url = data["items"][0]["avatar_url"]
print "Avatar url:", url
# Eventually try to find the user with Gravatar
else:
url = "http://www.gravatar.com/avatar/" + \
md5_hex(email) + "?d=identicon&s=" + str(90)
print "Avatar url:", url
# Finally retrieve the image
try:
output_file = os.path.join(output_dir, name + '.png')
if not os.path.exists(output_file):
r = requests.get(url)
if r.ok:
with open(output_file, 'wb') as img:
img.write(r.content)
except:
print "There was an error with", name, email
@AJenbo
Copy link

AJenbo commented Oct 14, 2021

Couldn't figure out hot to install requests and the script seams hard coded to your Windows home folder, and written for an older version of Python, so ported this to PHP instead.

https://gist.github.com/AJenbo/544aaf50bf691975d6d38b72e0a6ab21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment