view gpodutil.py @ 0:206b58198b48

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