-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedin_wrapper.py
69 lines (55 loc) · 2.25 KB
/
linkedin_wrapper.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
from flask import request, redirect
from linkedin import linkedin
class LinkedInWrapper(object):
def __init__(self, linkedin_key, linkedin_secret, callback_url, token=None):
self.authentication = None
self.application = None
self.token = None
if token is None:
self.authentication = linkedin.LinkedInAuthentication(
linkedin_key,
linkedin_secret,
callback_url,
linkedin.PERMISSIONS.enums.values())
else:
self.application = linkedin.LinkedInApplication(token=token)
self.token = token
def authorize(self):
return redirect(self.authentication.authorization_url)
def authorize_callback(self):
if 'code' not in request.args:
return None, None
self.authentication.authorization_code = request.args['code']
self.authentication.get_access_token()
self.token = self.authentication.token[0]
self.application = linkedin.LinkedInApplication(self.authentication)
def get_token(self):
return self.token
def get_profile(self):
if self.application is None:
raise LinkedInUnauthorized()
try:
user = self.application.get_profile(selectors=['id', 'formatted-name'])
except linkedin.LinkedInError:
raise LinkedInUnauthorized()
return (
'linkedin$' + user['id'],
user.get('formattedName')
)
def get_companies_worked_at(self):
if self.application is None:
raise LinkedInUnauthorized()
try:
positions = self.application.get_profile(selectors=['positions']).get('positions').get('values')
except linkedin.LinkedInError:
raise LinkedInUnauthorized()
companies = map(lambda position: position.get('company'), positions)
unique_companies = []
for cur_company in companies:
if (cur_company.get('id') is not None
and len(filter(lambda company: cur_company.get('id') == company.get('id'), unique_companies)) == 0):
unique_companies.append(cur_company)
return unique_companies
class LinkedInUnauthorized(Exception):
def __init__(self):
pass