diff gpodutil.py @ 0:206b58198b48

add gpodutil.py
author paulo@twcdns.fastsearch.net
date Wed, 09 Feb 2011 23:29:12 -0800
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gpodutil.py	Wed Feb 09 23:29:12 2011 -0800
     1.3 @@ -0,0 +1,74 @@
     1.4 +import os
     1.5 +import sys
     1.6 +import shutil
     1.7 +import gpod
     1.8 +
     1.9 +
    1.10 +def exportTracks(tracks):
    1.11 +	for t in tracks:
    1.12 +		artist = t["artist"]
    1.13 +		title = t["title"]
    1.14 +		src_fn = t.ipod_filename()
    1.15 +		(root, ext) = os.path.splitext(src_fn)
    1.16 +		dst_fn = artist + " - " + title + ext
    1.17 +
    1.18 +		# check if dst exists
    1.19 +		i = 0
    1.20 +		while os.path.exists(dst_fn):
    1.21 +			dst_fn = artist + " - " + title + '.' + str(i) + ext
    1.22 +			i += 1
    1.23 +
    1.24 +		shutil.copy(src_fn, dst_fn)
    1.25 +
    1.26 +
    1.27 +def _trackName(track):
    1.28 +	return str(track["artist"]) + " - " + str(track["title"])
    1.29 +
    1.30 +
    1.31 +def importTracks(db, pl, filenames):
    1.32 +	# set of tracks already in database
    1.33 +	dbTracks = set()
    1.34 +	for i in db:
    1.35 +		dbTracks.add(_trackName(i))
    1.36 +
    1.37 +	for fn in filenames:
    1.38 +		(root, ext) = os.path.splitext(os.path.basename(fn))
    1.39 +		name = root.split('-', 1)
    1.40 +		if len(name) < 2:
    1.41 +			artist = None
    1.42 +			title = name[0].strip()
    1.43 +		else:
    1.44 +			artist = name[0].strip()
    1.45 +			title = name[1].strip()
    1.46 +		t = gpod.Track(fn)
    1.47 +		t["artist"] = artist
    1.48 +		t["title"] = title
    1.49 +
    1.50 +		print "artist: %s, title: %s" % (artist, title)
    1.51 +
    1.52 +		# only add if track is not already in database
    1.53 +		trackName = _trackName(t)
    1.54 +		if trackName not in dbTracks:
    1.55 +			dbTracks.add(trackName)
    1.56 +			db.add(t)
    1.57 +			pl.add(t)
    1.58 +
    1.59 +
    1.60 +def safeClose(db):
    1.61 +	db.copy_delayed_files()
    1.62 +	db.close()
    1.63 +
    1.64 +
    1.65 +if __name__ == "__main__":
    1.66 +
    1.67 +	ipodMountPoint = "/mnt/sda1"
    1.68 +	
    1.69 +	db = gpod.Database(ipodMountPoint)
    1.70 +	pl0 = db.get_playlists()[0]
    1.71 +
    1.72 +	fnargs = sys.argv[1:]
    1.73 +
    1.74 +	#pl_shoutcast = db.get_playlists()[-1]
    1.75 +
    1.76 +	#importTracks(db, pl_shoutcast, fnargs)
    1.77 +	#safeClose(db)