From 22872c6b05203d5921276269f9eb00fcc474c16d Mon Sep 17 00:00:00 2001 From: Nicholas Piper Date: Fri, 7 Apr 2006 14:51:40 +0000 Subject: Clean up this directory a little, and improve coverart fetching. git-svn-id: https://gtkpod.svn.sf.net/svnroot/gtkpod/libgpod/trunk@1228 f01d2545-417e-4e96-918e-98f8d0dbbcb6 --- bindings/python/examples/Makefile.am | 2 + bindings/python/examples/add_song.py | 75 +++++++++++++++++ bindings/python/examples/coverart_fetch.py | 56 +++++++++---- bindings/python/examples/play.py | 90 --------------------- .../examples/tag-genre-from-audioscrobber.py | 79 ------------------ .../examples/tag_genre_from_audioscrobber.py | 79 ++++++++++++++++++ bindings/python/examples/toy_around.py | 93 ++++++++++++++++++++++ 7 files changed, 289 insertions(+), 185 deletions(-) create mode 100644 bindings/python/examples/Makefile.am create mode 100755 bindings/python/examples/add_song.py delete mode 100755 bindings/python/examples/play.py delete mode 100755 bindings/python/examples/tag-genre-from-audioscrobber.py create mode 100755 bindings/python/examples/tag_genre_from_audioscrobber.py create mode 100755 bindings/python/examples/toy_around.py (limited to 'bindings/python') diff --git a/bindings/python/examples/Makefile.am b/bindings/python/examples/Makefile.am new file mode 100644 index 0000000..1c3ba52 --- /dev/null +++ b/bindings/python/examples/Makefile.am @@ -0,0 +1,2 @@ +EXTRA_DIST = coverart_fetch.py toy_around.py tag-genre-from-audioscrobber.py add_song.py + diff --git a/bindings/python/examples/add_song.py b/bindings/python/examples/add_song.py new file mode 100755 index 0000000..4203284 --- /dev/null +++ b/bindings/python/examples/add_song.py @@ -0,0 +1,75 @@ +#!/usr/bin/python + +## Copyright (C) 2006 Nick Piper +## Part of the gtkpod project. + +## URL: http://www.gtkpod.org/ +## URL: http://gtkpod.sourceforge.net/ + +## The code contained in this file is free software; you can redistribute +## it and/or modify it under the terms of the GNU Lesser General Public +## License as published by the Free Software Foundation; either version +## 2.1 of the License, or (at your option) any later version. + +## This file is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## Lesser General Public License for more details. + +## You should have received a copy of the GNU Lesser General Public +## License along with this code; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +# this file is just a little example to see how you could add music + +import os, os.path +import gpod +import sys +from optparse import OptionParser +import eyeD3 + +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") +(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) + +itdb = gpod.itdb_parse(options.mountpoint, None) +if not itdb: + print "Failed to read iPod at %s" % options.mountpoint + 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. + +print "Adding %s (Title: %s)" % (options.filetoadd, track.title) + +gpod.itdb_track_add(itdb, track, -1) +master = gpod.sw_get_playlists(itdb)[0] +gpod.itdb_playlist_add_track(master, 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" + +gpod.itdb_write(itdb, None) +print "Saved db" + + diff --git a/bindings/python/examples/coverart_fetch.py b/bindings/python/examples/coverart_fetch.py index 0df6451..3db279f 100755 --- a/bindings/python/examples/coverart_fetch.py +++ b/bindings/python/examples/coverart_fetch.py @@ -36,15 +36,33 @@ if not itdb: print "Failed to read %s" % dbname sys.exit(2) -# set your key here... -amazon.setLicense('') +# set your key here, or see amazon.py for a list of other places to +# store it. +amazon.setLicense('THISISNOTREALLYMYKEY') + +images = {} for track in gpod.sw_get_tracks(itdb): - output = ("/tmp/%s %s.jpg" % (track.artist, track.album)).replace(' ','_') - if not os.path.exists(output): - print "Searching for %s %s" % (track.artist, track.album) + print track.artist, track.album, track.title, " :", + + if track.artwork.artwork_size: + print "Already has artwork, skipping." + continue + + if not (track.artist and track.album): + print "Need an artist AND album name, skipping." + continue + + # avoid fetching again if we already had a suitable image + if not images.has_key((track.album,track.artist)): + query = "%s + %s" % (track.artist, track.album) + # nasty hacks to get better hits. Is there a library out there + # for this? + query = query.replace("Disk 2","") + query = query.replace("Disk 1","") + print "Searching for %s: " % query, try: - albums = amazon.searchByKeyword("%s %s" % (track.artist, track.album), + albums = amazon.searchByKeyword(query, type="lite",product_line="music") except amazon.AmazonError, e: print e @@ -54,19 +72,25 @@ for track in gpod.sw_get_tracks(itdb): continue album = albums[0] + hdle, filename = tempfile.mkstemp() i = urllib.urlopen(album.ImageUrlLarge) - o = open(output, "wb") - o.write(i.read()) - o.close() - img = Image.open(output) + open(filename,"w").write(i.read()) + img = Image.open(filename) if not (img.size[0] > 10 or img.size[1] > 10): - os.unlink(output) + os.unlink(filename) else: - print "Fetched image!" - - if os.path.exists(output): - if gpod.itdb_track_set_thumbnail(track,output) != 0: - print "Failed to save image thumbnail" + print "Fetched image for %s, %s" % (track.album,track.artist) + images[(track.album,track.artist)] = filename + + try: + r = gpod.itdb_track_set_thumbnails(track,images[(track.album,track.artist)]) + if r != 1: + print "Failed to save image thumbnail to ipod." + else: + print "Added thumbnails for %s, %s" % (track.album,track.artist) + except KeyError: + print "No image available for %s, %s" % (track.album,track.artist) + gpod.itdb_write(itdb, None) print "Saved db" diff --git a/bindings/python/examples/play.py b/bindings/python/examples/play.py deleted file mode 100755 index 7356de1..0000000 --- a/bindings/python/examples/play.py +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/python - -## Copyright (C) 2005 Nick Piper -## Part of the gtkpod project. - -## URL: http://www.gtkpod.org/ -## URL: http://gtkpod.sourceforge.net/ - -## The code contained in this file is free software; you can redistribute -## it and/or modify it under the terms of the GNU Lesser General Public -## License as published by the Free Software Foundation; either version -## 2.1 of the License, or (at your option) any later version. - -## This file is distributed in the hope that it will be useful, -## but WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -## Lesser General Public License for more details. - -## You should have received a copy of the GNU Lesser General Public -## License along with this code; if not, write to the Free Software -## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -## $Id$ - - -# this file is just a little example I use for testing, it doesn't -# play the music, but plays with the database ;-) - -import os, os.path -import gpod -import sys - -ipod_mount = '/mnt/ipod' - -remove_track = "The Dancer" - -#dbname = os.path.join(os.environ['HOME'],".gtkpod/iTunesDB") -#dbname = os.path.join(os.environ['HOME'],".gtkpod/local_0.itdb") -dbname = os.path.join(ipod_mount,"iPod_Control/iTunes/iTunesDB") - -#itdb = gpod.itdb_parse_file(dbname, None) -# the image related functions require us to use parse and give it the -# mount point; and they won't work without an actual ipod. -itdb = gpod.itdb_parse(ipod_mount, None) -if not itdb: - print "Failed to read %s" % dbname - sys.exit(2) -itdb.mountpoint = ipod_mount - -if False: - for playlist in gpod.sw_get_playlists(itdb): - print playlist.name - print type(playlist.name) - print gpod.itdb_playlist_tracks_number(playlist) - for track in gpod.sw_get_playlist_tracks(playlist): - print track.title - -for track in gpod.sw_get_tracks(itdb): - lists = [] - for playlist in gpod.sw_get_playlists(itdb): - if gpod.itdb_playlist_contains_track(playlist, track): - lists.append(playlist) - - if track.artist == "Placebo": - print u"%-25s %-20s %-20s %-30s %s" % (track.title, - track.album, - track.artist, - gpod.itdb_filename_on_ipod(track), - repr(u",".join([l.name for l in lists]))) - - if gpod.itdb_track_set_thumbnail(track,"/tmp/placebo.jpg") != 0: - print "Failed to save image thumbnail" - print track.orig_image_filename - - if track.title == remove_track: - print "Removing track.." - print "..disk" - os.unlink(gpod.itdb_filename_on_ipod(track)) - for l in lists: - print u"..playlist %s" % l.name - gpod.itdb_playlist_remove_track(l, track) - print "..db" - gpod.itdb_track_unlink(track) - print "Track removed." - -gpod.itdb_write(itdb, None) -print "Saved db" - - - diff --git a/bindings/python/examples/tag-genre-from-audioscrobber.py b/bindings/python/examples/tag-genre-from-audioscrobber.py deleted file mode 100755 index 205779e..0000000 --- a/bindings/python/examples/tag-genre-from-audioscrobber.py +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/python - -## Copyright (C) 2005 Nick Piper -## Part of the gtkpod project. - -## URL: http://www.gtkpod.org/ -## URL: http://gtkpod.sourceforge.net/ - -## The code contained in this file is free software; you can redistribute -## it and/or modify it under the terms of the GNU Lesser General Public -## License as published by the Free Software Foundation; either version -## 2.1 of the License, or (at your option) any later version. - -## This file is distributed in the hope that it will be useful, -## but WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -## Lesser General Public License for more details. - -## You should have received a copy of the GNU Lesser General Public -## License along with this code; if not, write to the Free Software -## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -## $Id$ - - -import os, os.path -import gpod -import sys -from xml import xpath -from xml.dom import minidom -from xml.parsers.expat import ExpatError -import urllib2, urllib - -TRUST_LIMIT = 10 -dbname = os.path.join(os.environ['HOME'],".gtkpod/local_0.itdb") - - -itdb = gpod.itdb_parse_file(dbname, None) -if not itdb: - print "Failed to read %s" % dbname - sys.exit(2) - -cache={} -for track in gpod.sw_get_tracks(itdb): - if track.artist is None: - continue - - key = track.artist.upper() - if not cache.has_key(key): - url = "http://ws.audioscrobbler.com/1.0/artist/%s/toptags.xml" % urllib.quote(track.artist) - - try: - reply = urllib2.urlopen(url).read() - xmlreply = minidom.parseString(reply) - attlist = xpath.Evaluate("//toptags/tag[1]/@name",xmlreply) - count = xpath.Evaluate("//toptags/tag[1]/@count",xmlreply) - if attlist and count and int(count[0].value) > TRUST_LIMIT: - cache[key] = str(attlist[0].value.title()) # no unicode please :-) - except urllib2.HTTPError, e: - pass - #print "Urllib failed.", e - except ExpatError, e: - print "Failed to parse,", e - print reply - - if cache.has_key(key): - track.genre = cache[key] - print "%-25s %-20s %-20s --> %s" % (track.title, - track.album, - track.artist, - track.genre) - else: - print "%-25s %-20s %-20s === %s" % (track.title, - track.album, - track.artist, - track.genre) - - -gpod.itdb_write_file(itdb, dbname, None) diff --git a/bindings/python/examples/tag_genre_from_audioscrobber.py b/bindings/python/examples/tag_genre_from_audioscrobber.py new file mode 100755 index 0000000..205779e --- /dev/null +++ b/bindings/python/examples/tag_genre_from_audioscrobber.py @@ -0,0 +1,79 @@ +#!/usr/bin/python + +## Copyright (C) 2005 Nick Piper +## Part of the gtkpod project. + +## URL: http://www.gtkpod.org/ +## URL: http://gtkpod.sourceforge.net/ + +## The code contained in this file is free software; you can redistribute +## it and/or modify it under the terms of the GNU Lesser General Public +## License as published by the Free Software Foundation; either version +## 2.1 of the License, or (at your option) any later version. + +## This file is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## Lesser General Public License for more details. + +## You should have received a copy of the GNU Lesser General Public +## License along with this code; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +## $Id$ + + +import os, os.path +import gpod +import sys +from xml import xpath +from xml.dom import minidom +from xml.parsers.expat import ExpatError +import urllib2, urllib + +TRUST_LIMIT = 10 +dbname = os.path.join(os.environ['HOME'],".gtkpod/local_0.itdb") + + +itdb = gpod.itdb_parse_file(dbname, None) +if not itdb: + print "Failed to read %s" % dbname + sys.exit(2) + +cache={} +for track in gpod.sw_get_tracks(itdb): + if track.artist is None: + continue + + key = track.artist.upper() + if not cache.has_key(key): + url = "http://ws.audioscrobbler.com/1.0/artist/%s/toptags.xml" % urllib.quote(track.artist) + + try: + reply = urllib2.urlopen(url).read() + xmlreply = minidom.parseString(reply) + attlist = xpath.Evaluate("//toptags/tag[1]/@name",xmlreply) + count = xpath.Evaluate("//toptags/tag[1]/@count",xmlreply) + if attlist and count and int(count[0].value) > TRUST_LIMIT: + cache[key] = str(attlist[0].value.title()) # no unicode please :-) + except urllib2.HTTPError, e: + pass + #print "Urllib failed.", e + except ExpatError, e: + print "Failed to parse,", e + print reply + + if cache.has_key(key): + track.genre = cache[key] + print "%-25s %-20s %-20s --> %s" % (track.title, + track.album, + track.artist, + track.genre) + else: + print "%-25s %-20s %-20s === %s" % (track.title, + track.album, + track.artist, + track.genre) + + +gpod.itdb_write_file(itdb, dbname, None) diff --git a/bindings/python/examples/toy_around.py b/bindings/python/examples/toy_around.py new file mode 100755 index 0000000..202541c --- /dev/null +++ b/bindings/python/examples/toy_around.py @@ -0,0 +1,93 @@ +#!/usr/bin/python + +## Copyright (C) 2005 Nick Piper +## Part of the gtkpod project. + +## URL: http://www.gtkpod.org/ +## URL: http://gtkpod.sourceforge.net/ + +## The code contained in this file is free software; you can redistribute +## it and/or modify it under the terms of the GNU Lesser General Public +## License as published by the Free Software Foundation; either version +## 2.1 of the License, or (at your option) any later version. + +## This file is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## Lesser General Public License for more details. + +## You should have received a copy of the GNU Lesser General Public +## License along with this code; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +## $Id$ + + +# this file is just a little example I use for testing, it doesn't +# play the music, but plays with the database ;-) + +import os, os.path +import gpod +import sys + +ipod_mount = '/mnt/ipod' + +remove_track = "The Dancer" + +#dbname = os.path.join(os.environ['HOME'],".gtkpod/iTunesDB") +#dbname = os.path.join(os.environ['HOME'],".gtkpod/local_0.itdb") +dbname = os.path.join(ipod_mount,"iPod_Control/iTunes/iTunesDB") + +#itdb = gpod.itdb_parse_file(dbname, None) +# the image related functions require us to use parse and give it the +# mount point; and they won't work without an actual ipod. +itdb = gpod.itdb_parse(ipod_mount, None) +if not itdb: + print "Failed to read %s" % dbname + sys.exit(2) +itdb.mountpoint = ipod_mount + +if True: + for playlist in gpod.sw_get_playlists(itdb): + print playlist.name + print type(playlist.name) + print gpod.itdb_playlist_tracks_number(playlist) + for track in gpod.sw_get_playlist_tracks(playlist): + print track.title + +for track in gpod.sw_get_tracks(itdb): + lists = [] + for playlist in gpod.sw_get_playlists(itdb): + if gpod.itdb_playlist_contains_track(playlist, track): + lists.append(playlist) + + print track.artist + print track.tracklen + print track.size + if track.artist == "Placebo": + print u"%-25s %-20s %-20s %-30s %s" % (track.title, + track.album, + track.artist, + gpod.itdb_filename_on_ipod(track), + repr(u",".join([l.name for l in lists]))) + + if gpod.itdb_track_set_thumbnail(track,"/tmp/placebo.jpg") != 0: + print "Failed to save image thumbnail" + print track.orig_image_filename + + if track.title == remove_track: + print "Removing track.." + print "..disk" + os.unlink(gpod.itdb_filename_on_ipod(track)) + for l in lists: + print u"..playlist %s" % l.name + gpod.itdb_playlist_remove_track(l, track) + print "..db" + gpod.itdb_track_unlink(track) + print "Track removed." + +gpod.itdb_write(itdb, None) +print "Saved db" + + + -- cgit