Beispiel #1
0
def episode_count():
    db = get_main_database()
    r = get_single_result(db, 'episodes/by_podcast',
            reduce = True,
            stale  = 'update_after',
        )
    return r['value'] if r else 0
Beispiel #2
0
def episode_count_for_podcast(podcast, since=None, until={}, **kwargs):

    if not podcast:
        raise QueryParameterMissing('podcast')


    if kwargs.get('descending', False):
        since, until = until, since

    if isinstance(since, datetime):
        since = since.isoformat()

    if isinstance(until, datetime):
        until = until.isoformat()

    db = get_main_database()
    res = get_single_result(db, 'episodes/by_podcast',
            startkey     = [podcast.get_id(), since],
            endkey       = [podcast.get_id(), until],
            reduce       = True,
            group_level  = 1,
            **kwargs
        )

    return res['value'] if res else 0
Beispiel #3
0
def podcast_state_for_user_podcast(user, podcast):

    if not user:
        raise QueryParameterMissing('user')

    if not podcast:
        raise QueryParameterMissing('podcast')

    udb = get_userdata_database()

    p = get_single_result(udb, 'podcast_states/by_podcast',
                key          = [podcast.get_id(), user._id],
                limit        = 1,
                include_docs = True,
                schema       = PodcastUserState,
            )

    if not p:
        p = PodcastUserState()
        p.podcast = podcast.get_id()
        p.user = user._id
        p.ref_url = podcast.url
        p.settings[PUBLIC_SUB_PODCAST.name]=user.get_wksetting(PUBLIC_SUB_USER)

        p.set_device_state(user.devices)

    return p
Beispiel #4
0
def get_nth_episode_state(n):
    udb = get_userdata_database()
    state = get_single_result(udb, 'episode_states/by_user_episode',
            skip         = n,
            include_docs = True,
            limit        = 1,
            schema       = EpisodeUserState,
        )

    return state
Beispiel #5
0
def get_license_podcast_count(license_url=None):
    """ returns the number of podcasts that contain license information """

    kwargs = {}
    if license_url:
        kwargs['key'] = license_url

    db = get_main_database()
    r = get_single_result(db, 'podcasts/license', **kwargs)

    return r['value'] if r else 0
Beispiel #6
0
def subscription_for_topic(topic):
    """ return the subscription for the given topic, one None """

    db = get_pubsub_database()
    sub = get_single_result(db, 'subscriptions/by_topic',
            key          = topic,
            include_docs = True,
            reduce       = False,
            schema       = Subscription
        )

    return sub
Beispiel #7
0
def category_for_tag_uncached(tag):

    if not tag:
        raise QueryParameterMissing('tag')

    db = get_categories_database()
    cat = get_single_result(db, 'categories/by_tags',
            key          = tag,
            include_docs = True,
            stale        = 'update_after',
            schema       = Category
        )

    return cat
Beispiel #8
0
def episode_listener_count(episode, start=None, end={}):
    """ returns the number of users that have listened to this episode """

    if not episode:
        raise QueryParameterMissing('episode')

    udb = get_userdata_database()
    r = get_single_result(udb, 'listeners/by_episode',
            startkey    = [episode._id, start],
            endkey      = [episode._id, end],
            group       = True,
            group_level = 2,
            reduce      = True,
            stale       = 'update_after',
        )
    return r['value'] if r else 0
Beispiel #9
0
def podcastlist_for_user_slug(user_id, slug):

    if not user_id:
        raise QueryParameterMissing('user_id')

    if not slug:
        raise QueryParameterMissing('slug')

    db = get_main_database()
    l = get_single_result(db, 'podcastlists/by_user_slug',
            key          = [user_id, slug],
            include_docs = True,
            schema       = PodcastList,
        )

    return l
Beispiel #10
0
def podcast_listener_count(episode):
    """ returns the number of users that have listened to this podcast """

    if not episode:
        raise QueryParameterMissing('episode')

    udb = get_userdata_database()
    r = get_single_result(udb, 'listeners/by_podcast',
            startkey    = [episode.get_id(), None],
            endkey      = [episode.get_id(), {}],
            group       = True,
            group_level = 1,
            reduce      = True,
            stale       = 'update_after',
        )
    return r['value'] if r else 0
Beispiel #11
0
def subscriberdata_for_podcast(podcast_id):

    if not podcast_id:
        raise QueryParameterMissing('podcast_id')

    db = get_main_database()
    data = get_single_result(db, 'podcasts/subscriber_data',
            key          = podcast_id,
            include_docs = True,
            schema       = PodcastSubscriberData,
        )

    if not data:
        data = PodcastSubscriberData()
        data.podcast = podcast_id

    return data
Beispiel #12
0
def suggestions_for_user(user):

    if not user:
        raise QueryParameterMissing('user')

    from mygpo.users.models import Suggestions
    db = get_main_database()
    s = get_single_result(db, 'suggestions/by_user',
                key          = user._id,
                include_docs = True,
                schema       = Suggestions,
            )

    if not s:
        s = Suggestions()
        s.user = user._id

    return s
