Pages

2012-09-02

Python: Filtering Delicious bookmarks by tags

bmconv.py is required.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# bmfilter.py - Filter bookmarks by tags.
import bmconv

def tagfilter(tags_list):
    """Return a list of bookmark dictionaries."""
    return filter(lambda x: set(tags_list) <= set(x['tags']), bmconv.main())

if __name__ == '__main__':
    print(tagfilter(['book', 'history']))
The above example outputs only bookmarks that have both 'book' and 'history' tags.

Update, September 2, 8:41 p.m. JST: AND/OR filters.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# bmfilter.py - Filter bookmarks by tags.
import bmconv

def andfilter(tags_list):
    """Return a list of bookmarks that have all the tags in tags_list."""
    return filter(lambda x: set(tags_list) <= set(x['tags']), bmconv.main())

def orfilter(tags_list):
    """Return a list of bookmarks that have any of the tags in tags_list."""
    return filter(lambda x: len(set(tags_list).intersection(set(x['tags']))) > 0, bmconv.main())

if __name__ == '__main__':
#    print(andfilter(['book', 'history']))
    print(orfilter(['cd', 'dvd']))

Update, September 3, 6:59 p.m. JST: Changed function name and arguments. Added case sensitivity switch.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# bmfilter.py - Filter bookmarks by tags.
import bmconv

def tagfilter(tags_list, bookmarks_list, filter_type_and=True, ignore_case=True):
    """Return a list of dictionaries of bookmarks filtered from bookmarks_list by tags in tags_list."""
    
    def proc_case(str_list):
        if ignore_case:
            return map(lambda x: x.upper(), str_list)
        else: # case sensitive
            return str_list

    if filter_type_and: # AND
        return filter(lambda x: set(proc_case(tags_list)) <= set(proc_case(x['tags'])), bookmarks_list)
    else: # OR
        return filter(lambda x: len(set(proc_case(tags_list)).intersection(set(proc_case(x['tags'])))) > 0, bookmarks_list)

if __name__ == '__main__':
    print(tagfilter(['book', 'history'], bmconv.main()))
##      bookmarks that have both tags 'book' and 'history'
##      (case insensitive. also matches 'Book', 'History', etc.)
    print(tagfilter(['cd', 'dvd'], bmconv.main(), False))
##      bookmarks that have any of tags 'cd' and 'dvd'
##      (case insensitive. also matches 'CD', 'Cd', 'DVD', 'Dvd', etc.)
    print(tagfilter(['Apple'], bmconv.main(), True, False))
##      doesn't match 'apple'.

No comments: