Example #1
0
    def handle(self, *args, **options):

        # couchdbkit doesn't preserve microseconds
        start_time = datetime.utcnow().replace(microsecond=0)

        excluded_tags = settings.DIRECTORY_EXCLUDED_TAGS

        tags = args or Tag.all()

        for n, tag in enumerate(tags):

            if not isinstance(tag, basestring):
                tag = str(tag)

            label = utils.remove_control_chars(tag.strip())
            if not label:
                continue

            tag_obj = Tag(tag)
            podcast_ids, weights = utils.unzip(list(tag_obj.get_podcasts()))
            podcast_objs = Podcast.get_multi(podcast_ids)
            podcasts = []
            for podcast, weight in zip(podcast_objs, weights):
                e = CategoryEntry()
                e.podcast = podcast.get_id()
                e.weight = float(weight * podcast.subscriber_count())
                podcasts.append(e)

            category = Category.for_tag(label)

            if not category:
                if not label or label in excluded_tags:
                    continue

                category = Category()
                category.label = label
                category.spellings = []

            # delete if it has been excluded after it has been created
            if label in excluded_tags:
                category.delete()
                continue

            # we overwrite previous data
            if category.updated != start_time:
                category.podcasts = []

            category.merge_podcasts(podcasts)

            category.updated = start_time

            if 'weight' in category:
                del category['weight']

            category.save()

            try:
                utils.progress(n % 1000, 1000, category.label.encode('utf-8'))
            except:
                pass
Example #2
0
def create_subscriptionlist(request):

    user = request.user
    user.sync_all()

    subscriptions = user.get_subscriptions()

    if not subscriptions:
        return []

    # Load all Podcasts and Devices first to ensure that they are
    # only loaded once, not for each occurance in a subscription
    public, podcast_ids, device_ids = unzip(subscriptions)
    podcast_ids= list(set(podcast_ids))
    device_ids = list(set(device_ids))

    podcasts = get_to_dict(Podcast, podcast_ids, get_id=Podcast.get_id)
    devices = dict([ (id, user.get_device(id)) for id in device_ids])

    subscription_list = {}
    for public, podcast_id, device_id in subscriptions:
        device = devices[device_id]
        if not podcast_id in subscription_list:
            podcast = podcasts.get(podcast_id, None)
            if podcast is None:
                continue

            episode = get_cache_or_calc('%s-latest-episode' % podcast.get_id(),
                    timeout=60*60, calc=lambda: podcast.get_latest_episode())

            subscription_list[podcast_id] = {
                'podcast': podcasts[podcast_id],
                'devices': [device] if device else [],
                'episode': episode
            }
        else:
            if device:
                subscription_list[podcast_id]['devices'].append(device)

    return subscription_list.values()
Example #3
0
def create_subscriptionlist(request):
    user = request.user
    subscriptions = subscriptions_by_user(user)

    if not subscriptions:
        return []

    # Load all Podcasts and Devices first to ensure that they are
    # only loaded once, not for each occurance in a subscription
    public, podcast_ids, device_ids = unzip(subscriptions)
    podcast_ids= list(set(podcast_ids))
    device_ids = list(set(device_ids))

    podcasts = podcasts_to_dict(podcast_ids)
    devices = user.get_devices_by_id(device_ids)

    subscription_list = {}
    for public, podcast_id, device_id in subscriptions:
        device = devices.get(device_id)
        if not podcast_id in subscription_list:
            podcast = podcasts.get(podcast_id, None)
            if podcast is None:
                continue

            subscription_list[podcast_id] = {
                'podcast': podcasts[podcast_id],
                'devices': [device] if device else [],
                'episodes': podcast.episode_count,
            }
        else:
            if device:
                subscription_list[podcast_id]['devices'].append(device)

    subscriptions = subscription_list.values()
    sort_key = lambda s: s['podcast'].latest_episode_timestamp or datetime.utcnow()
    subscriptions = sorted(subscriptions, key=sort_key, reverse=True)
    return subscriptions