Beispiel #13
0
def podcastgroup_for_oldid(oldid):

    if not oldid:
        raise QueryParameterMissing('oldid')

    db = get_main_database()
    pg = get_single_result(db, 'podcasts/groups_by_oldid',
            key          = long(oldid),
            include_docs = True,
            schema       = PodcastGroup,
        )

    if not pg:
        return None

    if pg.needs_update:
        incomplete_obj.send_robust(sender=pg)

    return pg
Beispiel #14
0
def podcast_for_oldid(oldid):

    if oldid is None:
        raise QueryParameterMissing('oldid')

    db = get_main_database()
    podcast = get_single_result(db, 'podcasts/by_oldid',
            key          = long(oldid),
            include_docs = True,
            wrapper      = _wrap_podcast_group_key1,
        )

    if not podcast:
        return None

    if podcast.needs_update:
        incomplete_obj.send_robust(sender=podcast)

    return podcast
Beispiel #15
0
def episode_for_podcast_id_url(podcast_id, episode_url, create=False):

    if not podcast_id:
        raise QueryParameterMissing('podcast_id')

    if not episode_url:
        raise QueryParameterMissing('episode_url')


    key = u'episode-podcastid-%s-url-%s' % (
            sha1(podcast_id.encode('utf-8')).hexdigest(),
            sha1(episode_url.encode('utf-8')).hexdigest())

#   Disabled as cache invalidation is not working properly
#   episode = cache.get(key)
#   if episode:
#       return episode

    db = get_main_database()
    episode = get_single_result(db, 'episodes/by_podcast_url',
            key          = [podcast_id, episode_url],
            include_docs = True,
            reduce       = False,
            schema       = Episode,
        )

    if episode:
        if episode.needs_update:
            incomplete_obj.send_robust(sender=episode)
        else:
            cache.set(key, episode)
        return episode

    if create:
        episode = Episode()
        episode.created_timestamp = get_timestamp(datetime.utcnow())
        episode.podcast = podcast_id
        episode.urls = [episode_url]
        episode.save()
        incomplete_obj.send_robust(sender=episode)
        return episode

    return None
Beispiel #16
0
def podcast_by_id_uncached(podcast_id, current_id=False):

    if not podcast_id:
        raise QueryParameterMissing('podcast_id')

    db = get_main_database()
    podcast = get_single_result(db, 'podcasts/by_id',
            key          = podcast_id,
            include_docs = True,
            wrapper      = _wrap_podcast_group,
        )

    if not podcast:
        return None

    if podcast.needs_update:
        incomplete_obj.send_robust(sender=podcast)

    return podcast
Beispiel #17
0
def podcast_for_slug(slug):

    if not slug:
        raise QueryParameterMissing('slug')

    db = get_main_database()
    obj = get_single_result(db, 'podcasts/by_slug',
            startkey     = [slug, None],
            endkey       = [slug, {}],
            include_docs = True,
            wrapper      = _wrap_podcast_group_key1,
        )

    if not obj:
        return None

    if obj.needs_update:
        incomplete_obj.send_robust(sender=obj)

    return obj
Beispiel #18
0
def podcast_subscriber_count(podcast):

    if not podcast:
        raise QueryParameterMissing('podcast')

    udb = get_userdata_database()
    subscriber_sum = 0

    for podcast_id in podcast.get_ids():
        x = get_single_result(udb, 'subscribers/by_podcast',
                startkey    = [podcast_id, None],
                endkey      = [podcast_id, {}],
                reduce      = True,
                group       = True,
                group_level = 1,
            )

        subscriber_sum += x['value'] if x else 0

    return subscriber_sum
Beispiel #19
0
def episode_state_for_user_episode(user, episode):

    if not user:
        raise QueryParameterMissing('user')

    if not episode:
        raise QueryParameterMissing('episode')


    key = 'episode-state-userid-%s-episodeid-%s' % (sha1(user._id).hexdigest(),
            sha1(episode._id).hexdigest())

#   Disabled as cache invalidation does not work properly
#   state = cache.get(key)
#   if state:
#       return state

    udb = get_userdata_database()
    state = get_single_result(udb, 'episode_states/by_user_episode',
            key          = [user._id, episode._id],
            include_docs = True,
            limit        = 1,
            schema       = EpisodeUserState,
        )

    if state:
        cache.set(key, state)
        return state

    else:
        podcast = podcast_by_id(episode.podcast)

        state = EpisodeUserState()
        state.episode = episode._id
        state.podcast = episode.podcast
        state.user = user._id
        state.ref_url = episode.url
        state.podcast_ref_url = podcast.url
        # don't cache here, because the state is saved by the calling function

        return state
