def write(self, au_file): """Adds a an audio file to the collection. Args: au_file: An AudioFile object to add to the collection. """ entry_data = {} entry_data["order_num"], entry_data["total_num"] = order.decode( str(au_file.mutagen_id3.get("TRCK"))) if entry_data["total_num"] is None: entry_data["total_num"] = 100 entry_data["artist"] = unicode_util.simplify( au_file.mutagen_id3.get("TPE1", _UNKNOWN_ARTIST)) entry_data["album"] = unicode_util.simplify( au_file.mutagen_id3.get("TALB", _UNKNOWN_ALBUM)) entry_data["song"] = unicode_util.simplify( au_file.mutagen_id3.get("TIT2", _UNKNOWN_SONG)) # TODO(trow): Set this somehow. entry_data["genre"] = "Unknown" entry_data["dir"] = _traktor_path_quote( au_file.canonical_directory(prefix=self._root_dir)) entry_data["file"] = au_file.canonical_filename() entry_data["volume"] = self._file_volume_quoted entry_data["bitrate"] = int( au_file.mp3_header.bit_rate_kbps * 1000) entry_data["size_in_kb"] = int(au_file.frame_size / 1024) entry_data["duration_s"] = int(au_file.duration_ms / 1000) entry_data["import_date"] = time.strftime( "%Y/%m/%d", time.gmtime(au_file.import_timestamp)) entry_data["modified_date"] = entry_data["import_date"] entry_data["modified_time"] = "35364" order_num = int(entry_data["order_num"]) # Clean up any XML-unsafe characters and wrap each value in # quotes. for k, v in entry_data.items(): new_v = xml.sax.saxutils.quoteattr(unicode(v)) if new_v != v: entry_data[k] = new_v # TODO(trow): For now, we build a list of all entries so that # we can fix the ordering --- that is because Traktor # idiotically chooses to order tracks based on the order they # appear in the NML file, not based on the track numbering. entry_key = (au_file.album_id, order_num) self._all_entries.append((entry_key, entry_data)) # TODO(trow): This is how we should do it! #self._out_fh.write(_NML_ENTRY % entry_data) self.num_entries += 1
def write(self, au_file): """Adds a an audio file to the collection. Args: au_file: An AudioFile object to add to the collection. """ entry_data = {} entry_data["order_num"], entry_data["total_num"] = order.decode( str(au_file.mutagen_id3.get("TRCK"))) if entry_data["total_num"] is None: entry_data["total_num"] = 100 entry_data["artist"] = unicode_util.simplify( au_file.mutagen_id3.get("TPE1", _UNKNOWN_ARTIST)) entry_data["album"] = unicode_util.simplify( au_file.mutagen_id3.get("TALB", _UNKNOWN_ALBUM)) entry_data["song"] = unicode_util.simplify( au_file.mutagen_id3.get("TIT2", _UNKNOWN_SONG)) # TODO(trow): Set this somehow. entry_data["genre"] = "Unknown" entry_data["dir"] = _traktor_path_quote( au_file.canonical_directory(prefix=self._root_dir)) entry_data["file"] = au_file.canonical_filename() entry_data["volume"] = self._file_volume_quoted entry_data["bitrate"] = int(au_file.mp3_header.bit_rate_kbps * 1000) entry_data["size_in_kb"] = int(au_file.frame_size / 1024) entry_data["duration_s"] = int(au_file.duration_ms / 1000) entry_data["import_date"] = time.strftime( "%Y/%m/%d", time.gmtime(au_file.import_timestamp)) entry_data["modified_date"] = entry_data["import_date"] entry_data["modified_time"] = "35364" order_num = int(entry_data["order_num"]) # Clean up any XML-unsafe characters and wrap each value in # quotes. for k, v in entry_data.items(): new_v = xml.sax.saxutils.quoteattr(unicode(v)) if new_v != v: entry_data[k] = new_v # TODO(trow): For now, we build a list of all entries so that # we can fix the ordering --- that is because Traktor # idiotically chooses to order tracks based on the order they # appear in the NML file, not based on the track numbering. entry_key = (au_file.album_id, order_num) self._all_entries.append((entry_key, entry_data)) # TODO(trow): This is how we should do it! #self._out_fh.write(_NML_ENTRY % entry_data) self.num_entries += 1
def test_decode(self): test_cases = (("1", 1, None), (" 6", 6, None), ("006", 6, None), ("1/2", 1, 2), ("3 of 7", 3, 7), ("03anything04", 3, 4)) for text, order_num, max_num in test_cases: self.assertEqual((order_num, max_num), order.decode(text)) # These should not be parseable. error_test_cases = ("", "xxx", "0", "-1", "0/3", "3/", "3/0", "6/5", "-1/4", "2/-1", "2/-", "3-4", "3/0") for text in error_test_cases: self.assertRaises(order.BadOrderError, order.decode, text)
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 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)