mekk.feeds / src / mekk / feeds / greader2org / initial_load.py

# -*- coding: utf-8 -*-

from base_command import BaseCommand
from collections import defaultdict
from helpers import true_categories
from mekk.feeds.orgfile import Folder, Feed

import logging

log = logging.getLogger("greader2org")

class InitialLoad(BaseCommand):
    """
    Initial import from Google Reader (creating first version
    of text mirror file).
    """

    def execute(self):

        if self.org.exists():
            raise Exception("File %s already exists.\nTo update it, use 'get' option instead of 'init'" % self.org.file_name)
        
        subs = self.reader.get_subscription_list()
        
        # tag (for untagged 'ZZZ: Unlabeled') ---> list of matching elements
        subscribed = defaultdict(lambda : [])
        
        for item in subs['subscriptions']:
            feed = item['id']
            if feed.startswith("feed/"):
                feed = feed[5:]
            elif feed.startswith("user/"):
                continue   # starred, broadcast itp
            else:
                raise Exception("Ugly feed: %s" % feed)
        
            title = item['title']
        
            label = "ZZZ: Unlabeled"
            cat = true_categories( item['categories'] )
            if len(cat) > 0:
                label = cat[0]['label']
                if len(cat) > 1:
                    log.warn("Multiple folders for %s. Using first one of: %s" % (
                        item['id'], ", ".join((x['label'] for x in cat))))
        
            subscribed[label].append( dict(feed = feed, title = title) )

        for label in sorted(subscribed.keys()):
            folder = Folder(label)
            self.org.add_folder(folder)

            print "Creating folder %s" % label

            for info in subscribed[label]:
                feed = Feed(title=info['title'],
                            tags=[], 
                            feed=info['feed'],
                            level=None, 
                            comment="")
                folder.add_feed(feed)
                print "   adding feed %s" % info['title']
        
        self.org.save_to()
                
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.