All pastes #633899 Raw Edit

ipod

public python v1 · immutable
#633899 ·published 2007-07-26 00:19 UTC
rendered paste body
#!/usr/bin/python# ipod.py# a little script for viewing and modifying some of the# contents of an ipod.## requires: python & libipod## pls email me bugs and not bear porn# by: ktf <esthetiq@gmail.com># last change: 7/25/2007from ipodlib import *import getopt, sys, os, os.pathimport stringhdr = "\033[1;36mipod\033[0;0m"verbose = 0paths = IPod.Discover()def usage():	print "usage: %s [-h?vraqplim] [-f file]" % sys.argv[0]	print "-h\tshow this help message screen"	print "-?\tsame function as the previous flag"	print "-v\tverbosely show informational messages"	print "-r\tremove tracks from ipod (by trackid ONLY (see '-l'))"	print "-a\tadd tracks to ipod"	print "-q\tremove playlist from ipod"	print "-p\tadd playlist to ipod"	print "-l\tlist tracks on ipod"	print "-i\tlist playlists on ipod"	print "-m\tsignifies the argument passed to the '-f' flag"	print "  \tis a filename containing a list of multiple strings"	print "-f str\ta string being either a track id, name of a file,"	print "      \tor the name of a track/playlist to be added/removed"	printdef about():	print "ipod.py by ktf <esthetiq@gmail.com>"	print "last change: 7/25/2007"	printdef ipod_add_tracks(lm, str):	if len(paths):		path = paths[0]		ipod = IPod(path)		args = str		arry = []		if lm:			for item in str:				temp = string.split(item, '\n')				arry.append(temp[0])			args = arry		print "%s: opening ipod @ %s" % (hdr, path)		print "%s: adding track(s) to ipod db" % hdr		for arg in args:			if os.path.exists(arg):				(root, ext) = os.path.splitext(arg)				if ext in ['.mp3', '.m4a', '.wav']:					track = IPodTrack(ipod, arg)					if track:						print "%s: trackid %d: %s" % (hdr, track.GetAttribute(IPOD_TRACK_ID), track.GetText(IPOD_TITLE))					else:						print "%s: error adding track from file %s" % (hdr, arg)				else:					print "%s: skipping file" % hdr		ipod.Flush()	else:		print "%s: no ipods connected... is it mounted?" % hdr	print "%s: finished" % hdrdef ipod_add_pls(lm, file):	if len(paths):		path = paths[0]		ipod = IPod(path)		args = file		arry = []		if lm:			for item in file:				temp = string.split(item, '\n')				arry.append(temp[0])			args = arry		print "%s: opening ipod @ %s" % (hdr, path)		print "%s: adding playlist to ipod db" % hdr		for arg in args:			pls = IPodPlaylist(ipod)			if pls:				pls.SetText(IPOD_TITLE, arg)				print "%s: playlistid %d: %s" % hdr, (pls.GetAttribute(IPOD_PLAYLIST_PLAYLIST_ID_LO), pls.GetText(IPOD_TITLE))		ipod.Flush()	else:		print "%s: no ipods connected... is it mounted?" % hdrdef ipod_rm_pls(lm, name):	if len(paths):		path = paths[0]		ipod = IPod(path)		args = name		arry = []		if lm:			for item in name:				temp = string.split(item, '\n')				arry.append(temp[0])			args = arry		print "%s: opening ipod @ %s" % (hdr, path)		print "%s: removing playlist from ipod db" % hdr		for arg in args:			for i in xrange(ipod.PlaylistCount()):				pls = ipod.PlaylistByIndex(i)				if pls:					if pls.GetText(IPOD_TITLE) == arg:						pls.Remove()						print "%s: removed playlist %s" % (hdr, arg)						break		ipod.Flush()	else:		print "%s: no ipods connected... is it mounted?" % hdrdef ipod_rm_tracks(lm, name):	if len(paths):		path = paths[0]		ipod = IPod(path)		args = name		arry = []		if lm:			for item in name:				temp = string.split(item, '\n')				arry.append(temp[0])			args = arry		print "%s: opening ipod @ %s" % (hdr, path)		print "%s: removing track from ipod db" % hdr		for arg in args:			for i in xrange(ipod.TrackCount()):				track = ipod.TrackByIndex(i)				if track:					if track.GetAttribute(IPOD_TRACK_ID) == int(name):						track.Remove()						print "%s: removed trackid %d" % (hdr, int(name))						break		ipod.Flush()	else:		print "%s: no ipods connected... is it mounted?" % hdrdef ipod_ls_tracks():	for path in paths:		ipod = IPod(path)		(total, free) = ipod.DiskUsage()		print "%s: found ipod @ %s (%d tracks, %d playlists, %dKB total, %dKB free)" % (hdr, path, ipod.TrackCount(), ipod.PlaylistCount(), total, free)		for tindex in xrange(ipod.TrackCount()):			track = ipod.TrackByIndex(tindex)			trackid = track.GetAttribute(IPOD_TRACK_ID)			print "%s: index %d trackid %d: %s %s/%s" % (hdr, tindex, trackid, track.GetText(IPOD_TITLE), track.GetText(IPOD_ARTIST), track.GetText(IPOD_ALBUM))		for pindex in xrange(ipod.PlaylistCount()):			pls = ipod.PlaylistByIndex(pindex)			print "%s: playlist %d: %s" % (hdr, pindex, pls.GetText(IPOD_TITLE))			for titemindex in xrange(pls.TrackItemCount()):				titem = pls.TrackItemByIndex(titemindex)				trackid = titem.GetAttribute(IPOD_TRACK_ITEM_TRACK_ID)				track = ipod.TrackByTrackID(trackid)				print "%s: index %d trackid %d: %s" % (hdr, titemindex, trackid, track.GetText(IPOD_TITLE))def main():	about()	action = 0	listmode = 0	file = ""	dir = ""	if len(sys.argv) < 2:		usage()	try:		opts, args = getopt.getopt(sys.argv[1:], "?hvf:raqplim")	except getopt.GetoptError:		usage()		sys.exit(1)	for a, b in opts:		if a in ("-?", "-h"):			usage()		if a == "-v":			verbose = 1		if a == "-f":			file = b		if a == "-r":			action = 1		if a == "-a":			action = 2		if a == "-q":			action = 3		if a == "-p":			action = 4		if a == "-l":			action = 5		if a == "-i":			action = 6		if a == "-m":			listmode = 1		if listmode:		if verbose:			print "%s: using listmode, assuming '-f' value is filename" % hdr		f = open(file, 'r')	if action is 1:		if listmode:			ipod_rm_tracks(1, f.readlines())			f.close()		else:			ipod_rm_tracks(0, file)	if action is 2:		if listmode:			ipod_add_tracks(1, f.readlines())			f.close()		else:			ipod_add_tracks(0, file)	if action is 3:		if listmode:			ipod_rm_pls(1, f.readlines())			f.close()		else:			ipod_rm_pls(0, file)	if action is 4:		if listmode:			ipod_add_pls(1, f.readlines())			f.close()		else:			ipod_add_pls(0, file)	if action is 5:		ipod_ls_tracks()	if action is 6:		print "%s: option currently unavaliable" % hdr		# ipod_ls_pls()if __name__ == "__main__":	main()