QtPass  1.2.3
Multi-platform GUI for pass, the standard unix password manager.
avatars.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 #
3 # Based on:
4 # https://code.google.com/p/gource/wiki/GravatarExample
5 # https://gist.github.com/macagua/5c2f5e4e38df92aae7fe
6 #
7 # Usage with Gource: gource --user-image-dir .git/avatar/
8 #
9 # Get list of authors + email with git log
10 # git log --format='%aN|%aE' | sort -u
11 #
12 # Get list of authors + email with hg log (todo)
13 # hg log --template 'author: {author}\n'
14 #
15 
16 import requests
17 import getpass
18 import os
19 import subprocess
20 import hashlib
21 from time import sleep
22 import sys
23 
24 def md5_hex(text):
25  m = hashlib.md5()
26  m.update(text.encode('ascii', errors='ignore'))
27  return m.hexdigest()
28 
29 
30 def get_data(api_request):
31  r = requests.get(api_request, headers={'Authorization': 'token ff91742de1e7a36921d16da90623474266b97abb'})
32  data = r.json()
33 
34  if "message" in data.keys():
35  # Countdown
36  # http://stackoverflow.com/questions/3249524/print-in-one-line-dynamically-python
37  for k in range(1, 60):
38  remaining = 60 - k
39  sys.stdout.write("\r%d seconds remaining " % remaining)
40  sys.stdout.flush()
41  sleep(1)
42  sys.stdout.write("\n")
43  # Another request
44  r = requests.get(api_request, headers={'Authorization': 'token ff91742de1e7a36921d16da90623474266b97abb'})
45  data = r.json()
46  else:
47  pass
48 
49  # Return data
50  return data
51 
52 if __name__ == "__main__":
53 
54  # Clear screen
55  os.system('cls' if os.name == 'nt' else 'clear')
56 
57  # Configure the path of the git project
58  projectpath = "/Users/annejan/Projects/qtpass"
59  gitpath = os.path.join(projectpath, '.git')
60  output_dir = os.path.join(gitpath, 'avatar')
61 
62  # Create the folder for storing the images. It's in the .git folder, so it won't be tracked by git
63  if not os.path.exists(output_dir):
64  os.makedirs(output_dir)
65 
66  # Get the authors from the git log
67  gitlog = subprocess.check_output(
68  ['git', 'log', '--pretty=format:%ae|%an'], cwd=projectpath)
69  authors = set(gitlog.decode('ascii', errors='ignore').splitlines())
70  print ""
71  print "USERS:"
72  print(authors)
73 
74  # Check each author
75  for author in authors:
76  # Get e-mail and name from log
77  email, name = author.split('|')
78 
79  if email == 'brouwer@annejan.com':
80  continue
81 
82  print ""
83  print "Checking", name, email
84  # Try to find the user on GitHub with the e-mail
85  api_request = "https://api.github.com/search/users?utf8=%E2%9C%93&q=" + \
86  email + "+in%3Aemail&type=Users"
87  data = get_data(api_request)
88 
89  # Check if the user was found
90  if "items" in data.keys():
91  if len(data["items"]) == 1:
92  url = data["items"][0]["avatar_url"]
93  print "Avatar url:", url
94  else:
95  # Try to find the user on GitHub with the name
96  api_request = "https://api.github.com/search/users?utf8=%E2%9C%93&q=" + \
97  name + "+in%3Aname&type=Users"
98  data = get_data(api_request)
99 
100  # Check if the user was found
101  if "items" in data.keys():
102  if len(data["items"]) == 1:
103  url = data["items"][0]["avatar_url"]
104  print "Avatar url:", url
105  # Eventually try to find the user with Gravatar
106  else:
107  url = "http://www.gravatar.com/avatar/" + \
108  md5_hex(email) + "?d=identicon&s=" + str(90)
109  print "Avatar url:", url
110 
111  # Finally retrieve the image
112  try:
113  output_file = os.path.join(output_dir, name + '.png')
114  if not os.path.exists(output_file):
115  r = requests.get(url)
116  if r.ok:
117  with open(output_file, 'wb') as img:
118  img.write(r.content)
119  except:
120  print "There was an error with", name, email
def md5_hex(text)
Definition: avatars.py:24
def get_data(api_request)
Definition: avatars.py:30