diff options
-rw-r--r-- | bindings/python/examples/Makefile.am | 3 | ||||
-rwxr-xr-x | bindings/python/examples/play_with_smart_playlists.py | 55 | ||||
-rw-r--r-- | bindings/python/gpod.i | 68 |
3 files changed, 113 insertions, 13 deletions
diff --git a/bindings/python/examples/Makefile.am b/bindings/python/examples/Makefile.am index 7130527..5e6100f 100644 --- a/bindings/python/examples/Makefile.am +++ b/bindings/python/examples/Makefile.am @@ -1,3 +1,4 @@ EXTRA_DIST = coverart_fetch.py toy_around.py \ tag_genre_from_audioscrobber.py add_song.py \ - playwith_ipod_api.py create_mp3_tags_from_itdb.py + playwith_ipod_api.py create_mp3_tags_from_itdb.py \ + play_with_smart_playlists.py diff --git a/bindings/python/examples/play_with_smart_playlists.py b/bindings/python/examples/play_with_smart_playlists.py new file mode 100755 index 0000000..3c23ef1 --- /dev/null +++ b/bindings/python/examples/play_with_smart_playlists.py @@ -0,0 +1,55 @@ +#!/usr/bin/python + +## Copyright (C) 2006 Nick Piper <nick-gtkpod at nickpiper co uk> +## 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 + + +import gpod +import time +from optparse import OptionParser + +parser = OptionParser() +parser.add_option("-m", "--mountpoint", dest="mountpoint", + default="/mnt/ipod", + help="use iPod at MOUNTPOINT", metavar="MOUNTPOINT") +(options, args) = parser.parse_args() + + +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 + +for playlist in gpod.sw_get_playlists(itdb): + if playlist.is_spl: + n = gpod.sw_get_list_len(playlist.splrules.rules) + splrules = [gpod.sw_get_rule(playlist.splrules.rules,i) for i in xrange(n)] + print "Playlist: %s" % playlist.name + for i in xrange(gpod.sw_get_list_len(playlist.splrules.rules)): + rule = gpod.sw_get_rule(playlist.splrules.rules, i) + print "| field: %4d action: %4d |" % (rule.field,rule.action) + print "| string: %25s |" % rule.string + print "| fromvalue: %4d fromdate: %4d |" % (rule.fromvalue,rule.fromdate) + print "| fromunits: %4d |" % rule.fromunits + print "Contains:" + for track in gpod.sw_get_playlist_tracks(playlist): + print track.title, track.artist, time.strftime("%c", + time.localtime(track.time_added - 2082844800)) diff --git a/bindings/python/gpod.i b/bindings/python/gpod.i index 7103ecf..c6f0bbf 100644 --- a/bindings/python/gpod.i +++ b/bindings/python/gpod.i @@ -59,6 +59,16 @@ PyObject* sw_get_track(GList *list, gint index) { return SWIG_NewPointerObj((void*)(position->data), SWIGTYPE_p__Itdb_Track, 0); } +PyObject* sw_get_rule(GList *list, gint index) { + GList *position; + if ( (index >= g_list_length(list)) || index < 0 ) { + PyErr_SetString(PyExc_IndexError, "Value out of range"); + return NULL; + } + position = g_list_nth(list, index); + return SWIG_NewPointerObj((void*)(position->data), SWIGTYPE_p__SPLRule, 0); + } + PyObject* sw_get_list_len(GList *list) { return PyInt_FromLong(g_list_length(list)); } @@ -92,9 +102,11 @@ PyObject* sw_get_playlists(Itdb_iTunesDB *itdb) { typedef char gchar; %typemap(in) guint8 { - long ival; - ival = PyInt_AsLong($input); - if (( ival > 255 ) || ( ival < 0 )) { + unsigned long ival; + ival = PyInt_AsUnsignedLongMask($input); + if (PyErr_Occurred()) + SWIG_fail; + if (ival > 255) { PyErr_SetString(PyExc_ValueError, "$symname: Value must be between 0 and 255"); SWIG_fail; } else { @@ -102,36 +114,68 @@ typedef char gchar; } } +%typemap(in) guint16 { + unsigned long ival; + ival = PyInt_AsUnsignedLongMask($input); + if (PyErr_Occurred()) + SWIG_fail; + if (ival > 65535) { + PyErr_SetString(PyExc_ValueError, "$symname: Value must be between 0 and 65535"); + SWIG_fail; + } else { + $1 = (guint16) ival; + } +} + +%typemap(in) guint32 { + unsigned long ival; + ival = PyInt_AsUnsignedLongMask($input); + if (PyErr_Occurred()) + SWIG_fail; + $1 = (guint32) ival; +} + +%typemap(in) guint64 { + unsigned long ival; + ival = PyInt_AsUnsignedLongLongMask($input); + if (PyErr_Occurred()) + SWIG_fail; + $1 = (guint64) ival; +} + %typemap(out) guint64 { - $result = PyLong_FromLong($1); + $result = PyLong_FromUnsignedLongLong($1); +} + +%typemap(out) gint64 { + $result = PyLong_FromLongLong($1); } %typemap(out) guint32 { - $result = PyInt_FromLong($1); + $result = PyLong_FromUnsignedLong($1); } -%typemap(out) guint16 { - $result = PyInt_FromLong($1); +%typemap(out) gint32 { + $result = PyLong_FromLong($1); } +%typemap(out) guint16 { + $result = PyLong_FromUnsignedLong($1); +} %typemap(out) guint8 { $result = PyInt_FromLong($1); } typedef int gboolean; -typedef long gint64; -typedef int gint32; -typedef int gint16; typedef int gint; -typedef unsigned int guint32; - #define G_BEGIN_DECLS #define G_END_DECLS PyObject* sw_get_tracks(Itdb_iTunesDB *itdb); PyObject* sw_get_track(GList *list, gint index); +PyObject* sw_get_rule(GList *list, gint index); PyObject* sw_get_list_len(GList *list); PyObject* sw_get_playlists(Itdb_iTunesDB *itdb); PyObject* sw_get_playlist_tracks(Itdb_Playlist *pl); |