Skip to content

Commit

Permalink
Add python 3.4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Darune committed May 4, 2015
1 parent 49eeb55 commit aa76e20
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
27 changes: 16 additions & 11 deletions examples/http_api.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
__author__ = 'Samuel Marks <[email protected]>'
__version__ = '0.1.0'

from SocketServer import ThreadingTCPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse

from socketserver import ThreadingTCPServer
from http.server import SimpleHTTPRequestHandler

from webbrowser import open_new_tab
from json import dumps
from urlparse import urlparse
from os import environ
from types import NoneType


from linkedin.linkedin import LinkedInAuthentication, LinkedInApplication, PERMISSIONS

Expand Down Expand Up @@ -38,35 +43,35 @@ def json_headers(self, status_code=200):

def do_GET(self):
parsedurl = urlparse(self.path)
authed = type(liw.authentication.token) is not NoneType
authed = liw.authentication.token is not None

if parsedurl.path == '/code':
self.json_headers()

liw.authentication.authorization_code = params_to_d(self.path).get('code')
self.wfile.write(dumps({'access_token': liw.authentication.get_access_token(),
'routes': filter(lambda d: not d.startswith('_'), dir(liw.application))}))
'routes': list(filter(lambda d: not d.startswith('_'), dir(liw.application)))}).encode('utf8'))
elif parsedurl.path == '/routes':
self.json_headers()

self.wfile.write(dumps({'routes': filter(lambda d: not d.startswith('_'), dir(liw.application))}))
self.wfile.write(dumps({'routes': list(filter(lambda d: not d.startswith('_'), dir(liw.application)))}).encode('utf8'))
elif not authed:
self.json_headers()

if not globals()['run_already']:
open_new_tab(liw.authentication.authorization_url)
globals()['run_already'] = True
self.wfile.write(dumps({'path': self.path, 'authed': type(liw.authentication.token) is NoneType}))
self.wfile.write(dumps({'path': self.path, 'authed': type(liw.authentication.token) is None}).encode('utf8'))
elif authed and len(parsedurl.path) and parsedurl.path[1:] in dir(liw.application):
self.json_headers()
self.wfile.write(dumps(getattr(liw.application, parsedurl.path[1:])()))
self.wfile.write(dumps(getattr(liw.application, parsedurl.path[1:])()).encode('utf8'))
else:
self.json_headers(501)
self.wfile.write(dumps({'error': 'NotImplemented'}))
self.wfile.write(dumps({'error': 'NotImplemented'}).encode('utf8'))


if __name__ == '__main__':
httpd = ThreadingTCPServer(('localhost', PORT), CustomHandler)

print 'Server started on port:', PORT
print('Server started on port:{}'.format(PORT))
httpd.serve_forever()
24 changes: 15 additions & 9 deletions linkedin/linkedin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import contextlib
import hashlib
import random
import urllib

try:
from urllib.parse import quote, quote_plus
except ImportError:
from urllib import quote, quote_plus

import requests
from requests_oauthlib import OAuth1
Expand Down Expand Up @@ -93,7 +98,7 @@ def authorization_url(self):
'redirect_uri': self.redirect_uri}
# urlencode uses quote_plus when encoding the query string so,
# we ought to be encoding the qs by on our own.
qsl = ['%s=%s' % (urllib.quote(k), urllib.quote(v)) for k, v in qd.items()]
qsl = ['%s=%s' % (quote(k), quote(v)) for k, v in qd.items()]
return '%s?%s' % (self.AUTHORIZATION_URL, '&'.join(qsl))

@property
Expand All @@ -102,7 +107,8 @@ def last_error(self):

def _make_new_state(self):
return hashlib.md5(
'%s%s' % (random.randrange(0, 2 ** 63), self.secret)).hexdigest()
'{}{}'.format(random.randrange(0, 2 ** 63), self.secret).encode("utf8")
).hexdigest()

