Skip to content

Commit

Permalink
Added a new file called comments_apply.
Browse files Browse the repository at this point in the history
This is a new function which takes any item tagged with "comments"
and automagically adds the hn comment url to the list.

Also seperated any tag logic out into seperate tags.py file.

Added dependency on HNComments into requirements
  • Loading branch information
Newky committed Jan 8, 2013
1 parent 6f0589d commit 8faae7c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 25 deletions.
44 changes: 44 additions & 0 deletions comments_apply.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import copy

from optparse import OptionParser
from auth import auth
from hncomments import utils

from pocket import retrieve, add
from tags import has_tag, remove_tags

def find_items_with_comments_tag(items):
uids = []

for uid, item in items.iteritems():
if has_tag(item, "comments"):
uids.append((uid, item.get('given_url', '')))

return uids


def find_and_add_hn_urls(credentials, comment_items):
config = copy.deepcopy(credentials)

for uid, url in comment_items:
hn_url = utils.get_hn_comments_url(url)
if hn_url:
config["url"] = hn_url
add(config)
uids = [uid for uid, url in comment_items]
remove_tags(credentials, uids, ["comments"])


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)
# retrieve items
items = retrieve(config, verbose=True)

comment_items = find_items_with_comments_tag(items)
comment_urls = find_and_add_hn_urls(credentials, comment_items)
13 changes: 2 additions & 11 deletions operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@
from optparse import OptionParser
from auth import auth
from jsonconfig import JsonConfig
from pocket import retrieve, add_tags
from pocket import retrieve
from tags import add_tags, has_tag


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 = []

Expand Down
27 changes: 13 additions & 14 deletions pocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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


def retrieve(config, verbose=False):
Expand All @@ -27,17 +28,15 @@ def modify(config):
sys.exit(1)
return response


def add_tags(config, item_ids, tags):
if len(item_ids) <= 0:
return
actions = []

for item_id in item_ids:
action = {"action": "tags_add", "item_id": item_id, "tags": tags}
actions.append(action)

config["actions"] = actions
response = modify(config)
body = response.json()
assert(body["status"] == 1)
def add(config):
if 'url' not in config:
raise Exception('"url" is not in the request body')
headers = {'content-type': 'application/json',
'X-Accept': 'application/json'}
payload = json.dumps(config)
response = requests.post(ADD_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
1 change: 1 addition & 0 deletions requirements
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
HNComments==0.1.1
argparse==1.2.1
requests==1.0.4
wsgiref==0.1.2
34 changes: 34 additions & 0 deletions tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from pocket import modify


def tag_action(config, item_ids, tags, action):
if len(item_ids) <= 0:
return
actions = []

for item_id in item_ids:
action = {"action": action, "item_id": item_id, "tags": tags}
actions.append(action)

config["actions"] = actions
response = modify(config)
body = response.json()
assert(body["status"] == 1)


def add_tags(config, item_ids, tags):
tag_action(config, item_ids, tags, "tags_add")


def remove_tags(config, item_ids, tags):
tag_action(config, item_ids, tags, "tags_remove")


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

0 comments on commit 8faae7c

Please sign in to comment.