-
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.
Added a new file called comments_apply.
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
Showing
5 changed files
with
94 additions
and
25 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,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) |
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
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
HNComments==0.1.1 | ||
argparse==1.2.1 | ||
requests==1.0.4 | ||
wsgiref==0.1.2 |
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,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 |