-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedin-2-query.py
157 lines (119 loc) · 5.22 KB
/
linkedin-2-query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#! C:\python27\python
# encoding: utf-8
"""
linkedin-2-query.py
inspired by Thomas Cabrol
Building the LinkedIn Graph
"""
try:
import json
except ImportError:
import simplejson as json
import oauth2 as oauth
import urlparse
import codecs
import shutil
<<<<<<< HEAD
CONSUMER_KEY = "js6zocdd9j86"
CONSUMER_SECRET = "alkyBWF4yidK20sb"
OAUTH_TOKEN = "be8fa919-c03a-465b-b4b8-631cf4cd5eb9"
OAUTH_TOKEN_SECRET = "e939a72d-8b7b-4278-bc05-f27014c20db3"
=======
CONSUMER_KEY = "your-api-key"
CONSUMER_SECRET = "your-secret-key"
OAUTH_TOKEN = "your-oauth-token"
OAUTH_TOKEN_SECRET = "your-oauth-secret"
>>>>>>> f31b4ee84bdeeb50ee80dd5248ccc74e3a29d6e9
start = 1357 # Starting location within the result set for paginated returns. Ranges are specified with a starting index and a number of results (count) to return.
count = 500 #number of results to return. You may specify any number. Default and max page size is 500. Implement pagination to retrieve more than 500 connections.
#end = start + count - 1
# start + count = nextStart
# nextStart =
filename = "linked_%s.csv" % start
filename_ids = "linked_ids_%s.csv" % start
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)
# daily limit per developer is 500 calls to the "People Search" API
# http://developer.linkedin.com/documents/throttle-limits
counter_GetConnectionProfile_APIcall = 0
cap_GCP_APIcalls = 500
# track how many connections been queried
counter_results = 0
# Fetch first degree connections
v = "https://api.linkedin.com/v1/people/~/connections?format=json&start=%s&count=%s" % (start, count)
resp, content = client.request(v)
results = json.loads(content)
# File that will store the results
output = codecs.open(filename, 'w', 'utf-8')
output_ids = codecs.open(filename_ids, 'w', 'utf-8')
def extract_connections(rels, con):
print '*'
try:
for rel in rels['relationToViewer']['relatedConnections']['values']:
sec = "%s %s" % (rel["firstName"].replace(","," "), rel["lastName"].replace(",", " "))
y = "%s,%s" % (con, sec)
print y
print >>output, y
except:
print "Exception encountered, exiting loop"
pass
print '*'
# Loop thru the 1st degree connections and see how they connect to each other
for result in results.get("values", "NA"):
#if result["id"] == "CjLypibuy8":
resultId = result["id"]
#resultId = CjLypibuy8
con = "%s %s" % (result["firstName"].replace(",", " "), result["lastName"].replace(",", " "))
print >>output, "%s,%s" % ("Micah Stubbs", con)
print "%s,%s" % ("Micah Stubbs", con)
print >>output_ids, "%s,%s" % (con, resultId)
print "%s,%s" % (con, resultId)
# This is the trick, use the search API to get related connections
# gather first 20 mutual connections, and the total
mutualConnectionsStart = 0
relatedConnectionsTotal = 0
u = "https://api.linkedin.com/v1/people/%s:(relation-to-viewer:(related-connections))?format=json&count=20&start=%s" % (resultId, mutualConnectionsStart)
resp, content = client.request(u)
counter_GetConnectionProfile_APIcall += 1
rels = json.loads(content)
try:
relatedConnectionsTotal = int(rels['relationToViewer']['relatedConnections']['_total'])
print relatedConnectionsTotal
extract_connections(rels,con)
except:
print "Exception encountered, going to next connection"
pass
counter_results += 1
if counter_GetConnectionProfile_APIcall >= cap_GCP_APIcalls:
print "API Call limit reached. %d call%s made to LinkedIn's Get Connection Profile API" % (counter_GetConnectionProfile_APIcall, "s"[counter_GetConnectionProfile_APIcall==1:])
if (relatedConnectionsTotal) > (mutualConnectionsStart + 20):
z = counter_results - 1
else:
z = counter_results
print "mutual connections retrieved for %d connection%s" % (z, "s"[z==1:])
return
# gather mutual connections 20 at a time, if the total is larger than the starting amount plus 20
while (relatedConnectionsTotal - 1) > (mutualConnectionsStart + 20):
mutualConnectionsStart += 20
u = "https://api.linkedin.com/v1/people/%s:(relation-to-viewer:(related-connections))?format=json&count=20&start=%s" % (resultId, mutualConnectionsStart)
resp, content = client.request(u)
counter_GetConnectionProfile_APIcall += 1
rels = json.loads(content)
extract_connections(rels,con)
if counter_GetConnectionProfile_APIcall >= cap_GCP_APIcalls:
print "API Call limit reached. %d call%s made to LinkedIn's Get Connection Profile API" % (counter_GetConnectionProfile_APIcall, "s"[counter_GetConnectionProfile_APIcall==1:])
if (relatedConnectionsTotal) > (mutualConnectionsStart + 20):
z = counter_results - 1
else:
z = counter_results
print "complete set of mutual connections retrieved for %d connection%s" % (z, "s"[z==1:])
return
# Print some stats to the console
print "%d call%s made to LinkedIn's Get Connection Profile API" % (counter_GetConnectionProfile_APIcall, "s"[counter_GetConnectionProfile_APIcall==1:])
z = counter_results
print "mutual connections retrieved for %d connection%s" % (z, "s"[z==1:])
if __name__ == '__main__':
linkedin_connections()