jacobian / jellyroll

You keep personal data in all sorts of places on the internets. Jellyroll brings them together onto your own site.

This URL has Read+Write access

jellyroll / jellyroll / providers / magnolia.py
100644 98 lines (85 sloc) 3.2 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import logging
import urllib
from django.conf import settings
from django.utils.encoding import smart_unicode
from jellyroll.models import Item, Bookmark
from jellyroll.providers import utils
 
#
# Magnolia client, contributed by Rob Hudson
#
class MagnoliaClient(object):
    """
A super-minimal Magnolia client :)
"""
    def __init__(self, api_key, method=None):
        self.api_key = api_key
        self.method = method
        
    def __getattr__(self, method):
        return MagnoliaClient(self.api_key, method)
        
    def __repr__(self):
        return "<MagnoliaClient: %s>" % self.method
        
    def __call__(self, **params):
        params['api_key'] = self.api_key
        url = ("http://ma.gnolia.com/api/rest/1/%s/?" % (self.method)) + urllib.urlencode(params)
        return utils.getxml(url)
 
#
# Public API
#
 
log = logging.getLogger("jellyroll.providers.magnolia")
 
def enabled():
    ok = hasattr(settings, 'MAGNOLIA_API_KEY') and hasattr(settings, 'MAGNOLIA_USERNAME')
    if not ok:
        log.warn('The Magnolia provider is not available because the '
                 'MAGNOLIA_API_KEY and/or MAGNOLIA_USERNAME settings are '
                 'undefined.')
    return ok
    
def update():
    magnolia = MagnoliaClient(settings.MAGNOLIA_API_KEY)
 
    # Check to see if we need an update
    last_update_date = Item.objects.get_last_update_of_model(Bookmark)
    params = {'person': settings.MAGNOLIA_USERNAME}
    if last_update_date:
        params['from'] = last_update_date.isoformat()
    xml = magnolia.bookmarks_find(**params)
    _update_bookmarks(xml)
                
#
# Private API
#
 
def _update_bookmarks(xml):
    for bookmark in xml.getiterator('bookmark'):
        # Get attribute belonging to the bookmark element
        info = dict((k, smart_unicode(bookmark.get(k))) for k in bookmark.keys())
        # Handle child tags under the bookmark element
        for e in bookmark.getchildren():
            if e.tag == 'tags':
                info['tags'] = ' '.join([t.get('name') for t in e.getchildren()])
            else:
                info[e.tag] = e.text
        _handle_bookmark(info)
        
def _handle_bookmark(info):
    """
info.keys() ==> ['id', 'created', 'updated', 'rating', 'private', 'owner',
'title', 'url', 'description', 'screenshot', 'tags']
Current Bookmark model doesn't support all of these.
"""
    if info['private'] == 'false':
        b, created = Bookmark.objects.get_or_create(
            url = info['url'],
            defaults = dict(
                description = info['title'],
                extended = info.get('description', ''),
                thumbnail_url = info.get('screenshot', ''),
            )
        )
        if not created:
            b.description = info['title']
            b.extended = info.get('description', '')
            b.thumbnail_url = info.get('screenshot', ''),
            b.save()
        return Item.objects.create_or_update(
            instance = b,
            timestamp = utils.parsedate(info['created']),
            tags = info.get('tags', ''),
            source = __name__,
            source_id = info['id'],
        )