def website(self): """Look for a URL in the audio metadata, or a Google search if no URL can be found.""" if "website" in self: return self.list("website")[0] for cont in self.list("contact") + self.list("comment"): c = cont.lower() if (c.startswith("http://") or c.startswith("https://") or c.startswith("www.")): return cont elif c.startswith("//www."): return "http:" + cont else: text = "http://www.google.com/search?q=" esc = lambda c: ord(c) > 127 and '%%%x'%ord(c) or c if "labelid" in self: text += ''.join(map(esc, self["labelid"])) else: artist = util.escape("+".join(self("artist").split())) album = util.escape("+".join(self("album").split())) artist = util.encode(artist) album = util.encode(album) artist = "%22" + ''.join(map(esc, artist)) + "%22" album = "%22" + ''.join(map(esc, album)) + "%22" text += artist + "+" + album text += "&ie=UTF8" return text
def to_dump(self): """A string of 'key=value' lines, similar to vorbiscomment output.""" s = [] for k in self.keys(): k = str(k) if isinstance(self[k], int) or isinstance(self[k], long): s.append("%s=%d" % (k, self[k])) elif isinstance(self[k], float): s.append("%s=%f" % (k, self[k])) else: for v2 in self.list(k): if isinstance(v2, str): s.append("%s=%s" % (k, v2)) else: s.append("%s=%s" % (k, util.encode(v2))) for k in (INTERN_NUM_DEFAULT - set(self.keys())): s.append("%s=%d" % (k, self.get(k, 0))) if "~#rating" not in self: s.append("~#rating=%f" % self("~#rating")) s.append("~format=%s" % self.format) s.append("") return "\n".join(s)
def copy(self, songlist, song): if self.__load_db() is None: return False track = gpod.itdb_track_new() # All values should be utf-8 encoded strings # Filepaths should be encoded with the fs encoding # Either combine tags with comma, or only take the first value if self['all_tags']: tag = song.comma else: tag = lambda key: (song.list(key) or ('',))[0] title = tag('title') if self['title_version'] and song('version'): title = " - ".join([title, song('version')]) track.title = util.encode(title) album = tag('album') if self['album_part'] and song('discsubtitle'): album = " - ".join([album, song('discsubtitle')]) track.album = util.encode(album) # String keys for key in ['artist', 'genre', 'grouping', 'composer', 'albumartist']: if hasattr(track, key): # albumartist since libgpod-0.4.2 setattr(track, key, util.encode(tag(key))) # Sort keys (since libgpod-0.5.0) for key in ['artist', 'album', 'albumartist']: if hasattr(track, 'sort_' + key): setattr(track, 'sort_' + key, util.encode(tag(key + 'sort'))) # Numeric keys for key in ['bitrate', 'playcount', 'year']: try: setattr(track, key, int(song('~#'+key))) except ValueError: continue # Numeric keys where the names differ for key, value in { 'cd_nr': song('~#disc'), 'cds': song('~#discs'), 'rating': min(100, song('~#rating') * 100), 'time_added': self.__mactime(time.time()), 'time_modified': self.__mactime(util.mtime(song('~filename'))), 'track_nr': song('~#track'), 'tracklen': song('~#length') * 1000, 'tracks': song('~#tracks'), 'size': util.size(song('~filename')), 'soundcheck': self.__soundcheck(song), }.items(): try: setattr(track, key, int(value)) except ValueError: continue track.filetype = song('~format') track.comment = util.encode(util.fsdecode(song('~filename'))) # Associate a cover with the track if self['covers']: cover = song.find_cover() if cover: # libgpod will copy the file later when the iTunesDB # is saved, so we have to keep a reference around in # case the cover is a temporary file. self.__covers.append(cover) gpod.itdb_track_set_thumbnails( track, util.fsencode(cover.name)) # Add the track to the master playlist gpod.itdb_track_add(self.__itdb, track, -1) master = gpod.itdb_playlist_mpl(self.__itdb) gpod.itdb_playlist_add_track(master, track, -1) # Copy the actual file if gpod.itdb_cp_track_to_ipod(track, song['~filename'], None) == 1: return IPodSong(track) else: return False
def test_unicode(self): self.failUnlessEqual(util.encode(u"abcde"), "abcde")
def test_empty(self): self.failUnlessEqual(util.encode(""), "")