Esempio n. 1
0
File: utils.py Progetto: fk-lx/mygpo
def episode_listener_data(episode, start_date=datetime(2010, 1, 1), leap=timedelta(days=1)):
    """ Returns data for the episode listener timeseries

    An iterator with data for each day (starting from the first listen-event)
    is returned, where each day is represented by a dictionary

     * date: the day
     * listeners: the number of listeners on that day
     * episode: the episode, if it was released on that day, otherwise None
    """

    listeners = episode_listener_count_timespan(episode, start=start_date)

    if not listeners:
        return

    # we always start at the first listen-event
    start = listeners[0][0]
    start = datetime.combine(start, time())

    for d in daterange(start, leap=leap):
        next = d + leap

        if listeners and listeners[0] and listeners[0][0] == d.date():
            day, l = listeners.pop(0)
        else:
            l = 0

        released = episode.released and episode.released >= d and episode.released <= next
        released_episode = episode if released else None

        yield dict(date=d, listeners=l, episode=released_episode)
Esempio n. 2
0
def episode_listener_data(
    episode, start_date=datetime(2010, 1, 1), leap=timedelta(days=1)
):
    """Returns data for the episode listener timeseries

    An iterator with data for each day (starting from the first event
    is returned, where each day is represented by a ListenerData tuple"""
    history = EpisodeHistoryEntry.objects.filter(
        episode=episode, timestamp__gte=start_date
    )
    # contains play-counts, indexed by date {date: play-count}
    play_counts = playcounts_timerange(history)

    # we start either at the episode-release or the first listen-event
    events = (
        list(play_counts.keys()) + [episode.released.date()] if episode.released else []
    )

    if not events:
        return

    # we always start at the first listen-event
    start = min(events)
    for date in daterange(start, leap=leap):
        playcount = play_counts.get(date, 0)
        e = episode if (episode.released.date() == date) else None
        yield ListenerData(date, playcount, e)
Esempio n. 3
0
def listener_data(podcasts, start_date=datetime(2010, 1, 1), leap=timedelta(days=1)):
    """Returns data for the podcast listener timeseries

    An iterator with data for each day (starting from either the first released
    episode or the earliest play-event) is returned, where each day is
    reresented by a ListenerData tuple."""
    # index episodes by releaes-date
    episodes = Episode.objects.filter(podcast__in=podcasts, released__gt=start_date)
    episodes = {e.released.date(): e for e in episodes}

    history = EpisodeHistoryEntry.objects.filter(
        episode__podcast__in=podcasts, timestamp__gte=start_date
    )
    # contains play-counts, indexed by date {date: play-count}
    play_counts = playcounts_timerange(history)

    # we start either at the first episode-release or the first listen-event
    events = list(episodes.keys()) + list(play_counts.keys())

    if not events:
        # if we don't have any events, stop
        return

    start = min(events)
    for date in daterange(start, leap=leap):
        playcount = play_counts.get(date, 0)
        episode = episodes.get(date, None)
        yield ListenerData(date, playcount, episode)
Esempio n. 4
0
def episode_listener_data(episode, start_date=datetime(2010, 1, 1),
                          leap=timedelta(days=1)):
    """ Returns data for the episode listener timeseries

    An iterator with data for each day (starting from the first event
    is returned, where each day is represented by a ListenerData tuple """
    history = EpisodeHistoryEntry.objects\
                                 .filter(episode=episode,
                                         timestamp__gte=start_date)\
    # contains play-counts, indexed by date {date: play-count}
    play_counts = playcounts_timerange(history)

    # we start either at the episode-release or the first listen-event
    events = play_counts.keys() + \
             [episode.released.date()] if episode.released else []

    if not events:
        return

    # we always start at the first listen-event
    start = min(events)
    for date in daterange(start, leap=leap):
        playcount = play_counts.get(date, 0)
        e = episode if (episode.released.date() == date) else None
        yield ListenerData(date, playcount, e)
Esempio n. 5
0
def listener_data(podcasts, start_date=datetime(2010, 1, 1),
                  leap=timedelta(days=1)):
    """ Returns data for the podcast listener timeseries

    An iterator with data for each day (starting from either the first released
    episode or the earliest play-event) is returned, where each day is
    reresented by a ListenerData tuple. """
    # index episodes by releaes-date
    episodes = Episode.objects.filter(podcast__in=podcasts,
                                      released__gt=start_date)
    episodes = {e.released.date(): e for e in episodes}

    history = EpisodeHistoryEntry.objects\
                                 .filter(episode__podcast__in=podcasts,
                                         timestamp__gte=start_date)\
    # contains play-counts, indexed by date {date: play-count}
    play_counts = playcounts_timerange(history)

    # we start either at the first episode-release or the first listen-event
    events = episodes.keys() + play_counts.keys()

    if not events:
        # if we don't have any events, stop
        return

    start = min(events)
    for date in daterange(start, leap=leap):
        playcount = play_counts.get(date, 0)
        episode = episodes.get(date, None)
        yield ListenerData(date, playcount, episode)
Esempio n. 6
0
File: utils.py Progetto: fk-lx/mygpo
def listener_data(podcasts, start_date=datetime(2010, 1, 1), leap=timedelta(days=1)):
    """ Returns data for the podcast listener timeseries

    An iterator with data for each day (starting from either the first released
    episode or the earliest listen-event) is returned, where each day
    is reresented by a dictionary

     * date: the day
     * listeners: the number of listeners on that day
     * episode: (one of) the episode(s) released on that day
    """

    # pre-calculate episode list, make it index-able by release-date
    episodes = (episodes_for_podcast(podcast, since=start_date) for podcast in podcasts)
    episodes = flatten(episodes)
    episodes = dict((e.released.date(), e) for e in episodes)

    listeners = [ podcast_listener_count_timespan(p, start=start_date)
                    for p in podcasts ]
    listeners = filter(None, listeners)

    # we start either at the first episode-release or the first listen-event
    events = []

    if episodes.keys():
        events.append(min(episodes.keys()))

    if listeners:
        events.append(min([l[0][0] for l in listeners]))

    if not events:
        return

    start = min(events)

    for d in daterange(start, leap=leap):

        listener_sum = 0
        for l in listeners:
            if not l:
                continue

            day, count = l[0]
            if day == d:
                listener_sum += count
                l.pop(0)

        episode = episodes[d] if d in episodes else None

        yield dict(date=d, listeners=listener_sum, episode=episode)