Skip to content

Commit

Permalink
Merge pull request #4 from ta2xeo/python3
Browse files Browse the repository at this point in the history
Python3 support. Fix coding style for pep8.
  • Loading branch information
clsung committed Feb 24, 2016
2 parents 80e8140 + 220fa21 commit 8891823
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 224 deletions.
26 changes: 20 additions & 6 deletions README.markdown
Expand Up @@ -8,6 +8,20 @@ About
Plurk-OAuth is a wrapper for [Plurk API 2.0 beta](http://www.plurk.com/API/2)
You will need to [Sign Up](http://www.plurk.com/PlurkApp/register) for your own CUSTOMER TOKENs.

Installation
----
simply:
```
$ pip install plurk-oauth
```

for development:
```
$ git clone git@github.com:clsung/plurk-oauth.git
$ cd plurk-oauth
$ pip install -e .
```

API.keys
----
You will need to save CONSUMER_KEY/CONSUMER_SECRET in API.keys, the
Expand All @@ -18,31 +32,31 @@ format is JSON. The example is below:
Example with API.keys
----
``` python
from PlurkAPI import PlurkAPI
from plurk_oauth import PlurkAPI

plurk = PlurkAPI.fromfile(<path_to_API.keys>)
print plurk.callAPI('/APP/Profile/getOwnProfile')
print(plurk.callAPI('/APP/Profile/getOwnProfile'))
```

Example with ACCESS_TOKEN
----
``` python
from PlurkAPI import PlurkAPI
from plurk_oauth import PlurkAPI

plurk = PlurkAPI(CONSUMER_KEY, CONSUMER_SECRET)
plurk.authorize(ACCESS_TOKEN,ACCESS_TOKEN_SECRET)
print plurk.callAPI('/APP/Profile/getOwnProfile')
print(plurk.callAPI('/APP/Profile/getOwnProfile'))
```


Example without ACCESS_TOKEN
----
``` python
from PlurkAPI import PlurkAPI
from plurk_oauth import PlurkAPI

plurk = PlurkAPI(CONSUMER_KEY, CONSUMER_SECRET)
plurk.authorize()
print plurk.callAPI('/APP/Profile/getOwnProfile')
print(plurk.callAPI('/APP/Profile/getOwnProfile'))
```


Expand Down
39 changes: 22 additions & 17 deletions bin/get_own_profile.py 100644 → 100755
@@ -1,31 +1,36 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
sys.path.append('plurk_oauth/')
from PlurkAPI import PlurkAPI
from plurk_oauth import PlurkAPI
import getopt
import json


def usage():
print '''Help Information:
print('''Help Information:
-h: Show help information
'''
''')


if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
except getopt.GetoptError, err:
print str(err) # will print something like "option -a not recognized"
except getopt.GetoptError as err:
print(str(err)) # will print something like "option -a not recognized"
usage()
sys.exit(2)
file = open('API.keys', 'r+')
data = json.load(file)
plurk = PlurkAPI(data["CONSUMER_KEY"], data["CONSUMER_SECRET"])
if data.get('ACCESS_TOKEN'):
plurk.authorize(data["ACCESS_TOKEN"],data["ACCESS_TOKEN_SECRET"])
else:
plurk.authorize()
data["ACCESS_TOKEN"] = plurk._oauth.oauth_token['oauth_token']
data["ACCESS_TOKEN_SECRET"] = plurk._oauth.oauth_token['oauth_token_secret']
json.dump(data,file)

print plurk.callAPI('/APP/Profile/getOwnProfile')

with open('API.keys', 'r+') as f:
data = json.load(f)
plurk = PlurkAPI(data["CONSUMER_KEY"], data["CONSUMER_SECRET"])
if data.get('ACCESS_TOKEN'):
plurk.authorize(data["ACCESS_TOKEN"], data["ACCESS_TOKEN_SECRET"])
else:
plurk.authorize()
data["ACCESS_TOKEN"] = plurk._oauth.oauth_token['oauth_token']
data["ACCESS_TOKEN_SECRET"] = plurk._oauth.oauth_token['oauth_token_secret']
f.seek(0)
json.dump(data, f)

print(plurk.callAPI('/APP/Profile/getOwnProfile'))
45 changes: 25 additions & 20 deletions bin/post_to_plurk.py 100644 → 100755
@@ -1,39 +1,44 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
sys.path.append('plurk_oauth/')
from PlurkAPI import PlurkAPI
from plurk_oauth import PlurkAPI
import getopt
import json


def usage():
print '''Help Information:
print('''Help Information:
-h: Show help information
'''
''')


