Skip to content

Instantly share code, notes, and snippets.

@cshoe
Last active December 10, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cshoe/4513551 to your computer and use it in GitHub Desktop.
Save cshoe/4513551 to your computer and use it in GitHub Desktop.
Delete Tag Test
import json
import oauth2
# FILL THESE OUT
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
# has to be an access token, not a request token
TOKEN = ''
TOKEN_SECRET = ''
READABILITY_URL_TEMPLATE = 'https://readability.com/api/rest/v1/{0}'
def main():
"""
Get Tags for a Bookmark.
Delete a single Tag for a Bookmark.
Check that deleted Tag is no longer associated with Bookmark.
"""
token = oauth2.Token(TOKEN, TOKEN_SECRET)
consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
client = oauth2.Client(consumer, token)
bookmarks_url = _get_url('bookmarks')
r, content = client.request(bookmarks_url, method='GET')
bookmarks = json.loads(content)
# first bookmark must have tags
bookmark_id = bookmarks['bookmarks'][0]['id']
# Get tags on bookmark
bookmarks_tags_url = _get_url('bookmarks/{0}/tags'.format(bookmark_id))
r, content = client.request(bookmarks_tags_url, method='GET')
tags = json.loads(content)
if len(tags['tags']) == 0:
print 'Bookmark {0} has no tags'.format(bookmark_id)
return
print 'bookmark has {0} tags'.format(len(tags['tags']))
print 'tags are: {0}'.format([tag['text'] for tag in tags['tags']])
tag_id = tags['tags'][0]['id']
tag_text = tags['tags'][0]['text']
print 'doing delete on tag id {0}, {1}'.format(tag_id, tag_text)
tag_url = \
_get_url('bookmarks/{0}/tags/{1}'.format(bookmark_id, tag_id))
r, content = client.request(tag_url, method='DELETE')
# Get tags on bookmark again
bookmarks_tags_url = _get_url('bookmarks/{0}/tags'.format(bookmark_id))
r, content = client.request(bookmarks_tags_url, method='GET')
tags = json.loads(content)
print 'bookmark has {0} tags'.format(len(tags['tags']))
print 'tags are: {0}'.format([tag['text'] for tag in tags['tags']])
def _get_url(call):
"""Get a Readability url using READABILITY_URL_TEMPLATE.
"""
return READABILITY_URL_TEMPLATE.format(call)
if __name__ == '__main__':
main()
@cshoe
Copy link
Author

cshoe commented Jan 11, 2013

Received the following output when ran on a user account who's first Bookmark had 4 tags:

# python delete_test.py
bookmark has 4 tags
tags are: [u'bar', u'foo', u'hello', u'this is a tag']
doing delete on tag id 45673, bar
bookmark has 3 tags
tags are: [u'foo', u'hello', u'this is a tag']
(tagtest)[ shoe@shoetop ~/Desktop ]
# python delete_test.py
bookmark has 3 tags
tags are: [u'foo', u'hello', u'this is a tag']
doing delete on tag id 45672, foo
bookmark has 2 tags
tags are: [u'hello', u'this is a tag']
(tagtest)[ shoe@shoetop ~/Desktop ]
# python delete_test.py
bookmark has 2 tags
tags are: [u'hello', u'this is a tag']
doing delete on tag id 45674, hello
bookmark has 1 tags
tags are: [u'this is a tag']
(tagtest)[ shoe@shoetop ~/Desktop ]
# python delete_test.py
bookmark has 1 tags
tags are: [u'this is a tag']
doing delete on tag id 45671, this is a tag
bookmark has 0 tags
tags are: []
(tagtest)[ shoe@shoetop ~/Desktop ]
# python delete_test.py
Bookmark 28816682 has no tags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment