diff options
author | Nicholas Piper <nicholas@users.sourceforge.net> | 2006-04-08 20:16:25 +0000 |
---|---|---|
committer | Nicholas Piper <nicholas@users.sourceforge.net> | 2006-04-08 20:16:25 +0000 |
commit | 0655a7316c7cb9da46279758be1a1e87a75c3418 (patch) | |
tree | 3b4b7068cd82e4bcf64640979b2596cf7b645c36 | |
parent | 4c6389fd517150cb1c4814581f766624960843c3 (diff) | |
download | libgpod-0655a7316c7cb9da46279758be1a1e87a75c3418.tar.gz libgpod-0655a7316c7cb9da46279758be1a1e87a75c3418.tar.xz libgpod-0655a7316c7cb9da46279758be1a1e87a75c3418.zip |
Fix adding to podcast playlist, support adding multiple songs at once and support downloading mp3 first.
git-svn-id: https://gtkpod.svn.sf.net/svnroot/gtkpod/libgpod/trunk@1236 f01d2545-417e-4e96-918e-98f8d0dbbcb6
-rwxr-xr-x | bindings/python/examples/add_song.py | 124 |
1 files changed, 80 insertions, 44 deletions
diff --git a/bindings/python/examples/add_song.py b/bindings/python/examples/add_song.py index 0553be9..5817db1 100755 --- a/bindings/python/examples/add_song.py +++ b/bindings/python/examples/add_song.py @@ -27,14 +27,39 @@ import gpod import sys from optparse import OptionParser import eyeD3 +import urlparse, urllib2 +import tempfile +import shutil + +def download(path): + print "Downloading %s" % path + remotefile = urllib2.urlopen(path) + try: + size = int(remotefile.info()['Content-Length']) + except KeyError: + size = None + hndl, tempfilename = tempfile.mkstemp() + temp = open(tempfilename,"wb") + fetched = 0 + while 1: + buf = remotefile.read(1024*10) + if not buf: break + temp.write(buf) + fetched += len(buf) + if size: + sys.stdout.write("%.02f%% of %s Bytes\r" % (100*fetched / float(size), size)) + else: + sys.stdout.write(" Fetched %d bytes\r" % fetched) + sys.stdout.flush() + temp.close() + remotefile.close() + return tempfilename + parser = OptionParser() parser.add_option("-m", "--mountpoint", dest="mountpoint", default="/mnt/ipod", help="use iPod at MOUNTPOINT", metavar="MOUNTPOINT") -parser.add_option("-a", "--add", - dest="filetoadd",metavar="FILE", - help="add mp3 FILE") parser.add_option("-p", "--podcast", dest="ispodcast", action="store_true", @@ -42,11 +67,8 @@ parser.add_option("-p", "--podcast", help="add to podcast playlist") (options, args) = parser.parse_args() -if not options.filetoadd: - parser.error("Require -a argument to specify mp3 to add.") - -if not eyeD3.isMp3File(options.filetoadd): - parser.error("%s it not recognised as an mp3 file." % options.filetoadd) +if len(args) == 0: + parser.error("Requires an mp3 to add.") itdb = gpod.itdb_parse(options.mountpoint, None) if not itdb: @@ -54,42 +76,56 @@ if not itdb: sys.exit(2) itdb.mountpoint = options.mountpoint -track = gpod.itdb_track_new() -audiofile = eyeD3.Mp3AudioFile(options.filetoadd) -tag = audiofile.getTag() - -track.artist= str(tag.getArtist()) -track.album = str(tag.getAlbum()) -track.title = str(tag.getTitle()) -track.filetype = 'mp3' -track.tracklen = audiofile.getPlayTime() * 1000 # important to add!, iPod uses ms. - -if options.ispodcast: - print track.flag1 - print type(track.flag1) - track.flag1 = 0x02 # unknown - track.flag2 = 0x01 # skip when shuffling - track.flag3 = 0x01 # remember playback position - track.flag4 = 0x01 # Show Title/Album on the 'Now Playing' page - playlists = [gpod.itdb_playlist_mpl(itdb)] -else: - track.flag1 = 0x02 # unknown - track.flag2 = 0x00 # do not skip when shuffling - track.flag3 = 0x00 # do not remember playback position - track.flag4 = 0x00 # Show Title/Album/Artist on the 'New Playing' page - playlists = [itdb_playlist_podcasts(itdb)] - -print "Adding %s (Title: %s)" % (options.filetoadd, track.title) - -gpod.itdb_track_add(itdb, track, -1) - -for playlist in playlists: - gpod.itdb_playlist_add_track(playlist, track, -1) - -if gpod.itdb_cp_track_to_ipod(track, options.filetoadd, None) == 1: - print "Copied to %s" % gpod.itdb_filename_on_ipod(track) -else: - print "Copy failed" +for path in args: + deleteWhenDone = [] + transport = urlparse.urlsplit(path)[0] + if transport: + path = download(path) + deleteWhenDone.append(path) + + if not eyeD3.isMp3File(path): + sys.stderr.write("%s it not recognised as an mp3 file by eyeD3 tag library.\n" % path) + if transport: + sys.stderr.write("Leaving for inspection.\n") + deleteWhenDone.pop() + continue + + track = gpod.itdb_track_new() + audiofile = eyeD3.Mp3AudioFile(path) + tag = audiofile.getTag() + + track.artist= str(tag.getArtist()) + track.album = str(tag.getAlbum()) + track.title = str(tag.getTitle()) + track.filetype = 'mp3' + track.tracklen = audiofile.getPlayTime() * 1000 # important to add!, iPod uses ms. + + if options.ispodcast: + track.flag1 = 0x02 # unknown + track.flag2 = 0x01 # skip when shuffling + track.flag3 = 0x01 # remember playback position + track.flag4 = 0x01 # Show Title/Album on the 'Now Playing' page + playlists = [gpod.itdb_playlist_podcasts(itdb)] + print "Adding Podcast %s (Title: %s)" % (path,track.title) + else: + track.flag1 = 0x02 # unknown + track.flag2 = 0x00 # do not skip when shuffling + track.flag3 = 0x00 # do not remember playback position + track.flag4 = 0x00 # Show Title/Album/Artist on the 'New Playing' page + playlists = [gpod.itdb_playlist_mpl(itdb)] + print "Adding Song %s (Title: %s)" % (path,track.title) + + gpod.itdb_track_add(itdb, track, -1) + + for playlist in playlists: + gpod.itdb_playlist_add_track(playlist, track, -1) + + if gpod.itdb_cp_track_to_ipod(track, path, None) == 1: + print "Copied to %s" % gpod.itdb_filename_on_ipod(track) + else: + print "Copy failed" + + [os.unlink(f) for f in deleteWhenDone] gpod.itdb_write(itdb, None) print "Saved db" |