Beispiel #20
0
def episode_for_oldid(oldid):

    if not oldid:
        raise QueryParameterMissing('oldid')

    oldid = int(oldid)
    db = get_main_database()
    episode = get_single_result(db, 'episodes/by_oldid',
            key          = oldid,
            limit        = 1,
            include_docs = True,
            schema       = Episode,
        )

    if not episode:
        return None

    if episode.needs_update:
        incomplete_obj.send_robust(sender=episode)

    return episode
Beispiel #21
0
def get_num_played_episodes(user, since=None, until={}):
    """ Number of played episodes in interval """

    if not user:
        raise QueryParameterMissing('user')

    since_str = since.strftime('%Y-%m-%d') if since else None
    until_str = until.strftime('%Y-%m-%d') if until else {}

    startkey = [user._id, since_str]
    endkey   = [user._id, until_str]

    udb = get_userdata_database()
    val = get_single_result(udb, 'listeners/by_user',
            startkey = startkey,
            endkey   = endkey,
            reduce   = True,
            stale    = 'update_after',
        )

    return val['value'] if val else 0
Beispiel #22
0
def episode_for_slug(podcast_id, episode_slug):

    if not podcast_id:
        raise QueryParameterMissing('podcast_id')

    if not episode_slug:
        raise QueryParameterMissing('episode_slug')

    db = get_main_database()
    episode = get_single_result(db, 'episodes/by_slug',
            key          = [podcast_id, episode_slug],
            include_docs = True,
            schema       = Episode,
        )

    if not episode:
        return None

    if episode.needs_update:
        incomplete_obj.send_robust(sender=episode)

    return episode
Beispiel #23
0
def episode_state_for_ref_urls(user, podcast_url, episode_url):

    if not user:
        raise QueryParameterMissing('user')

    if not podcast_url:
        raise QueryParameterMissing('podcast_url')

    if not episode_url:
        raise QueryParameterMissing('episode_url')


    cache_key = 'episode-state-%s-%s-%s' % (user._id,
            sha1(podcast_url).hexdigest(),
            sha1(episode_url).hexdigest())

    state = cache.get(cache_key)
    if state:
        return state

    udb = get_userdata_database()
    state = get_single_result(udb, 'episode_states/by_ref_urls',
            key   = [user._id, podcast_url, episode_url],
            limit = 1,
            include_docs=True,
            schema      = EpisodeUserState,
        )

    if state:
        state.ref_url = episode_url
        state.podcast_ref_url = podcast_url
        cache.set(cache_key, state, 60*60)
        return state

    else:
        podcast = podcast_for_url(podcast_url, create=True)
        episode = episode_for_podcast_id_url(podcast.get_id(), episode_url,
            create=True)
        return episode_state_for_user_episode(user, episode)
Beispiel #24
0
def get_seconds_played(user, since=None, until={}):
    """ Returns the number of seconds that the user has listened

    Can be selected by timespan, podcast and episode """

    if not user:
        raise QueryParameterMissing('user')

    since_str = since.strftime('%Y-%m-%dT%H:%M:%S') if since else None
    until_str = until.strftime('%Y-%m-%dT%H:%M:%S') if until else {}

    startkey = [user._id, since_str]
    endkey   = [user._id, until_str]

    udb = get_userdata_database()
    val = get_single_result(udb, 'listeners/times_played_by_user',
            startkey = startkey,
            endkey   = endkey,
            reduce   = True,
            stale    = 'update_after',
        )

    return val['value'] if val else 0
Beispiel #25
0
def episode_by_id(episode_id, current_id=False):

    if not episode_id:
        raise QueryParameterMissing('episode_id')

    db = get_main_database()

    episode = get_single_result(db, 'episodes/by_id',
            key          = episode_id,
            include_docs = True,
            schema       = Episode,
        )

    if not episode:
        return None

    if current_id and episode._id != episode_id:
        raise MergedIdException(episode, episode._id)

    if episode.needs_update:
        incomplete_obj.send_robust(sender=episode)

    return episode
Beispiel #26
0
def podcast_for_url(url, create=False):

    if not url:
        raise QueryParameterMissing('url')

    key = 'podcast-by-url-%s' % sha1(url.encode('utf-8')).hexdigest()

    podcast = cache.get(key)
    if podcast:
        return podcast

    db = get_main_database()
    podcast_group = get_single_result(db, 'podcasts/by_url',
            key          = url,
            include_docs = True,
            wrapper      = _wrap_pg,
        )

    if podcast_group:
        podcast = podcast_group.get_podcast_by_url(url)

        if podcast.needs_update:
            incomplete_obj.send_robust(sender=podcast)
        else:
            cache.set(key, podcast)

        return podcast

    if create:
        podcast = Podcast()
        podcast.created_timestamp = get_timestamp(datetime.utcnow())
        podcast.urls = [url]
        podcast.save()
        incomplete_obj.send_robust(sender=podcast)
        return podcast

    return None
Beispiel #27
0
def get_flattr_podcast_count():
    """ returns the number of podcasts that contain Flattr payment URLs """
    db = get_main_database()
    r = get_single_result(db, 'podcasts/flattr')
    return r['value']