From affdb361b5abbfd7f066dbd0b1353639a91c6c99 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Wed, 10 Feb 2016 10:22:59 +0100 Subject: [PATCH] new github2card.py --- contrib/faces/github2card.py | 66 ++++++++++++++++++++++++++++++++++ contrib/faces/gravatar2card.sh | 2 +- contrib/faces/image2card.sh | 2 +- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100755 contrib/faces/github2card.py diff --git a/contrib/faces/github2card.py b/contrib/faces/github2card.py new file mode 100755 index 0000000..f8a136d --- /dev/null +++ b/contrib/faces/github2card.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# github2card.py (Feb 2017) by Jan-Piet Mens +# Usage: github2card username +# +# Read the Github profile of `username' and obtain user's +# name and avatar_url to create a CARD JSON payload to +# be published retained to owntracks/username/device/info +# +# You probably want to do this: +# +# github2card.py jjolie > card.json +# mosquitto_pub -t owntracks/jane/phone/info -r -f card.json +# +# Note: the two commands cannot be piplelined (mosquitto_pub -l) +# because of a bug in mosquitto_pub: https://bugs.eclipse.org/bugs/show_bug.cgi?id=478917 +# If you have a newer version it should work fine. + +import requests +import base64 +import json +import sys + +def user_profile(username): + url = "https://api.github.com/users/" + username + r = requests.get(url) + if r.status_code != 200: + print "User not found; response:", r + sys.exit(1) + + profile = json.loads(r.content) + name = profile.get('name') + avatar_url = profile.get('avatar_url', None) + + if name is None: + print "User has no name; stopping" + sys.exit(1) + + if avatar_url is None: + print "No avatar for this user" + sys.exit(1) + + # sizing an avatar works only for "real" avatars; not + # for the github-generated thingies (identicons) + avatar_url = avatar_url + '&size=40' + + r = requests.get(avatar_url) + if r.status_code != 200: + print "Cannot retrieve avatar for this user:", r + sys.exit(1) + + f = open(username + ".png", "wb") + f.write(r.content) + f.close() + + card = { + '_type': 'card', + 'name' : name, + 'face' : base64.b64encode(r.content) + } + + print json.dumps(card) + + +if __name__ == '__main__': + (username) = sys.argv[1] + user_profile(username) diff --git a/contrib/faces/gravatar2card.sh b/contrib/faces/gravatar2card.sh index 9b951ec..b72470c 100755 --- a/contrib/faces/gravatar2card.sh +++ b/contrib/faces/gravatar2card.sh @@ -9,7 +9,7 @@ # You probably want to do this: # # gravatar2card.sh email-address "Jane Jolie" > card.json -# mosquitto_pub -t owntracks/jane/phone -r -f card.json +# mosquitto_pub -t owntracks/jane/phone/info -r -f card.json # # Note: the two commands cannot be piplelined (mosquitto_pub -l) # because of a bug in mosquitto_pub: https://bugs.eclipse.org/bugs/show_bug.cgi?id=478917 diff --git a/contrib/faces/image2card.sh b/contrib/faces/image2card.sh index 06259ac..92bf106 100755 --- a/contrib/faces/image2card.sh +++ b/contrib/faces/image2card.sh @@ -10,7 +10,7 @@ # You probably want to do this: # # image2card.sh filename.jpg "Jane Jolie" > card.json -# mosquitto_pub -t owntracks/jane/phone -f card.json +# mosquitto_pub -t owntracks/jane/phone/info -f card.json # # Note: the two commands cannot be piplelined (mosquitto_pub -l) # because of a bug in mosquitto_pub: https://bugs.eclipse.org/bugs/show_bug.cgi?id=478917