def process_one_album(idx, alb): # Build up an Album entity. kwargs = {} kwargs["parent"] = idx.transaction kwargs["title"] = alb.title() kwargs["album_id"] = alb.album_id kwargs["import_timestamp"] = datetime.datetime.utcfromtimestamp( alb.import_timestamp()) kwargs["num_tracks"] = len(alb.all_au_files) kwargs["import_tags"] = alb.tags() if alb.is_compilation(): kwargs["is_compilation"] = True else: kwargs["is_compilation"] = False kwargs["album_artist"] = get_artist_by_name(alb.artist_name()) #for key, val in sorted(kwargs.iteritems()): #print "%s: %s" % (key, val) if seen_album(alb.album_id): #print " Skipping" return album = models.Album(**kwargs) # Look for a disc number in the tags. for tag in kwargs["import_tags"]: m = _DISC_NUM_RE.search(tag) if m: album.disc_number = int(m.group(1)) break idx.add_album(album) for au_file in alb.all_au_files: track_title, import_tags = titles.split_tags(au_file.tit2()) track_num, _ = order.decode( unicode(au_file.mutagen_id3["TRCK"])) kwargs = {} if alb.is_compilation(): kwargs["track_artist"] = get_artist_by_name(au_file.tpe1()) track = models.Track( parent=idx.transaction, ufid=au_file.ufid(), album=album, title=track_title, import_tags=import_tags, track_num=track_num, sampling_rate_hz=au_file.mp3_header.sampling_rate_hz, bit_rate_kbps=int(au_file.mp3_header.bit_rate_kbps), channels=au_file.mp3_header.channels_str, duration_ms=au_file.duration_ms, **kwargs) idx.add_track(track)
def main(): parser = optparse.OptionParser(usage='%prog') parser.add_option('--clear-datastore', action='store_true') (options, args) = parser.parse_args() startup_appengine(clear_datastore=options.clear_datastore) from djdb import search, models import datetime idx = search.Indexer() # Create some test artists. art1 = models.Artist(name=u"Fall, The", parent=idx.transaction, key_name="art1") art2 = models.Artist(name=u"Eno, Brian", parent=idx.transaction, key_name="art2") # Create some test albums. alb1 = models.Album(title=u"This Nation's Saving Grace", album_id=12345, import_timestamp=datetime.datetime.now(), album_artist=art1, num_tracks=123, parent=idx.transaction) alb2 = models.Album(title=u"Another Green World", album_id=67890, import_timestamp=datetime.datetime.now(), album_artist=art2, num_tracks=456, parent=idx.transaction) for i, track_title in enumerate( (u"Spider And I", u"Running To Tie Your Shoes", u"Kings Lead Hat")): idx.add_track( models.Track(ufid="test3-%d" % i, album=alb2, sampling_rate_hz=44110, bit_rate_kbps=128, channels="mono", duration_ms=789, title=track_title, track_artist=art2, track_num=i + 1, parent=idx.transaction)) idx.add_artist(art1) idx.add_artist(art2) idx.add_album(alb1) idx.add_album(alb2) idx.save() # saves all objects print "created some artists and stuff"
def bootstrap(request): """Inject test library data into the datastore.""" # We never want to do this in prod! if in_prod(): return http.HttpResponse(status=403) # First, inject the artist names. search.create_artists(_ARTIST_NAMES) # Now build up a bunch of random albums for each artist. counter = 1 for art in models.Artist.all().fetch(100): for _ in range(random.randint(1, 10)): # 10% are multi-disc sets discs = [None] if random.randint(1, 10) == 1: discs = range(1, random.randint(2, 5)) idx = search.Indexer() for disc_num in discs: counter += 1 alb = models.Album( title=random_phrase(), #label=_LABELS[random.randint(0, len(_LABELS) - 1)], #year=_YEARS[random.randint(0, len(_YEARS) - 1)], disc_number=disc_num, album_id=counter, import_timestamp=datetime.datetime.now(), album_artist=art, num_tracks=random.randint(2, 12), parent=idx.transaction, ) idx.add_album(alb) for i in range(alb.num_tracks): trk = models.Track(ufid="%d:%d" % (counter, i), album=alb, title=random_phrase(), track_num=i + 1, sampling_rate_hz=44100, bit_rate_kbps=128, channels=u"stereo", duration_ms=random.randint( 60000, 300000), parent=idx.transaction) idx.add_track(trk) idx.save() return http.HttpResponseRedirect("/djdb/")
def test_track(self): # Check key generation against a known case. self.assertEqual(u"djdb/t:test ufid", models.Track.get_key_name("test ufid")) alb = models.Album(title='test album', album_id=12345, import_timestamp=datetime.datetime.now(), album_artist=self.test_artist, current_tags=[u"zap", u"explicit", u"foo"], num_tracks=13) trk = models.Track(ufid="test ufid", album=alb, sampling_rate_hz=44100, bit_rate_kbps=320, channels='stereo', duration_ms=123456, title="test track", current_tags=[u"zap", u"explicit", u"foo"], track_num=4) # Check that the ufid property works properly. self.assertEqual("test ufid", trk.ufid) # Our human-readable duration is truncated to the nearest second. self.assertEqual("2:03", trk.duration) # Check that the artist_name property works when no track_artist # has been set. self.assertEqual(self.test_artist.name, trk.artist_name) # Check that the unicode representation of the object is the track # name. self.assertEqual("test track", unicode(trk)) # Check that tag-related things work. for obj in (alb, trk): self.assertEqual([u"explicit", u"foo", u"zap"], obj.sorted_current_tags) self.assertTrue(trk.is_explicit) trk.current_tags = [u"foo"] self.assertFalse(trk.is_explicit) # Set a separate artist for this track, then check that the # artist_name property still works. trk_art = models.Artist(name='Track Artist') trk_art.save() trk.track_artist = trk_art self.assertEqual(trk.track_artist.name, trk.artist_name)
def test_album_sorted_tracks(self): art = models.Artist(name='Test Artist') art.save() alb = models.Album(title='test album', album_id=12345, import_timestamp=datetime.datetime.now(), album_artist=art, num_tracks=6) alb.save() # Add our test tracks in a fixed but random order. test_track_nums = (4, 2, 1, 3, 6, 5) for track_num in test_track_nums: trk = models.Track(ufid="test ufid %d" % track_num, album=alb, sampling_rate_hz=44100, bit_rate_kbps=320, channels='stereo', duration_ms=123456, title="test track %d" % track_num, track_num=track_num) trk.save() # Make sure the tracks come back in proper sort order. self.assertEqual(sorted(test_track_nums), [trk.track_num for trk in alb.sorted_tracks])
def test_object_indexing(self): idx = search.Indexer() # Create some test artists. art1 = models.Artist(name=u"Fall, The", parent=idx.transaction, key_name="art1") art2 = models.Artist(name=u"Eno, Brian", parent=idx.transaction, key_name="art2") # Create some test single-artist albums. alb1 = models.Album(title=u"This Nation's Saving Grace", year=1985, album_id=12345, label=u"Some Label", import_timestamp=datetime.datetime.now(), album_artist=art1, num_tracks=123, parent=idx.transaction) trk1 = [] for i, track_title in enumerate( (u"Mansion", u"Bombast", u"Cruiser's Creek", u"What You Need", u"Spoiled Victorian Child", u"L.A.")): trk1.append( models.Track(ufid="test1-%d" % i, album=alb1, sampling_rate_hz=44110, bit_rate_kbps=320, channels="stereo", duration_ms=123, title=track_title, track_num=i + 1, parent=idx.transaction)) alb2 = models.Album(title=u"Another Green World", album_id=67890, label=u"Some Label", import_timestamp=datetime.datetime.now(), album_artist=art2, num_tracks=456, parent=idx.transaction) trk2 = [] for i, track_title in enumerate( (u"Sky Saw", u"Over Fire Island", u"St. Elmo's Fire", u"In Dark Trees", u"The Big Ship")): trk2.append( models.Track(ufid="test2-%d" % i, album=alb2, sampling_rate_hz=44110, bit_rate_kbps=192, channels="joint_stereo", duration_ms=456, title=track_title, track_num=i + 1, parent=idx.transaction)) # Create a test album that is a compilation. alb3 = models.Album(title=u"R&B Gold: 1976", album_id=76543, label=u"Some Label", import_timestamp=datetime.datetime.now(), is_compilation=True, num_tracks=789, parent=idx.transaction) trk3_art = [] trk3 = [] for i, (artist_name, track_title) in enumerate( ((u"Earth, Wind & Fire", u"Sing A Song"), (u"Diana Ross", u"Love Hangover"), (u"Aretha Franklin", u"Something He Can Feel"), (u"KC & the Sunshine Band", u"(Shake, Shake, Shake) Shake Your Booty"))): art = models.Artist(name=artist_name, key_name=artist_name, parent=idx.transaction) trk3_art.append(art) trk3.append( models.Track(ufid="test3-%d" % i, album=alb3, sampling_rate_hz=44110, bit_rate_kbps=128, channels="mono", duration_ms=789, title=track_title, track_artist=art, track_num=i + 1, parent=idx.transaction)) # Now index everything we just created. idx.add_artist(art1) idx.add_artist(art2) for art in trk3_art: idx.add_artist(art) idx.add_album(alb1) idx.add_album(alb2) idx.add_album(alb3) for trk in trk1 + trk2 + trk3: idx.add_track(trk) idx.save() # This also saves all of the objects. # Now do some test searches. # This query matches the album and all of the tracks. expected = {alb1.key(): set(["title"])} self.assertEqual( expected, search.fetch_keys_for_query_string(u"nations", entity_kind="Album")) for t in trk1: expected[t.key()] = set(["album"]) self.assertEqual(expected, search.fetch_keys_for_query_string(u"nations")) # The query "fire" should match: # * Two of the songs from "Another Green World" # * The band "Earth, Wind & Fire" # * The EW&F track from the compilation. expected = { trk2[1].key(): set(["title"]), trk2[2].key(): set(["title"]), trk3_art[0].key(): set(["name"]), trk3[0].key(): set(["artist"]), } self.assertEqual(expected, search.fetch_keys_for_query_string(u"fire"))