Ejemplo n.º 1
0
    def get_json(self):
        end = datetime.now()
        start = end - timedelta(days=7)
        qs = PlayCountSnapshot.all().filter("established >=", start).filter("established <=", end)

        # Collect the play counts.
        weekly = {}
        for count in qs.run():
            id = count.track_id
            weekly.setdefault(id, {"play_count": []})
            weekly[id].update(
                {"artist": count.artist_name, "release": count.album_title, "label": count.label, "id": id}
            )
            weekly[id]["play_count"].append(count.play_count)

        for key, stat in weekly.iteritems():
            pc = stat["play_count"]
            weekly[key].update(
                {
                    # Average the play counts per release.
                    "play_count": int(round(sum(pc) / len(pc), 1)),
                    # Make this ID shorter so it's easier for clients.
                    "id": hashlib.sha1(stat["id"]).hexdigest(),
                }
            )

        # Sort the releases in descending order of play count.
        rel = sorted(weekly.values(), key=lambda c: (c["play_count"], c["release"]), reverse=True)
        # Limit to top 40.
        rel = rel[0:40]

        return {"this_week": {"start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d"), "releases": rel}}
Ejemplo n.º 2
0
def clear_data():
    for pl in Playlist.all():
        for track in PlaylistTrack.all().filter('playlist =', pl):
            track.delete()
        pl.delete()
    for u in User.all():
        u.delete()
    for ob in PlayCountSnapshot.all():
        ob.delete()
Ejemplo n.º 3
0
def clear_data():
    for pl in Playlist.all():
        for track in PlaylistTrack.all().filter('playlist =', pl):
            track.delete()
        pl.delete()
    for u in User.all():
        u.delete()
    for ob in PlayCountSnapshot.all():
        ob.delete()
Ejemplo n.º 4
0
 def test_snapshot_count_track_ids(self):
     self.count()
     self.count()
     res = self.snapshot()
     res = self.snapshot()  # second run
     track_ids = [w.track_id for w in PlayCountSnapshot.all()]
     # For the same track name and album, the IDs should be the same.
     eq_(track_ids[0], track_ids[1])
     assert track_ids[0] is not None
     assert track_ids[1] is not None
Ejemplo n.º 5
0
 def test_snapshot_count_track_ids(self):
     self.count()
     self.count()
     res = self.snapshot()
     res = self.snapshot()  # second run
     track_ids = [w.track_id for w in PlayCountSnapshot.all()]
     # For the same track name and album, the IDs should be the same.
     eq_(track_ids[0], track_ids[1])
     assert track_ids[0] is not None
     assert track_ids[1] is not None
Ejemplo n.º 6
0
 def test_snapshot_count(self):
     self.count()
     self.count()
     res = self.snapshot()
     eq_(res.status_code, 200)
     snap = PlayCountSnapshot.all()[0]
     eq_(snap.established.strftime('%Y-%m-%d'),
         datetime.datetime.now().strftime('%Y-%m-%d'))
     eq_(snap.play_count, 2)
     eq_(snap.artist_name, self.track.artist_name)
     eq_(snap.album_title, self.track.album_title)
     eq_(snap.label, self.track.label)
Ejemplo n.º 7
0
 def test_snapshot_count(self):
     self.count()
     self.count()
     res = self.snapshot()
     eq_(res.status_code, 200)
     snap = PlayCountSnapshot.all()[0]
     eq_(snap.established.strftime('%Y-%m-%d'),
         datetime.datetime.now().strftime('%Y-%m-%d'))
     eq_(snap.play_count, 2)
     eq_(snap.artist_name, self.track.artist_name)
     eq_(snap.album_title, self.track.album_title)
     eq_(snap.label, self.track.label)
