mekk.feeds / src / mekk / feeds / greader2org /

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

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

log = logging.getLogger("greader2org")


class UpdateFromGoogle(BaseCommand):
    Uzupełnia plik na bazie zawartości w Google

    def execute(self):
        if not
            raise Exception("%s does not exist. Use\n  greader2org init\nto create initial version" %

        subs = self.reader.get_subscription_list()

        # tag (dla nieotagowanych 'ZZZ: Unlabeled') ---> lista elementów
        subscribed = defaultdict(lambda : [])
        all_google_feeds = set()

        re_postrank = re.compile(
        for item in subs['subscriptions']:
            feed_url = item['id']
            if feed_url.startswith("feed/"):
                feed_url = feed_url[5:]
            elif feed_url.startswith("user/"):
                continue   # starred, broadcast itp
                raise Exception("Ugly feed: %s" % feed_url)
            # In case some postrank feed is still present on Google,
            # we replace it with normal URL
            m = re_postrank.match(feed_url)
            if m:
                feed_url =

            title = item['title']
            folder = "ZZZ: Unlabeled"
            cat = true_categories( item['categories'] )
            if len(cat) > 0:
                folder = cat[0]['label']
                if len(cat) > 1:
                    if WARN_MULTIPLE_CATEGORIES:
                        log.warn("Multiple categories for %s. Using first one of: %s" % (
                            item['id'], ", ".join((x['label'] for x in cat))))
            if not
                print "New feed %s in category %s" % (title, folder)
                lab =
                    Feed(title, [], feed_url, None, None, None) )
        # Marking unsubscribed feeds as disabled, and other cleanups
        for folder in
            for org_feed in folder.feeds:
                if org_feed.feed not in all_google_feeds:
                    if 'disabled' not in org_feed.tags:
                        print "Marking unsubscribed feed %s as disabled" % org_feed.title
                # Drop postrank_feed attribute if still present
                if org_feed.postrank_feed:
                    if org_feed.feed:
                        print "Dropping legacy postrank_feed info for %s" % org_feed.title
                        org_feed.postrank_feed = None
                        print "Feed %s has only postrank url specified, so is usess. Consider removing it"