def test_timeline_to_models_with_min_songs(): models = timeline_to_models(kvf_stream_to_timeline(STREAM), min_songs=2) assert len(models) == 7 assert len([True for model in models if model.get_type() == 'MusicGroup']) == 2 assert len([True for model in models if model.get_type() == 'MusicRecording']) == 5 models = timeline_to_models(kvf_stream_to_timeline(STREAM), min_songs=3) assert len(models) == 4 assert len([True for model in models if model.get_type() == 'MusicGroup']) == 1 assert len([True for model in models if model.get_type() == 'MusicRecording']) == 3
def main(): """ Renders a graph """ logger = logging.getLogger('render_graph') logger.info("Going to parse a stream from %s", INPUT_FILE) # read and parse the stream into a timeline timeline = list(kvf_stream_to_timeline(read_gzip(INPUT_FILE))) logger.info('Got a timeline with %d entries', len(timeline)) logger.info(timeline[0]) logger.info(timeline[-1]) # now get models for artists and songs models = timeline_to_models(timeline, min_songs=MIN_SONGS) # some stats artists = len([True for model in models if model.get_type() == 'MusicGroup']) songs = len([True for model in models if model.get_type() == 'MusicRecording']) logger.info('Got a %d models (%d artists and %d songs)', len(models), artists, songs) # ok, now render the graph graph_json = models_to_graph_json(models, as_json=True, json_indent=False) print('// {} artists and {} songs'.format(artists, songs)) print('var graph={};'.format(graph_json))
def test_kvf_stream_from_string2(): stream = """ data: {"updated":"2019-01-27T19:55:55.896","now":{"artist":"Moyzeskvartettin","title":"String Quartet No.17 in Bb, K.458 ('The Hunt') 2.Menuetto- Moderato","start":"2019-01-27T19:55:54.386"},"next":{"artist":{},"title":{},"start":{}}} data: {"updated":"2019-01-27T19:59:51.112","now":{"artist":{},"title":"Ann ti Orff","start":"2019-01-27T19:59:49.625"},"next":{"artist":{},"title":{},"start":{}}} data: {"updated":"2019-01-27T19:59:51.112","now":{"artist":{},"title":"Ann ti Orff","start":"2019-01-27T19:59:49.625"},"next":{"artist":{},"title":{},"start":{}}} data: {"updated":"2019-01-27T20:00:48.644","now":{"artist":{},"title":"Carmina Burana FSO","start":"2019-01-27T20:00:47.250"},"next":{"artist":{},"title":{},"start":{}}} data: {"updated":"2019-01-27T21:03:37.157","now":{"artist":{},"title":{},"start":{}},"next":{"artist":{},"title":{},"start":{}}} data: {"updated":"2019-01-27T22:00:27.400","now":{"artist":{},"title":{},"start":{}},"next":{"artist":"Elisa's","title":"Be mig! Se mig! Ge mig!","start":"2019-01-27T22:03:11.724"}} data: {"updated":"2019-01-27T22:00:27.400","now":{"artist":{},"title":{},"start":{}},"next":{"artist":"Elisa's","title":"Be mig! Se mig! Ge mig!","start":"2019-01-27T22:03:11.724"}} data: {"updated":"2019-01-27T22:00:27.400","now":{"artist":{},"title":{},"start":{}},"next":{"artist":"Elisa's","title":"Be mig! Se mig! Ge mig!","start":"2019-01-27T22:03:11.724"}} data: {"updated":"2019-01-27T22:03:08.947","now":{"artist":"Elisa's","title":"Be mig! Se mig! Ge mig!","start":"2019-01-27T22:03:06.959"},"next":{"artist":"Doodle Bugs","title":"Suderø","start":"2019-01-27T22:06:10.573"}} """.strip().split("\n") timeline = list(kvf_stream_to_timeline(stream)) print_timeline(timeline) assert len(timeline) == 2 assert [entry.artist_name for entry in timeline] == \ ['Moyzeskvartettin', "Elisa's"] assert [entry.song_title for entry in timeline] == \ ["String Quartet No.17 in Bb, K.458 ('The Hunt') 2.Menuetto- Moderato", 'Be mig! Se mig! Ge mig!'] assert [entry.duration for entry in timeline] == \ [236, 183]
def test_stats_for_stream_from_data(): in_file = read_gzip(path.join(dir_name, '..', 'data', 'kvf.log.gz')) timeline = kvf_stream_to_timeline(in_file) stats = get_timeline_stats(timeline) print(stats) # top artists assert ('Eyðun Nolsøe', 16) in stats['top_artists'] assert ('Marius Ziska', 16) in stats['top_artists'] # top songs assert stats['top_songs'][0] == ('Lukkan er ei gullið', 10) assert stats['top_songs'][1] == ('Á tíni slóð', 9) # longest songs assert stats['longest_songs'][0] == ( 'Symphony in C minor I. Allegro molto', 844) assert stats['longest_songs'][1] == ( 'Mendelssohn: Piano Sextet In D, Op. 110 - 1. Allegro Vivace', 773) # artist which are most frequently played on the air assert stats['longest_artists'][0] == ( 'Ilona Prunyi', 2433) # The Seasons, Op. 37b: May. "May Nights" assert stats['longest_artists'][1] == ( 'Gothenburg Symphony Orchestra cond. Okko Kamu, Cond.: Okko Kamu', 2210)
def test_read_stream_from_data(): in_file = read_gzip(path.join(dir_name, '..', 'data', 'kvf.log.gz')) timeline = list(kvf_stream_to_timeline(in_file)) # print_timeline(timeline) assert len(timeline) == 2197
def test_kvf_stream_from_string(): stream = """ data: {"updated":"2019-02-04T13:25:04.585","now":{"artist":"Rita Ora & Rudimental","title":"Summer Love","start":"2019-02-04T13:25:03.090"},"next":{"artist":"Alvaro Soler","title":"La Cintura","start":"2019-02-04T13:29:21.084"}} data: {"updated":"2019-02-04T13:25:04.585","now":{"artist":{},"title":{},"start":"2019-02-04T13:25:03.090"},"next":{"artist":"Alvaro Soler","title":"La Cintura","start":"2019-02-04T13:29:21.084"}} data: {"updated":"2019-02-04T13:49:36.550","now":{"artist":{},"title":{},"start":"2019-02-04T13:25:03.090"},"next":{"artist":"Bent Fabricius-Bjerre","title":"Nøglen til paradis","start":"2019-02-04T13:29:21.084"}} data: {"updated":"2019-02-04T14:00:01.086","now":{"artist":{},"title":{},"start":{}},"next":{"artist":{},"title":{},"start":{}}} data: {"updated":"2019-02-04T14:37:16.794","now":{"artist":"Vestmenn","title":"Ró","start":"2019-02-04T14:37:15.186"},"next":{"artist":"Kári P","title":"Bara tú riggar","start":"2019-02-04T14:43:20.266"}} """.strip().split("\n") timeline = list(kvf_stream_to_timeline(stream)) print_timeline(timeline) assert len(timeline) == 2
def test_kvf_stream_to_timeline_missing_timestamps(): in_file = read_text(path.join(dir_name, 'fixtures', 'streamB')) timeline = list(kvf_stream_to_timeline(in_file)) print_timeline(timeline) assert len(timeline) == 4 assert [entry.artist_name for entry in timeline] == \ ['Fróði Bjarnason', 'Vincent', 'Enekk', 'KYLIE MINOGUE feat. JACK SAVORETTI'] assert [entry.song_title for entry in timeline] == \ ['Where My Home Is', 'Dyrabart', 'Mín sorg', "Music's Too Sad Without You (edit)"] assert [entry.duration for entry in timeline] == \ [256, 287, 245, 206]
def main(): """ Renders a stream statistics """ logger = logging.getLogger('render_graph') logger.info("Going to parse a stream from %s", INPUT_FILE) in_file = read_gzip(INPUT_FILE) timeline = list(kvf_stream_to_timeline(in_file)) logger.info('Got a timeline with %d entries', len(timeline)) stats = get_timeline_stats(timeline) for key, value in stats.items(): print("{}: {}\n".format(key, value))
def test_kvf_stream_to_timeline_gzip(): in_file = read_gzip(path.join(dir_name, 'fixtures', 'streamA.gz')) timeline = list(kvf_stream_to_timeline(in_file)) print_timeline(timeline) assert len(timeline) == 5 assert [entry.artist_name for entry in timeline] == \ ['Jasmin', 'Teitur', 'Hamradun', 'Frændur', 'Wolfgang'] assert [entry.song_title for entry in timeline] == \ ['Make Sense', 'I Want to Be Kind', 'Ein stutt og stokkut løta', 'Uttanumtos', 'Ice Cold'] assert [entry.duration for entry in timeline] == \ [254, 249, 165, 208, 180] assert timeline[0].played_at.hour == 20, 'Has been played at 8 pm'
def test_timeline_to_models(): models = timeline_to_models(kvf_stream_to_timeline(STREAM)) assert len(models) == 9 assert "\n".join(map(repr, models)) == """ <ArtistModel https://schema.org/MusicGroup (Eivør Pálsdóttir) songs = "3"> <ArtistModel https://schema.org/MusicGroup (Enekk) songs = "2"> <ArtistModel https://schema.org/MusicGroup (Orka) songs = "1"> <SongModel https://schema.org/MusicRecording (Elisabeth og Elinborg) duration = "253"> --[:byArtist]->(Eivør Pálsdóttir) <SongModel https://schema.org/MusicRecording (Mannabarn) duration = "292"> --[:byArtist]->(Eivør Pálsdóttir) <SongModel https://schema.org/MusicRecording (Vársins ljóð) duration = "274"> --[:byArtist]->(Eivør Pálsdóttir) <SongModel https://schema.org/MusicRecording (Slatur) duration = "195"> --[:byArtist]->(Enekk) <SongModel https://schema.org/MusicRecording (Ódn) duration = "268"> --[:byArtist]->(Enekk) <SongModel https://schema.org/MusicRecording (Hon leitar) duration = "132"> --[:byArtist]->(Orka) """.strip()