if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
except getopt.GetoptError, err:
print str(err) # will print something like "option -a not recognized"
except getopt.GetoptError as err:
print(str(err)) # will print something like "option -a not recognized"
usage()
sys.exit(2)
file = open('API.keys', 'r+')
data = json.load(file)
plurk = PlurkAPI(data["CONSUMER_KEY"], data["CONSUMER_SECRET"])
if data.get('ACCESS_TOKEN'):
plurk.authorize(data["ACCESS_TOKEN"],data["ACCESS_TOKEN_SECRET"])
else:
plurk.authorize()
data["ACCESS_TOKEN"] = plurk._oauth.oauth_token['oauth_token']
data["ACCESS_TOKEN_SECRET"] = plurk._oauth.oauth_token['oauth_token_secret']
json.dump(data,file)
with open('API.keys', 'r+') as f:
data = json.load(f)
plurk = PlurkAPI(data["CONSUMER_KEY"], data["CONSUMER_SECRET"])
if data.get('ACCESS_TOKEN'):
plurk.authorize(data["ACCESS_TOKEN"], data["ACCESS_TOKEN_SECRET"])
else:
plurk.authorize()
data["ACCESS_TOKEN"] = plurk._oauth.oauth_token['oauth_token']
data["ACCESS_TOKEN_SECRET"] = plurk._oauth.oauth_token['oauth_token_secret']
f.seek(0)
json.dump(data, f)

content = 'Test from Plurk OAuth API'
if len(sys.argv) > 1:
if len(sys.argv) > 1:
content = sys.argv[1]
qualifier = 'says'
if len(sys.argv) > 2:
if len(sys.argv) > 2:
qualifier = sys.argv[2]
print plurk.callAPI('/APP/Timeline/plurkAdd', {
print(plurk.callAPI('/APP/Timeline/plurkAdd', {
'content': content,
'qualifier': qualifier } )

'qualifier': qualifier
}))
135 changes: 0 additions & 135 deletions plurk_oauth/PlurkOAuth.py

This file was deleted.

5 changes: 5 additions & 0 deletions plurk_oauth/__init__.py
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
from .oauth import PlurkOAuth
from .api import PlurkAPI

__all__ = (PlurkAPI, PlurkOAuth)
37 changes: 16 additions & 21 deletions plurk_oauth/PlurkAPI.py → plurk_oauth/api.py
@@ -1,49 +1,51 @@
# -*- coding: utf-8 -*-
import sys
import PlurkOAuth
import json
from .oauth import PlurkOAuth


class PlurkAPI:
def __init__(self, key = None, secret = None,
access_token = None, access_secret = None):
def __init__(self, key=None, secret=None,
access_token=None, access_secret=None):
if not key or not secret:
raise ValueError, "Both CONSUMER_KEY and CONSUMER_SECRET need to be specified"
self._oauth = PlurkOAuth.PlurkOAuth(key, secret)
raise ValueError("Both CONSUMER_KEY and CONSUMER_SECRET need to be specified")
self._oauth = PlurkOAuth(key, secret)
self._authorized = False
self._error = {'code' : 200, 'reason' : '', 'content': ''}
self._error = {'code': 200, 'reason': '', 'content': ''}
self._content = ''
if access_token and access_secret:
self.authorize(access_token, access_secret)

@classmethod
def fromfile(cls, filename = "API.keys"):
try:
def fromfile(cls, filename="API.keys"):
try:
file = open(filename, 'r+')
except IOError:
print "You need to put key/secret in API.keys"
print("You need to put key/secret in API.keys")
raise
except:
print "Unexpected error:", sys.exc_info()[0]
print("Unexpected error:", sys.exc_info()[0])
else:
data = json.load(file)
file.close()
if not data["CONSUMER_KEY"] or not data["CONSUMER_SECRET"]:
return cls()
if data["ACCESS_TOKEN"] and data["ACCESS_TOKEN_SECRET"]:
return cls(data["CONSUMER_KEY"], data["CONSUMER_SECRET"],
data["ACCESS_TOKEN"], data["ACCESS_TOKEN_SECRET"])
data["ACCESS_TOKEN"], data["ACCESS_TOKEN_SECRET"])
else:
return cls(data["CONSUMER_KEY"], data["CONSUMER_SECRET"])

def is_authorized(self):
return self._authorized

def authorize(self, access_key = None, access_secret = None):
def authorize(self, access_key=None, access_secret=None):
self._oauth.authorize(access_key, access_secret)
self._authorized = True

def callAPI(self, path, options = None):
def callAPI(self, path, options=None):
self._error['code'], self._content, self._error['reason'] = self._oauth.request(
path, None, options)
path, None, options)
self._error['content'] = json.loads(self._content)
if self._error['code'] != '200':
return None
Expand Down Expand Up @@ -72,10 +74,3 @@ def get_access_token(self, verifier):
'key': self._oauth.oauth_token['oauth_token'],
'secret': self._oauth.oauth_token['oauth_token_secret'],
}


if __name__ == '__main__':
import os
plurk = PlurkAPI(os.environ["CONSUMERKEY"], os.environ["CONSUMERSECRET"])
# plurk.authorize('tqRtGMu7Btw9','SCjkkydnkWNA7gwwuZo7y9wshVlzl7Lr')
print plurk.callAPI('/APP/Profile/getOwnProfile')

0 comments on commit 8891823

Please sign in to comment.