-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweb.py
101 lines (86 loc) · 3.21 KB
/
web.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
from linkedin import config, LinkedInApi, parser, nlp
from flask import Flask, render_template, request, session
from flask.ext.assets import Environment, Bundle
import oauth2
import urllib
import urlparse
import json
app = Flask(__name__)
app.secret_key = 'A0Zr98212asd3yasdf21WX/,?RT'
assets = Environment(app)
jsD3 = Bundle('js/d3.v2.js', 'js/d3.layout.cloud.js', output='gen/packed.js')
assets.register('js_d3', jsD3)
cfg = config.config_parser()
@app.route("/")
def index():
consumer_key = cfg.get('auth', 'consumer_key')
return render_template('index.html', key=consumer_key)
@app.route("/token-exchange", methods=['POST'])
def token_exchange():
oauth_token = None
oauth_secret = None
if session.get('linkedin_oauth_token2'):
print "SESSION"
oauth_token = session.get('linkedin_oauth_token')
oauth_secret = session.get('linkedin_oauth_secret')
access_token = oauth_token
else:
print "REQUEST"
oauth_token = request.cookies.get('linkedin_oauth_%s' % cfg.get('auth', 'consumer_key'))
oauth_token = urllib.unquote(oauth_token)
oauth_token = json.loads(oauth_token)
access_token = oauth_token.get('access_token')
access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken?scope=r_network'
consumer = oauth2.Consumer(
key=cfg.get('auth', 'consumer_key'),
secret=cfg.get('auth', 'consumer_secret')
)
client = oauth2.Client(consumer)
resp, content = client.request(access_token_url, "POST", body='xoauth_oauth2_access_token=%s' % access_token)
qsl = urlparse.parse_qsl(content)
request_token = dict(qsl)
if 'oauth_problem' in request_token:
return "BAD"
oauth_secret = request_token.get('oauth_token_secret')
oauth_token = request_token.get('oauth_token')
session['linkedin_oauth_token'] = oauth_token
session['linkedin_oauth_secret'] = oauth_secret
print oauth_token
print oauth_secret
return "GOOD"
@app.route("/profile/<profileId>")
def profile(profileId):
authCookie = request.cookies.get('linkedin_oauth_%s' % cfg.get('auth', 'consumer_key'))
authString = urllib.unquote(authCookie)
# profile
LinkedIn = LinkedInApi.LinkedInApi(session['linkedin_oauth_token'], session['linkedin_oauth_secret'])
profile = parser.parse(LinkedIn.profile(profileId))
print
#print profile
print
positions = profile['positions']['values']
raw = nlp.ascii(parser.raw(profile))
tokens = nlp.tokenize(raw)
tokens = nlp.normalize(tokens)
token_str = ' '.join(tokens)
freq = ' '.join(nlp.freq_dist(tokens, 3))
# connections
parser_parse = parser.parse(LinkedIn.connections(profileId))
# we can't (yet) get to our connections connections
if 'message' in parser_parse.keys():
print parser_parse
connections = []
else:
connections = parser_parse['values']
# context
context = {}
context['id'] = authString
context['profile'] = profile
context['positions'] = positions
context['tokens'] = token_str
context['connections'] = connections
context['frequent'] = freq
return render_template('profile.html', **context)
if __name__ == "__main__":
# app.run()
app.run(debug=True)