Ejemplo n.º 8
0
    def get_json(self):
        end = datetime.now()
        start = end - timedelta(days=7)
        qs = (PlayCountSnapshot.all().filter('established >=', start).filter(
            'established <=', end))

        # Collect the play counts.
        weekly = {}
        for count in qs.run():
            id = count.track_id
            weekly.setdefault(id, {'play_count': []})
            weekly[id].update({
                'artist': count.artist_name,
                'release': count.album_title,
                'label': count.label,
                'id': id
            })
            weekly[id]['play_count'].append(count.play_count)

        for key, stat in weekly.iteritems():
            pc = stat['play_count']
            weekly[key].update({
                # Average the play counts per release.
                'play_count': int(round(sum(pc) / len(pc), 1)),
                # Make this ID shorter so it's easier for clients.
                'id': hashlib.sha1(stat['id']).hexdigest()
            })

        # Sort the releases in descending order of play count.
        rel = sorted(weekly.values(),
                     key=lambda c: (c['play_count'], c['release']),
                     reverse=True)
        # Limit to top 40.
        rel = rel[0:40]

        return {
            'this_week': {
                'start': start.strftime('%Y-%m-%d'),
                'end': end.strftime('%Y-%m-%d'),
                'releases': rel
            }
        }
Ejemplo n.º 9
0
    def get_json(self):
        end = datetime.now()
        start = end - timedelta(days=7)
        qs = (PlayCountSnapshot.all()
              .filter('established >=', start)
              .filter('established <=', end))

        # Collect the play counts.
        weekly = {}
        for count in qs.run():
            id = count.track_id
            weekly.setdefault(id, {'play_count': []})
            weekly[id].update({'artist': count.artist_name,
                               'release': count.album_title,
                               'label': count.label,
                               'id': id})
            weekly[id]['play_count'].append(count.play_count)

        for key, stat in weekly.iteritems():
            pc = stat['play_count']
            weekly[key].update({
                # Average the play counts per release.
                'play_count': int(round(sum(pc) / len(pc), 1)),
                # Make this ID shorter so it's easier for clients.
                'id': hashlib.sha1(stat['id']).hexdigest()
            })

        # Sort the releases in descending order of play count.
        rel = sorted(weekly.values(),
                     key=lambda c: (c['play_count'], c['release']),
                     reverse=True)
        # Limit to top 40.
        rel = rel[0:40]

        return {
            'this_week': {
                'start': start.strftime('%Y-%m-%d'),
                'end': end.strftime('%Y-%m-%d'),
                'releases': rel
            }
        }
Ejemplo n.º 10
0
 def test_freeform_compilation(self):
     stevie, talking_book, tracks = create_stevie_wonder_album_data()
     talking_book.is_compilation = True
     talking_book.put()
     for artist, track in (('Stevie Wonder', 'Superstition'),
                           ('Stevie Wonder', 'Big Brother')):
         new_trk = PlaylistTrack(
             playlist=self.track.playlist,
             selector=self.track.selector,
             freeform_album_title='Talking Book',
             freeform_artist_name=artist,
             freeform_track_title=track,
             freeform_label='...')
         new_trk.put()
         self.count(track_key=new_trk.key())
     res = self.snapshot()
     eq_(res.status_code, 200)
     snap = PlayCountSnapshot.all()[0]
     eq_(snap.play_count, 2)
     eq_(snap.artist_name, 'Various')
     eq_(snap.album_title, 'Talking Book')
Ejemplo n.º 11
0
 def test_freeform_compilation(self):
     stevie, talking_book, tracks = create_stevie_wonder_album_data()
     talking_book.is_compilation = True
     talking_book.put()
     for artist, track in (('Stevie Wonder', 'Superstition'),
                           ('Stevie Wonder', 'Big Brother')):
         new_trk = PlaylistTrack(
             playlist=self.track.playlist,
             selector=self.track.selector,
             freeform_album_title='Talking Book',
             freeform_artist_name=artist,
             freeform_track_title=track,
             freeform_label='...')
         new_trk.put()
         self.count(track_key=new_trk.key())
     res = self.snapshot()
     eq_(res.status_code, 200)
     snap = PlayCountSnapshot.all()[0]
     eq_(snap.play_count, 2)
     eq_(snap.artist_name, 'Various')
     eq_(snap.album_title, 'Talking Book')