def get_access_token(self, timeout=60):
assert self.authorization_code, 'You must first get the authorization code'
Expand Down Expand Up @@ -174,7 +180,7 @@ def get_profile(self, member_id=None, member_url=None, selectors=None,
else:
url = '%s/id=%s' % (ENDPOINTS.PEOPLE, str(member_id))
elif member_url:
url = '%s/url=%s' % (ENDPOINTS.PEOPLE, urllib.quote_plus(member_url))
url = '%s/url=%s' % (ENDPOINTS.PEOPLE, quote_plus(member_url))
else:
url = '%s/~' % ENDPOINTS.PEOPLE
if selectors:
Expand All @@ -200,7 +206,7 @@ def get_picture_urls(self, member_id=None, member_url=None,
url = '%s/id=%s/picture-urls::(original)' % (ENDPOINTS.PEOPLE, str(member_id))
elif member_url:
url = '%s/url=%s/picture-urls::(original)' % (ENDPOINTS.PEOPLE,
urllib.quote_plus(member_url))
quote_plus(member_url))
else:
url = '%s/~/picture-urls::(original)' % ENDPOINTS.PEOPLE

Expand All @@ -214,7 +220,7 @@ def get_connections(self, member_id=None, member_url=None, selectors=None,
url = '%s/id=%s/connections' % (ENDPOINTS.PEOPLE, str(member_id))
elif member_url:
url = '%s/url=%s/connections' % (ENDPOINTS.PEOPLE,
urllib.quote_plus(member_url))
quote_plus(member_url))
else:
url = '%s/~/connections' % ENDPOINTS.PEOPLE
if selectors:
Expand All @@ -230,7 +236,7 @@ def get_memberships(self, member_id=None, member_url=None, group_id=None,
url = '%s/id=%s/group-memberships' % (ENDPOINTS.PEOPLE, str(member_id))
elif member_url:
url = '%s/url=%s/group-memberships' % (ENDPOINTS.PEOPLE,
urllib.quote_plus(member_url))
quote_plus(member_url))
else:
url = '%s/~/group-memberships' % ENDPOINTS.PEOPLE

Expand Down Expand Up @@ -307,7 +313,7 @@ def like_post(self, post_id, action):
url = '%s/%s/relation-to-viewer/is-liked' % (ENDPOINTS.POSTS, str(post_id))
try:
self.make_request('PUT', url, data=json.dumps(action))
except (requests.ConnectionError, requests.HTTPError), error:
except (requests.ConnectionError, requests.HTTPError) as error:
raise LinkedInError(error.message)
else:
return True
Expand All @@ -319,7 +325,7 @@ def comment_post(self, post_id, comment):
url = '%s/%s/comments' % (ENDPOINTS.POSTS, str(post_id))
try:
self.make_request('POST', url, data=json.dumps(post))
except (requests.ConnectionError, requests.HTTPError), error:
except (requests.ConnectionError, requests.HTTPError) as error:
raise LinkedInError(error.message)
else:
return True
Expand Down
35 changes: 20 additions & 15 deletions linkedin/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# -*- coding: utf-8 -*-
import requests
from .exceptions import LinkedInError, get_exception_for_error_code

try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import sys
from io import StringIO

try:
import simplejson as json
Expand All @@ -16,6 +13,22 @@
import json


if sys.version_info < (3,):
import __builtin__

def to_utf8(x):
return __builtin__.unicode(x)

def to_string(x):
return str(x)
else:
def to_utf8(x):
return x

def to_string(x):
return x


def enum(enum_type='enum', base_classes=None, methods=None, **attrs):
"""
Generates a enumeration with the given attributes.
Expand All @@ -31,21 +44,13 @@ def __init__(instance, *args, **kwargs):
methods = {}

base_classes = base_classes + (object,)
for k, v in methods.iteritems():
for k, v in methods.items():
methods[k] = classmethod(v)

attrs['enums'] = attrs.copy()
methods.update(attrs)
methods['__init__'] = __init__
return type(enum_type, base_classes, methods)


def to_utf8(st):
if isinstance(st, unicode):
return st.encode('utf-8')
else:
return bytes(st)

return type(to_string(enum_type), base_classes, methods)

def raise_for_error(response):
try:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
future==0.14.3
requests
requests_oauthlib
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Natural Language :: English',
],
keywords='linkedin python',
Expand Down

0 comments on commit aa76e20

Please sign in to comment.