Created
October 3, 2011 06:49
-
-
Save AdrienLemaire/1258579 to your computer and use it in GitHub Desktop.
Create a circle on Google+ for your github contacts
This file contains 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 | |
''' | |
File: create_github_circle.py | |
Author: Adrien Lemaire | |
Description: Generate a csv file for google+ using the github api 3 | |
''' | |
import pycurl | |
import json | |
from local_settings import USERNAME, PASSWORD | |
URL = "https://api.github.com/" | |
CREDENTIALS = "%s:%s" % (USERNAME, PASSWORD) | |
followers = [] | |
emails = [] | |
def get_followers(data): | |
"Save the followers username in the followers dict" | |
dico = json.loads(data) | |
for contact in dico: | |
followers.append(contact["login"]) | |
def get_email(data): | |
"Save a tuple (name, email) for each follower in the emails dict" | |
contact = json.loads(data) | |
if contact.get("email", False): | |
emails.append((contact["name"], contact["email"])) | |
def request(url, callback): | |
"Curl request to github" | |
print "url: %s; callback: %s" % (url, callback) | |
url = str(url) # fix bug with unicode | |
conn = pycurl.Curl() | |
conn.setopt(pycurl.SSL_VERIFYPEER, 0) | |
conn.setopt(pycurl.USERPWD, CREDENTIALS) | |
conn.setopt(pycurl.URL, url) | |
conn.setopt(pycurl.WRITEFUNCTION, callback) | |
conn.perform() | |
conn.close() | |
def save_csv(data): | |
"Save the data in a correct format to upload on google circles" | |
with open("github.csv", "w") as f: | |
f.write("Name,Email\n") | |
for user in data: | |
f.write("%s\n" % ",".join(user)) | |
def main(): | |
""" | |
Get the list of your github followers email for any purpose, like creating | |
a Github circle in your google+ | |
""" | |
followers_url = URL + "user/followers" | |
# 1. Get list of usernames | |
request(followers_url, get_followers) | |
# 2. For each username, get his email if exist | |
for follower in followers: | |
follower_url = "%susers/%s" % (URL, follower) | |
request(follower_url, get_email) | |
save_csv(emails) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment