Skip to content

Commit

Permalink
Finally got around to adding the modify function.
Browse files Browse the repository at this point in the history
Added functions for adding tags.
Another function for isolating long articles and tagging them as such.
Should be able to be run as a script.
  • Loading branch information
Newky committed Dec 25, 2012
1 parent 43a60aa commit 1ff1b36
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 32 deletions.
50 changes: 50 additions & 0 deletions operators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import copy

from optparse import OptionParser
from auth import auth
from pocket import retrieve, add_tags


def has_long_tag(item):
return has_tag(item, 'long')


def has_tag(item, tag):
if "tags" not in item:
return False
else:
if tag not in item["tags"].keys():
return False
else:
return True

def isolate_long_articles(items, long_word_count=5000):
uids = []

for uid, item in items.iteritems():
# get a word count from each item
word_count = item.get('word_count', None)
if not word_count:
continue
word_count = long(word_count)
# if the word count is above the long word count
if word_count > long_word_count:
if not has_long_tag(item):
uids.append(uid)

return uids


if __name__ == "__main__":
parser = OptionParser()
parser.add_option('--key', dest='key',
help='pocket apps consumer key')
(options, args) = parser.parse_args()

config = auth(options)
credentials = copy.deepcopy(config)
items = retrieve(config, verbose=True)
uids = isolate_long_articles(items)

if len(uids) > 0:
add_tags(credentials, uids, ["long"])
57 changes: 25 additions & 32 deletions pocket.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,41 @@
import json
import sys

from optparse import OptionParser

import requests

from auth import auth

RETRIEVE_URL = "https://getpocket.com/v3/get"
SEND_URL = "https://getpocket.com/v3/send"


def retrieve(config):
def retrieve(config, verbose=False):
if verbose:
config["detailType"] = 'complete'
response = requests.get(RETRIEVE_URL, params=config)
items = response.json()['list']
return items


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:")


def print_item(item, columns=['resolved_title', 'given_url'], delimiter='\t'):
values = [ item[k] for k in columns ]
print delimiter.join(values)

def modify(config):
if 'actions' not in config:
raise Exception('Actions are not in the request body')
headers = {'content-type': 'application/json',
'X-Accept': 'application/json'}
payload = json.dumps(config)
response = requests.post(SEND_URL, headers=headers, data=payload)
if response.status_code != 200:
print "Returned Status Code %d: %s" % (response.status_code,
response.content)
sys.exit(1)
return response

def simple_explore(items, func=explore):
for key, item in items.iteritems():
func(item)

def add_tags(config, item_ids, tags):
actions = []

if __name__ == "__main__":
parser = OptionParser()
parser.add_option('--key', dest='key',
help='pocket apps consumer key')
(options, args) = parser.parse_args()
for item_id in item_ids:
action = {"action": "tags_add", "item_id": item_id, "tags": tags}
actions.append(action)

config = auth(options)
items = retrieve(config)
simple_explore(items, func=print_item)
config["actions"] = actions
response = modify(config)
body = response.json()
assert(body["status"] == 1)

0 comments on commit 1ff1b36

Please sign in to comment.