-
-
Save drtobbe/6488761 to your computer and use it in GitHub Desktop.
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 | |
# encoding: utf-8 | |
''' | |
linkedin-query.py | |
Created by Thomas Cabrol on 2012-12-03. | |
Customised by Rik Van Bruggen | |
Customised by Torbjorn Sjogren | |
Copyright (c) 2012 dataiku. All rights reserved. | |
Building the LinkedIn Graph | |
''' | |
import codecs | |
import simplejson | |
import oauth2 as oauth | |
CONSUMER_KEY = "your-consumer-key-here" | |
CONSUMER_SECRET = "your-consumer-secret-here" | |
OAUTH_TOKEN = "your-oauth-token-here" | |
OAUTH_TOKEN_SECRET = "your-oauth-token-secret-here" | |
OUTPUT = "myconnections.cypher" | |
YOURNAME = "Torbjorn Sjogren" | |
def linkedin_connections(): | |
# Use your credentials to build the oauth client | |
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET) | |
token = oauth.Token(key=OAUTH_TOKEN, secret=OAUTH_TOKEN_SECRET) | |
client = oauth.Client(consumer, token) | |
# Fetch first degree connections | |
resp, content = client.request('http://api.linkedin.com/v1/people/~/connections?format=json') | |
print resp | |
results = simplejson.loads(content) | |
print "connections: " + str(len(results["values"])) | |
print "" | |
# Loop thru the 1st degree connection and see how they connect to each other | |
output = codecs.open(OUTPUT, 'w', 'utf-8') | |
print >> output, "create ({name:'%s'});" % YOURNAME | |
for result in results["values"]: | |
con = "%s %s" % (result["firstName"].replace(",", " "), result["lastName"].replace(",", " ")) | |
print >> output, "create ({name:'%s'});" % con | |
print >> output, "start n=node:node_auto_index(name='%s'), m=node:node_auto_index(name='%s') create unique n-[:CONNECTED_TO]->m;" % (YOURNAME, con) | |
# This is the trick, use the search API to get related connections | |
u = "https://api.linkedin.com/v1/people/%s:(relation-to-viewer:(related-connections))?format=json" % result["id"] | |
print con + " - " + result["id"] | |
resp, content = client.request(u) | |
rels = simplejson.loads(content) | |
try: | |
for rel in rels['relationToViewer']['relatedConnections']['values']: | |
sec = "%s %s" % (rel["firstName"].replace(",", " "), rel["lastName"].replace(",", " ")) | |
print "connection: " + sec | |
print >> output, "start n=node:node_auto_index(name='%s'), m=node:node_auto_index(name='%s') create unique n-[:CONNECTED_TO]->m;" % (con, sec) | |
print "" | |
except: | |
print rels | |
print "" | |
pass | |
if __name__ == '__main__': | |
linkedin_connections() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment