annotate gpodutil.py @ 0:206b58198b48

add gpodutil.py
author paulo@twcdns.fastsearch.net
date Wed, 09 Feb 2011 23:29:12 -0800
parents
children
rev   line source
paulo@0 1 import os
paulo@0 2 import sys
paulo@0 3 import shutil
paulo@0 4 import gpod
paulo@0 5
paulo@0 6
paulo@0 7 def exportTracks(tracks):
paulo@0 8 for t in tracks:
paulo@0 9 artist = t["artist"]
paulo@0 10 title = t["title"]
paulo@0 11 src_fn = t.ipod_filename()
paulo@0 12 (root, ext) = os.path.splitext(src_fn)
paulo@0 13 dst_fn = artist + " - " + title + ext
paulo@0 14
paulo@0 15 # check if dst exists
paulo@0 16 i = 0
paulo@0 17 while os.path.exists(dst_fn):
paulo@0 18 dst_fn = artist + " - " + title + '.' + str(i) + ext
paulo@0 19 i += 1
paulo@0 20
paulo@0 21 shutil.copy(src_fn, dst_fn)
paulo@0 22
paulo@0 23
paulo@0 24 def _trackName(track):
paulo@0 25 return str(track["artist"]) + " - " + str(track["title"])
paulo@0 26
paulo@0 27
paulo@0 28 def importTracks(db, pl, filenames):
paulo@0 29 # set of tracks already in database
paulo@0 30 dbTracks = set()
paulo@0 31 for i in db:
paulo@0 32 dbTracks.add(_trackName(i))
paulo@0 33
paulo@0 34 for fn in filenames:
paulo@0 35 (root, ext) = os.path.splitext(os.path.basename(fn))
paulo@0 36 name = root.split('-', 1)
paulo@0 37 if len(name) < 2:
paulo@0 38 artist = None
paulo@0 39 title = name[0].strip()
paulo@0 40 else:
paulo@0 41 artist = name[0].strip()
paulo@0 42 title = name[1].strip()
paulo@0 43 t = gpod.Track(fn)
paulo@0 44 t["artist"] = artist
paulo@0 45 t["title"] = title
paulo@0 46
paulo@0 47 print "artist: %s, title: %s" % (artist, title)
paulo@0 48
paulo@0 49 # only add if track is not already in database
paulo@0 50 trackName = _trackName(t)
paulo@0 51 if trackName not in dbTracks:
paulo@0 52 dbTracks.add(trackName)
paulo@0 53 db.add(t)
paulo@0 54 pl.add(t)
paulo@0 55
paulo@0 56
paulo@0 57 def safeClose(db):
paulo@0 58 db.copy_delayed_files()
paulo@0 59 db.close()
paulo@0 60
paulo@0 61
paulo@0 62 if __name__ == "__main__":
paulo@0 63
paulo@0 64 ipodMountPoint = "/mnt/sda1"
paulo@0 65
paulo@0 66 db = gpod.Database(ipodMountPoint)
paulo@0 67 pl0 = db.get_playlists()[0]
paulo@0 68
paulo@0 69 fnargs = sys.argv[1:]
paulo@0 70
paulo@0 71 #pl_shoutcast = db.get_playlists()[-1]
paulo@0 72
paulo@0 73 #importTracks(db, pl_shoutcast, fnargs)
paulo@0 74 #safeClose(db)