|
| 1 | +__author__ = 'Samuel Marks <samuelmarks@gmail.com>' |
| 2 | +__version__ = '0.1.0' |
| 3 | + |
| 4 | +from SocketServer import ThreadingTCPServer |
| 5 | +from SimpleHTTPServer import SimpleHTTPRequestHandler |
| 6 | +from webbrowser import open_new_tab |
| 7 | +from json import dumps |
| 8 | +from urlparse import urlparse |
| 9 | +from os import environ |
| 10 | +from types import NoneType |
| 11 | + |
| 12 | +from linkedin.linkedin import LinkedInAuthentication, LinkedInApplication, PERMISSIONS |
| 13 | + |
| 14 | +PORT = 8080 |
| 15 | + |
| 16 | + |
| 17 | +class LinkedInWrapper(object): |
| 18 | + """ Simple namespacing """ |
| 19 | + API_KEY = environ.get('LINKEDIN_API_KEY') |
| 20 | + API_SECRET = environ.get('LINKEDIN_API_SECRET') |
| 21 | + RETURN_URL = 'http://localhost:{0}/code'.format(globals()['PORT']) |
| 22 | + authentication = LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL, PERMISSIONS.enums.values()) |
| 23 | + application = LinkedInApplication(authentication) |
| 24 | + |
| 25 | + |
| 26 | +liw = LinkedInWrapper() |
| 27 | +run_already = False |
| 28 | +params_to_d = lambda params: { |
| 29 | + l[0]: l[1] for l in map(lambda j: j.split('='), urlparse(params).query.split('&')) |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +class CustomHandler(SimpleHTTPRequestHandler): |
| 34 | + def json_headers(self, status_code=200): |
| 35 | + self.send_response(status_code) |
| 36 | + self.send_header('Content-type', 'application/json') |
| 37 | + self.end_headers() |
| 38 | + |
| 39 | + def do_GET(self): |
| 40 | + parsedurl = urlparse(self.path) |
| 41 | + authed = type(liw.authentication.token) is not NoneType |
| 42 | + |
| 43 | + if parsedurl.path == '/code': |
| 44 | + self.json_headers() |
| 45 | + |
| 46 | + liw.authentication.authorization_code = params_to_d(self.path).get('code') |
| 47 | + self.wfile.write(dumps({'access_token': liw.authentication.get_access_token(), |
| 48 | + 'routes': filter(lambda d: not d.startswith('_'), dir(liw.application))})) |
| 49 | + elif parsedurl.path == '/routes': |
| 50 | + self.json_headers() |
| 51 | + |
| 52 | + self.wfile.write(dumps({'routes': filter(lambda d: not d.startswith('_'), dir(liw.application))})) |
| 53 | + elif not authed: |
| 54 | + self.json_headers() |
| 55 | + |
| 56 | + if not globals()['run_already']: |
| 57 | + open_new_tab(liw.authentication.authorization_url) |
| 58 | + globals()['run_already'] = True |
| 59 | + self.wfile.write(dumps({'path': self.path, 'authed': type(liw.authentication.token) is NoneType})) |
| 60 | + elif authed and len(parsedurl.path) and parsedurl.path[1:] in dir(liw.application): |
| 61 | + self.json_headers() |
| 62 | + self.wfile.write(dumps(getattr(liw.application, parsedurl.path[1:])())) |
| 63 | + else: |
| 64 | + self.json_headers(412) |
| 65 | + self.wfile.write(dumps({'error': 'NotImplemented'})) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == '__main__': |
| 69 | + httpd = ThreadingTCPServer(('localhost', PORT), CustomHandler) |
| 70 | + |
| 71 | + print 'Server started on port:', PORT |
| 72 | + httpd.serve_forever() |
0 commit comments