def test_DateFrame(): from eyed3.id3.frames import DateFrame from eyed3.core import Date # Default ctor df = DateFrame(b"TDRC") assert_equal(df.text, u"") assert_is_none(df.date) # Ctor with eyed3.core.Date arg for d in [ Date(2012), Date(2012, 1), Date(2012, 1, 4), Date(2012, 1, 4, 18), Date(2012, 1, 4, 18, 15), Date(2012, 1, 4, 18, 15, 30), ]: df = DateFrame(b"TDRC", d) assert_equal(df.text, unicode(str(d))) # Comparison is on each member, not reference ID assert_equal(df.date, d) # Test ctor str arg is converted for d in [ "2012", "2010-01", "2010-01-04", "2010-01-04T18", "2010-01-04T06:20", "2010-01-04T06:20:15", u"2012", u"2010-01", u"2010-01-04", u"2010-01-04T18", u"2010-01-04T06:20", u"2010-01-04T06:20:15", ]: df = DateFrame(b"TDRC", d) dt = Date.parse(d) assert_equal(df.text, unicode(str(dt))) assert_equal(df.text, unicode(d)) # Comparison is on each member, not reference ID assert_equal(df.date, dt) # Invalid dates for d in ["1234:12"]: date = DateFrame(b"TDRL") date.date = d assert_false(date.date) try: date.date = 9 except TypeError: pass else: assert_false("TypeError not thrown")
def testFileInfoConstructor(): from eyed3.id3.tag import FileInfo # Both bytes and unicode input file names must be accepted and the former # must be converted to unicode. for name in [__file__, unicode(__file__)]: fi = FileInfo(name) assert type(fi.name) is unicode assert name == unicode(name) assert fi.tag_size == 0
def test_DateFrame(): from eyed3.id3.frames import DateFrame from eyed3.core import Date # Default ctor df = DateFrame(b"TDRC") assert df.text == u"" assert df.date is None # Ctor with eyed3.core.Date arg for d in [Date(2012), Date(2012, 1), Date(2012, 1, 4), Date(2012, 1, 4, 18), Date(2012, 1, 4, 18, 15), Date(2012, 1, 4, 18, 15, 30), ]: df = DateFrame(b"TDRC", d) assert (df.text == unicode(str(d))) # Comparison is on each member, not reference ID assert (df.date == d) # Test ctor str arg is converted for d in ["2012", "2010-01", "2010-01-04", "2010-01-04T18", "2010-01-04T06:20", "2010-01-04T06:20:15", u"2012", u"2010-01", u"2010-01-04", u"2010-01-04T18", u"2010-01-04T06:20", u"2010-01-04T06:20:15", ]: df = DateFrame(b"TDRC", d) dt = Date.parse(d) assert (df.text == unicode(str(dt))) assert (df.text == unicode(d)) # Comparison is on each member, not reference ID assert (df.date == dt) # Invalid dates for d in ["1234:12"]: date = DateFrame(b"TDRL") date.date = d assert not date.date try: date.date = 9 except TypeError: pass else: pytest.fail("TypeError not thrown")
def _getOne(self, key, values, default=None, Type=UnicodeType, required=True): values = set(values) if None in values: values.remove(None) if len(values) != 1: printMsg(u"Detected %s %s names%s" % ( "0" if len(values) == 0 else "multiple", key, "." if not values else (":\n\t%s" % "\n\t".join([compat.unicode(v) for v in values])), )) value = prompt(u"Enter %s" % key.title(), default=default, type_=Type, required=required) else: value = values.pop() return value
def test_ObjectFrame(audiofile, id3tag): sixsixsix = b"\x29\x0a" * 666 with Path(__file__).open("rb") as fp: thisfile = fp.read() obj1 = ObjectFrame(description=u"Test Object", object_data=sixsixsix, filename=u"666.txt", mime_type="text/satan") obj2 = ObjectFrame(description=u"Test Object2", filename=unicode(__file__), mime_type="text/python", object_data=thisfile) id3tag.frame_set[OBJECT_FID] = obj1 id3tag.frame_set[OBJECT_FID].append(obj2) audiofile.tag = id3tag audiofile.tag.save() file = eyed3.load(audiofile.path) assert len(file.tag.objects) == 2 obj1_2 = file.tag.objects.get(u"Test Object") assert obj1_2.mime_type == "text/satan" assert obj1_2.object_data == sixsixsix assert obj1_2.filename == u"666.txt" obj2_2 = file.tag.objects.get(u"Test Object2") assert obj2_2.mime_type == "text/python" assert obj2_2.object_data == thisfile assert obj2_2.filename == __file__
def getXML(self, audio_file): tag = audio_file.tag xml = u"<tune xmlns='http://jabber.org/protocol/tune'>\n" if tag.artist: xml += " <artist>%s</artist>\n" % tag.artist if tag.title: xml += " <title>%s</title>\n" % tag.title if tag.album: xml += " <source>%s</source>\n" % tag.album xml += (" <track>file://%s</track>\n" % compat.unicode(os.path.abspath(audio_file.path))) if audio_file.info: xml += " <length>%s</length>\n" % \ compat.unicode(audio_file.info.time_secs) xml += "</tune>\n" return xml
def __compile(self): # TODO: add support for comments in pattern parser = DisplayPatternParser(whitespace='') try: asts = parser.parse(self.__text, rule_name='start') self.sub_patterns = self.__compile_asts(asts) self.__text = None except BaseException as parsing_error: raise PatternCompileException(compat.unicode(parsing_error))
def testSortOrderConversions(): test_file = "/tmp/soconvert.id3" tag = Tag() # 2.3 frames to 2.4 for fid in ["XSOA", "XSOP", "XSOT"]: frame = frames.TextFrame(fid) frame.text = unicode(fid) tag.frame_set[fid] = frame try: tag.save(test_file) # v2.4 is the default tag = eyed3.load(test_file).tag assert_equal(tag.version, ID3_V2_4) assert_equal(len(tag.frame_set), 3) del tag.frame_set["TSOA"] del tag.frame_set["TSOP"] del tag.frame_set["TSOT"] assert_equal(len(tag.frame_set), 0) finally: os.remove(test_file) tag = Tag() # 2.4 frames to 2.3 for fid in ["TSOA", "TSOP", "TSOT"]: frame = frames.TextFrame(fid) frame.text = unicode(fid) tag.frame_set[fid] = frame try: tag.save(test_file, version=eyed3.id3.ID3_V2_3) tag = eyed3.load(test_file).tag assert_equal(tag.version, ID3_V2_3) assert_equal(len(tag.frame_set), 3) del tag.frame_set["XSOA"] del tag.frame_set["XSOP"] del tag.frame_set["XSOT"] assert_equal(len(tag.frame_set), 0) finally: os.remove(test_file)
def testChapters(): test_file = "/tmp/chapters.id3" t = Tag() ch1 = t.chapters.set(b"c1", (0, 200)) ch2 = t.chapters.set(b"c2", (200, 300)) ch3 = t.chapters.set(b"c3", (300, 375)) ch4 = t.chapters.set(b"c4", (375, 600)) assert len(t.chapters) == 4 for i, c in enumerate(iter(t.chapters), 1): if i != 2: c.title = u"Chapter %d" % i c.subtitle = u"Subtitle %d" % i c.user_url = unicode("http://example.com/%d" % i).encode("ascii") t.save(test_file) try: t2 = eyed3.load(test_file).tag finally: os.remove(test_file) assert len(t2.chapters) == 4 for i in range(1, 5): c = t2.chapters.get(unicode("c%d" % i).encode("latin1")) if i == 2: assert c.title is None assert c.subtitle is None assert c.user_url is None else: assert c.title == u"Chapter %d" % i assert c.subtitle == u"Subtitle %d" % i assert (c.user_url == unicode("http://example.com/%d" % i).encode("ascii"))
def _getOne(self, key, values, default=None, Type=unicode, required=True): values = set(values) if None in values: values.remove(None) if len(values) != 1: printMsg( u"Detected %s %s names%s" % ("0" if len(values) == 0 else "multiple", key, "." if not values else (":\n\t%s" % "\n\t".join([compat.unicode(v) for v in values])), )) value = prompt(u"Enter %s" % key.title(), default=default, type_=Type, required=required) else: value = values.pop() return value
def testSortOrderConversions(): test_file = "/tmp/soconvert.id3" tag = Tag() # 2.3 frames to 2.4 for fid in [b"XSOA", b"XSOP", b"XSOT"]: frame = frames.TextFrame(fid) frame.text = fid.decode("ascii") tag.frame_set[fid] = frame try: tag.save(test_file) # v2.4 is the default tag = eyed3.load(test_file).tag assert (tag.version == ID3_V2_4) assert (len(tag.frame_set) == 3) del tag.frame_set[b"TSOA"] del tag.frame_set[b"TSOP"] del tag.frame_set[b"TSOT"] assert (len(tag.frame_set) == 0) finally: os.remove(test_file) tag = Tag() # 2.4 frames to 2.3 for fid in [b"TSOA", b"TSOP", b"TSOT"]: frame = frames.TextFrame(fid) frame.text = unicode(fid) tag.frame_set[fid] = frame try: tag.save(test_file, version=eyed3.id3.ID3_V2_3) tag = eyed3.load(test_file).tag assert (tag.version == ID3_V2_3) assert (len(tag.frame_set) == 3) del tag.frame_set[b"XSOA"] del tag.frame_set[b"XSOP"] del tag.frame_set[b"XSOT"] assert (len(tag.frame_set) == 0) finally: os.remove(test_file)
def set_mp3_tag(mp3_path,lyric_path): """ set mp3 tag info here :param mp3_path: :param lyric_path: :return: """ f = open(lyric_path, 'r', encoding='utf-8') result = u"" for line in f.readlines(): result += line audiofile = eyed3.load(mp3_path) t = Tag() # basic info t.title = u"B with U" t.artist = u"群星" t.album = u"2018拜年祭" t.genre = u"Hip-Hop" t.track_num = (3, 5) t.disc_num = (1, 1) # date info t.original_release_date = "1994-04-07" t.release_date = "1994-04-07" t.encoding_date = "2002-03" t.recording_date = 1996 t.tagging_date = "2012-2-5" # comments t.comments.set(u"Gritty, yo!") t.comments.set(u"Brownsville, Brooklyn", u"Origin") t.lyrics.set(unicode(result), "b_with_u") audiofile.tag = t audiofile.tag.save(version=ID3_V2_4)
def handleDirectory(self, d, _): global md5_file_cache md5_file_cache.clear() try: if not self._file_cache: print("%s: nothing to do." % d) return printMsg("\nProcessing %s" % d) # File images dir_art = [] for img_file in self._dir_images: img_base = os.path.basename(img_file) art_file = ArtFile(img_file) try: pil_img = pilImage(img_file) except IOError as ex: printWarning(compat.unicode(ex)) continue if art_file.art_type: printMsg("file %s: %s\n\t%s" % (img_base, art_file.art_type, pilImageDetails(pil_img))) dir_art.append(art_file) else: printMsg("file %s: unknown (ignored)" % img_base) if not dir_art: print("No art files found.") self._retval += 1 # Tag images all_tags = sorted([f.tag for f in self._file_cache], key=lambda x: x.file_info.name) for tag in all_tags: file_base = os.path.basename(tag.file_info.name) for img in tag.images: try: pil_img = pilImage(img) except IOError as ex: printWarning(compat.unicode(ex)) continue if img.picture_type in art.FROM_ID3_ART_TYPES: img_type = art.FROM_ID3_ART_TYPES[img.picture_type] printMsg("tag %s: %s (Description: %s)\n\t%s" % (file_base, img_type, img.description, pilImageDetails(pil_img))) if self.args.update_files: assert(not self.args.update_tags) path = os.path.dirname(tag.file_info.name) if img.description.startswith(DESCR_FNAME_PREFIX): # Use filename from Image description fname = img.description[ len(DESCR_FNAME_PREFIX):].strip() fname = os.path.splitext(fname)[0] else: fname = art.FILENAMES[img_type][0].strip("*") fname = img.makeFileName(name=fname) if (md5File(os.path.join(path, fname)) == md5Data(img.image_data)): printMsg("Skipping writing of %s, file " "exists and is exactly the same." % img_file) else: img_file = makeUniqueFileName( os.path.join(path, fname), uniq=img.description) printWarning("Writing %s..." % img_file) with open(img_file, "wb") as fp: fp.write(img.image_data) else: printMsg("tag %s: unhandled image type %d (ignored)" % (file_base, img.picture_type)) # Copy file art to tags. if self.args.update_tags: assert(not self.args.update_files) for tag in all_tags: for art_file in dir_art: descr = "filename: %s" % \ os.path.splitext( os.path.basename(art_file.file_path))[0] tag.images.set(art_file.id3_art_type, art_file.image_data, art_file.mime_type, description=descr) tag.save() finally: # Cleans up... super(ArtPlugin, self).handleDirectory(d, _)
def get(self, request, format=None): content = { 'user': unicode(request.user), 'auth': unicode(request.auth), } return Response(content)
def testUnicode(self): assert (unicode(Genre(u"Hardcore")) == u"(129)Hardcore") assert (unicode(Genre(u"Grindcore")) == u"Grindcore")
def handleDirectory(self, d, _): global md5_file_cache md5_file_cache.clear() if not self._file_cache: log.debug("%s: nothing to do." % d) return try: all_tags = sorted([f.tag for f in self._file_cache if f.tag], key=lambda x: x.file_info.name) # If not deemed an album, move on. if len(set([t.album for t in all_tags])) > 1: log.debug("Skipping directory '%s', non-album." % d) return printMsg(cformat("\nChecking: ", Fore.BLUE) + d) # File images dir_art = [] for img_file in self._dir_images: img_base = os.path.basename(img_file) art_file = ArtFile(img_file) try: pil_img = pilImage(img_file) except IOError as ex: printWarning(compat.unicode(ex)) continue if art_file.art_type: self._verbose("file %s: %s\n\t%s" % (img_base, art_file.art_type, pilImageDetails(pil_img))) dir_art.append(art_file) else: self._verbose("file %s: unknown (ignored)" % img_base) if not dir_art: print(cformat("NONE", Fore.RED)) self._retval += 1 else: print(cformat("OK", Fore.GREEN)) # --download handling if not dir_art and self.args.download and _have_lastfm: tag = all_tags[0] artists = set([t.artist for t in all_tags]) if len(artists) > 1: artist_query = VARIOUS_ARTISTS else: artist_query = tag.album_artist or tag.artist try: url = getAlbumArt(artist_query, tag.album) resp = requests.get(url) if resp.status_code != 200: raise ValueError() except ValueError: print("Album art download not found") else: print("Downloading album art...") img = pilImage(io.BytesIO(resp.content)) cover = Path(d) / "cover.{}".format(img.format.lower()) assert not cover.exists() img.save(str(cover)) print("Save {cover}".format(cover=cover)) # Tag images for tag in all_tags: file_base = os.path.basename(tag.file_info.name) for img in tag.images: try: pil_img = pilImage(img) pil_img_details = pilImageDetails(pil_img) except (OSError, IOError) as ex: printWarning(compat.unicode(ex)) continue if img.picture_type in art.FROM_ID3_ART_TYPES: img_type = art.FROM_ID3_ART_TYPES[img.picture_type] self._verbose("tag %s: %s (Description: %s)\n\t%s" % (file_base, img_type, img.description, pil_img_details)) if self.args.update_files: assert(not self.args.update_tags) path = os.path.dirname(tag.file_info.name) if img.description.startswith(DESCR_FNAME_PREFIX): # Use filename from Image description fname = img.description[ len(DESCR_FNAME_PREFIX):].strip() fname = os.path.splitext(fname)[0] else: fname = art.FILENAMES[img_type][0].strip("*") fname = img.makeFileName(name=fname) if (md5File(os.path.join(path, fname)) == md5Data(img.image_data)): printMsg("Skipping writing of %s, file " "exists and is exactly the same." % fname) else: img_file = makeUniqueFileName( os.path.join(path, fname), uniq=img.description) printWarning("Writing %s..." % img_file) with open(img_file, "wb") as fp: fp.write(img.image_data) else: self._verbose( "tag %s: unhandled image type %d (ignored)" % (file_base, img.picture_type) ) # Copy file art to tags. if self.args.update_tags: assert(not self.args.update_files) for tag in all_tags: for art_file in dir_art: art_path = os.path.basename(art_file.file_path) printMsg("Copying %s to tag '%s' image" % (art_path, art_file.id3_art_type)) descr = "filename: %s" % os.path.splitext(art_path)[0] tag.images.set(art_file.id3_art_type, art_file.image_data, art_file.mime_type, description=descr) tag.save() finally: # Cleans up... super(ArtPlugin, self).handleDirectory(d, _)
def testUnicode(self): assert_equal(unicode(Genre(u"Hardcore")), u"(129)Hardcore") assert_equal(unicode(Genre(u"Grindcore")), u"Grindcore")