-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A nice night of hacking on my pocket command line layer.
Things I've completed: -> Wrote an auth layer which can successfully auth with pocket, and can get an access token and username. Consumer key should be provided by a user -> the application provides a way to load/save config of consumer key and access token meaning you only have to auth once. -> I successfully completed the retrieve api call. With this much of the API done, I have experimented with a function called simple_explore which lets me look through what each list item has associated with it. TODO or things that come to mind: -> Its late, I need to put the username in the config that is passed from auth. -> There is a word count as well as a has_video attribute in the items that I have seen which would suggest a nice functionality where we auto tag stories which are above a certain word count as long. And posts which have videos as video. -> Provide a way to do nice little neat functions something like, if 'raspberry pi' in title then tag "pi" on item. That would be pretty sexy I must say.
- Loading branch information
0 parents
commit a2afdd4
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import json | ||
|
||
AUTH_URL = "https://getpocket.com/auth/authorize?request_token=%s&redirect_uri=%s" | ||
REQUEST_TOKEN_URL = "https://getpocket.com/v3/oauth/request" | ||
AUTH_TOKEN_URL = "https://getpocket.com/v3/oauth/authorize" | ||
REDIRECT_URL = "http://www.richydelaney.com/success/" | ||
CONFIG_FILE = '.creds' | ||
|
||
|
||
def auth(options): | ||
access_token = None | ||
if options.key: | ||
consumer_key = options.key | ||
else: | ||
consumer_key, access_token = read_config() | ||
if not consumer_key: | ||
print "Need to provide a consumer key using the .creds config file" | ||
print "Or by using the --key command line argument." | ||
sys.exit(1) | ||
|
||
if not access_token: | ||
access_token, username = access_token_flow(consumer_key) | ||
|
||
config = {'consumer_key':consumer_key, 'access_token': access_token} | ||
write_config(**config) | ||
return config | ||
|
||
|
||
def access_token_flow(consumer_key): | ||
code = get_authentication_token(consumer_key) | ||
redirect_user(code) | ||
return get_access_token(consumer_key, code) | ||
|
||
|
||
def get_authentication_token(consumer_key): | ||
payload = {'consumer_key': consumer_key, | ||
'redirect_uri': REDIRECT_URL} | ||
headers = {'X-Accept': 'application/json'} | ||
response = requests.post(REQUEST_TOKEN_URL, data=payload, headers=headers) | ||
if response.status_code != 200: | ||
print "Returned Status Code %d: %s" % (response.status_code, | ||
response.content) | ||
sys.exit(1) | ||
body = response.json() | ||
return body["code"] | ||
|
||
|
||
def redirect_user(code): | ||
print "please go the following website and authenticate:" | ||
print AUTH_URL % (code, REDIRECT_URL) | ||
raw_input("When you have completed, press enter:") | ||
|
||
|
||
def get_access_token(consumer_key, code): | ||
payload = {'consumer_key': consumer_key, | ||
'code': code} | ||
headers = {'X-Accept': 'application/json'} | ||
response = requests.post(AUTH_TOKEN_URL, data=payload, headers=headers) | ||
body = response.json() | ||
return body["access_token"], body["username"] | ||
|
||
|
||
def read_config(): | ||
with open(CONFIG_FILE, "rb") as infile: | ||
config = json.load(infile) | ||
return (config.get('consumer_key', None), | ||
config.get('access_token', None)) | ||
|
||
|
||
def write_config(**kwargs): | ||
with open(CONFIG_FILE, "wb") as outfile: | ||
json.dump(kwargs, outfile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import json | ||
import sys | ||
|
||
from optparse import OptionParser | ||
|
||
import requests | ||
|
||
from auth import auth | ||
|
||
RETRIEVE_URL = "https://getpocket.com/v3/get" | ||
|
||
|
||
def retrieve(config): | ||
response = requests.get(RETRIEVE_URL, params=config) | ||
items = response.json()['list'] | ||
return items | ||
|
||
def simple_explore(items): | ||
for key, item in items.iteritems(): | ||
explore(item) | ||
|
||
|
||
def explore(item): | ||
linefeed = raw_input("Enter n to continue to next item:") | ||
while linefeed != 'n': | ||
if linefeed == 'keys': | ||
print item.keys() | ||
else: | ||
if linefeed in item: | ||
print item[linefeed] | ||
linefeed = raw_input("Enter n to continue to next item:") | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = OptionParser() | ||
parser.add_option('--key', dest='key', | ||
help='pocket apps consumer key') | ||
parser.add_option('--access-token', '-t', dest='token', | ||
help='pocket apps consumer key') | ||
(options, args) = parser.parse_args() | ||
|
||
config = auth(options) | ||
items = retrieve(config) | ||
simple_explore(items) |