def send_file_to_ipod(self, itdb, fname, tags): if not os.path.exists(fname): logger.error("File '%s' does not exist" % fname) return False logger.debug("Copying file '%s' to iPod..." % fname) podcasts = gpod.itdb_playlist_podcasts(itdb) track = gpod.itdb_track_new() track.visible = 1 track.filetype = "mp3" track.ipod_path = fname track.album = str(tags['album']) track.artist = str(tags['artist']) track.title = str(tags['title']) track.genre = str(tags['genre']) track.tracklen = tags['length'] track.playcount = 0 gpod.itdb_track_add(itdb, track, -1) gpod.itdb_playlist_add_track(podcasts, track, -1) is_copied = gpod.itdb_cp_track_to_ipod(track, fname, None) if is_copied: logger.info("File '%s' has been successfully copied to iPod" % fname) else: # roll back logger.error("File '%s' could not be copied to iPod" % fname) gpod.itdb_playlist_remove_track(podcasts, track) gpod.itdb_track_remove(track) track = None gpod.itdb_write(itdb, None) return is_copied
def copyToiPod(self,itdb,filename): """ Copy file to iPod via gpod library. """ track = gpod.itdb_track_new() pl=gpod.itdb_playlist_podcasts(itdb) audiofile = eyeD3.Mp3AudioFile(filename) tag = audiofile.getTag() for func, attrib in (('getArtist','artist'), ('getTitle','title'), ('getBPM','BPM'), ('getPlayCount','playcount'), ('getAlbum','album')): value = getattr(tag,func)() if value: value = value.encode("utf-8") setattr(track,attrib,value) track.skip_when_shuffling=0x01 track.remember_playback_position=0x01 track.mediatype = 0x00000004 track.mark_unplayed = 0x02 #track.flag4=0x01 track.tracklen = audiofile.getPlayTime() * 1000 gpod.itdb_track_add(itdb, track, -1) gpod.itdb_playlist_add_track(pl, track, -1) if gpod.itdb_cp_track_to_ipod(track,filename, None)!= 1: raise Exception('Unable to copy %s to iPod' % filename)
def add(self, track, pos=-1): """Add a track to a database. If pos is set the track will be inserted at that position. By default the track will be added at the end of the database. """ gpod.itdb_track_add(self._itdb, track._track, pos) track.__database = self # so the db doesn't get gc'd
def add_track(self, episode, reporthook=None): self.notify('status', _('Adding %s') % episode.title) tracklist = gpod.sw_get_playlist_tracks(self.podcasts_playlist) podcasturls = [track.podcasturl for track in tracklist] if episode.url in podcasturls: # Mark as played on iPod if played locally (and set podcast flags) self.set_podcast_flags(tracklist[podcasturls.index(episode.url)], episode) return True original_filename = episode.local_filename(create=False) # The file has to exist, if we ought to transfer it, and therefore, # local_filename(create=False) must never return None as filename assert original_filename is not None local_filename = original_filename if util.calculate_size(original_filename) > self.get_free_space(): logger.error('Not enough space on %s, sync aborted...', self.mountpoint) d = {'episode': episode.title, 'mountpoint': self.mountpoint} message = _( 'Error copying %(episode)s: Not enough free space on %(mountpoint)s' ) self.errors.append(message % d) self.cancelled = True return False local_filename = episode.local_filename(create=False) (fn, extension) = os.path.splitext(local_filename) if extension.lower().endswith('ogg'): logger.error('Cannot copy .ogg files to iPod.') return False track = gpod.itdb_track_new() # Add release time to track if episode.published has a valid value if episode.published > 0: try: # libgpod>= 0.5.x uses a new timestamp format track.time_released = gpod.itdb_time_host_to_mac( int(episode.published)) except: # old (pre-0.5.x) libgpod versions expect mactime, so # we're going to manually build a good mactime timestamp here :) # # + 2082844800 for unixtime => mactime (1970 => 1904) track.time_released = int(episode.published + 2082844800) track.title = str(episode.title) track.album = str(episode.channel.title) track.artist = str(episode.channel.title) track.description = str(util.remove_html_tags(episode.description)) track.podcasturl = str(episode.url) track.podcastrss = str(episode.channel.url) track.tracklen = get_track_length(local_filename) track.size = os.path.getsize(local_filename) if episode.file_type() == 'audio': track.filetype = 'mp3' track.mediatype = 0x00000004 elif episode.file_type() == 'video': track.filetype = 'm4v' track.mediatype = 0x00000006 self.set_podcast_flags(track, episode) gpod.itdb_track_add(self.itdb, track, -1) gpod.itdb_playlist_add_track(self.master_playlist, track, -1) gpod.itdb_playlist_add_track(self.podcasts_playlist, track, -1) copied = gpod.itdb_cp_track_to_ipod(track, str(local_filename), None) reporthook(episode.file_size, 1, episode.file_size) # If the file has been converted, delete the temporary file here if local_filename != original_filename: util.delete_file(local_filename) return True
def add_track(self, episode,reporthook=None): self.notify('status', _('Adding %s') % episode.title) tracklist = gpod.sw_get_playlist_tracks(self.podcasts_playlist) podcasturls=[track.podcasturl for track in tracklist] if episode.url in podcasturls: # Mark as played on iPod if played locally (and set podcast flags) self.set_podcast_flags(tracklist[podcasturls.index(episode.url)], episode) return True original_filename = episode.local_filename(create=False) # The file has to exist, if we ought to transfer it, and therefore, # local_filename(create=False) must never return None as filename assert original_filename is not None local_filename = original_filename if util.calculate_size(original_filename) > self.get_free_space(): logger.error('Not enough space on %s, sync aborted...', self.mountpoint) d = {'episode': episode.title, 'mountpoint': self.mountpoint} message =_('Error copying %(episode)s: Not enough free space on %(mountpoint)s') self.errors.append(message % d) self.cancelled = True return False local_filename = episode.local_filename(create=False) (fn, extension) = os.path.splitext(local_filename) if extension.lower().endswith('ogg'): logger.error('Cannot copy .ogg files to iPod.') return False track = gpod.itdb_track_new() # Add release time to track if episode.published has a valid value if episode.published > 0: try: # libgpod>= 0.5.x uses a new timestamp format track.time_released = gpod.itdb_time_host_to_mac(int(episode.published)) except: # old (pre-0.5.x) libgpod versions expect mactime, so # we're going to manually build a good mactime timestamp here :) # # + 2082844800 for unixtime => mactime (1970 => 1904) track.time_released = int(episode.published + 2082844800) track.title = str(episode.title) track.album = str(episode.channel.title) track.artist = str(episode.channel.title) track.description = str(util.remove_html_tags(episode.description)) track.podcasturl = str(episode.url) track.podcastrss = str(episode.channel.url) track.tracklen = get_track_length(local_filename) track.size = os.path.getsize(local_filename) if episode.file_type() == 'audio': track.filetype = 'mp3' track.mediatype = 0x00000004 elif episode.file_type() == 'video': track.filetype = 'm4v' track.mediatype = 0x00000006 self.set_podcast_flags(track, episode) gpod.itdb_track_add(self.itdb, track, -1) gpod.itdb_playlist_add_track(self.master_playlist, track, -1) gpod.itdb_playlist_add_track(self.podcasts_playlist, track, -1) copied = gpod.itdb_cp_track_to_ipod(track, str(local_filename), None) reporthook(episode.file_size, 1, episode.file_size) # If the file has been converted, delete the temporary file here if local_filename != original_filename: util.delete_file(local_filename) return True
def rebuild(mountpoint, ipod_name, dry_run=True): db = gpod.itdb_new() gpod.itdb_set_mountpoint(db, mountpoint) master = gpod.itdb_playlist_new(ipod_name, False) gpod.itdb_playlist_set_mpl(master) gpod.itdb_playlist_add(db, master, -1) mb_albumid_to_artwork = dict() def store_artwork(artwork_data, mb_albumid): import gio from gtk import gdk artwork_in = gio.memory_input_stream_new_from_data(artwork_data) pixbuf = gdk.pixbuf_new_from_stream(artwork_in, None) artwork = gpod.itdb_artwork_new() gpod.itdb_artwork_set_thumbnail_from_pixbuf(artwork, pixbuf, 0, None) mb_albumid_to_artwork[mb_albumid] = artwork return artwork def get_artwork(mb_albumid): return mb_albumid_to_artwork.get(mb_albumid) def action(path): from os import sep, stat from os.path import relpath from mutagen.m4a import M4AInfo relative_path = relpath(path, mountpoint) ipod_path = ":" + relative_path.replace(sep, ":") md_hard, md_easy = get_metadata(path) info = md_easy.info c = is_compilation(md_easy) track = gpod.itdb_track_new() track.title = get_first_utf8(md_easy, "title") track.artist = get_first_utf8(md_easy, "artist") track.album = get_first_utf8(md_easy, "album") track.compilation = c track.tracklen = int(info.length * 1000) track.bitrate = int(info.bitrate) track.samplerate = int(info.sample_rate) track.ipod_path = ipod_path track.size = stat(path).st_size if isinstance(info, M4AInfo): track.filetype = "M4A-file" else: track.filetype = "MP3-file" mb_albumid = get_first_utf8(md_easy, "musicbrainz_albumid", None) if mb_albumid is not None: existing_artwork = get_artwork(mb_albumid) if existing_artwork is not None: _log.debug( "found existing artwork for track %r (%r-%r)", path, get_first(md_easy, "artist"), get_first(md_easy, "album"), ) artwork = existing_artwork else: artwork_data = get_any_artwork(md_hard) if artwork_data is not None: _log.debug("storing artwork for track %r", path) artwork = store_artwork(artwork_data, mb_albumid) else: artwork = None else: artwork = None if artwork is not None: track.artwork = gpod.itdb_artwork_duplicate(artwork) try: track_number = get_first(md_easy, "tracknumber") disc_number = get_first(md_easy, "discnumber", "1") track_n = track_number.split("/") disc_n = disc_number.split("/") track.track_nr = int(track_n[0]) track.cd_nr = int(disc_n[0]) if len(track_n) > 1: track.tracks = int(track_n[1]) if len(disc_n) > 1: track.cds = int(disc_n[1]) except Exception, e: _log.error("%r %r", e, md_easy) gpod.itdb_track_add(db, track, -1) gpod.itdb_playlist_add_track(master, track, -1)
def add_track(self, episode): self.notify('status', _('Adding %s') % episode.title) for track in gpod.sw_get_playlist_tracks(self.podcasts_playlist): if episode.url == track.podcasturl: if track.playcount > 0: gl.history_mark_played(track.podcasturl) # Mark as played on iPod if played locally (and set podcast flags) self.set_podcast_flags(track) return True original_filename=str(episode.local_filename()) local_filename=original_filename # Reserve 10 MiB for iTunesDB writing (to be on the safe side) RESERVED_FOR_ITDB=1024*1024*10 space_for_track=util.get_free_disk_space(self.mountpoint) - RESERVED_FOR_ITDB needed=util.calculate_size(local_filename) if needed > space_for_track: log('Not enough space on %s: %s available, but need at least %s', self.mountpoint, util.format_filesize(space_for_track), util.format_filesize(needed), sender=self) self.errors.append( _('Error copying %s: Not enough free disk space on %s') % (episode.title, self.mountpoint)) self.cancelled=True return False (fn, extension)=os.path.splitext(original_filename) if libconverter.converters.has_converter(extension): log('Converting: %s', original_filename, sender=self) callback_status=lambda percentage: self.notify('sub-progress', int(percentage)) local_filename=libconverter.converters.convert(original_filename, callback=callback_status) if not libtagupdate.update_metadata_on_file(local_filename, title=episode.title, artist=episode.channel.title): log('Could not set metadata on converted file %s', local_filename, sender=self) if local_filename is None: log('Cannot convert %s', original_filename, sender=self) return False else: local_filename=str(local_filename) (fn, extension)=os.path.splitext(local_filename) if extension.lower().endswith('ogg'): log('Cannot copy .ogg files to iPod.', sender=self) return False track=gpod.itdb_track_new() # Add release time to track if pubDate is parseable ipod_date=email.Utils.parsedate(episode.pubDate) if ipod_date is not None: try: # libgpod>= 0.5.x uses a new timestamp format track.time_released=gpod.itdb_time_host_to_mac(int(time.mktime(ipod_date))) except: # old (pre-0.5.x) libgpod versions expect mactime, so # we're going to manually build a good mactime timestamp here :) # # + 2082844800 for unixtime => mactime (1970 => 1904) track.time_released=int(time.mktime(ipod_date) + 2082844800) track.title=str(episode.title) track.album=str(episode.channel.title) track.artist=str(episode.channel.title) track.description=str(episode.description) track.podcasturl=str(episode.url) track.podcastrss=str(episode.channel.url) track.tracklen=get_track_length(local_filename) track.size=os.path.getsize(local_filename) if episode.file_type() == 'audio': track.filetype='mp3' track.mediatype=0x00000004 elif episode.file_type() == 'video': track.filetype='m4v' track.mediatype=0x00000006 self.set_podcast_flags(track) self.set_cover_art(track, local_filename) gpod.itdb_track_add(self.itdb, track, -1) gpod.itdb_playlist_add_track(self.podcasts_playlist, track, -1) gpod.itdb_cp_track_to_ipod( track, local_filename, None) # If the file has been converted, delete the temporary file here if local_filename != original_filename: util.delete_file(local_filename) return True
track.grouping = f.grouping.encode('utf-8') #track.sort_artist = f.artist_sort.encode('utf-8') #track.sort_albumartist = f.albumartist_sort.encode('utf-8') #track.size = f.size track.tracklen = f.length * 1000 if f.disc: track.cd_nr = f.disc if f.disctotal: track.cds = f.disctotal if f.track: track.track_nr = f.track if f.tracktotal: track.tracks = f.tracktotal track.bitrate = f.bitrate track.year = f.year track.visible = 1 gpod.itdb_track_add(db, track, -1) gpod.itdb_playlist_add_track(gpod.itdb_playlist_mpl(db), track, -1) # Write the database if not (new_files or deleted_files): print "Not syncing: No files have been added, modified or removed." else: gpod.itdb_write(db, None)