Media Kind in iTunes COM for Windows SDK - python

I recently found out about the awesomeness of the iTunes COM for Windows SDK. I am using Python with win32com to talk to my iTunes library. Needless to say, my head is in the process of exploding. This API rocks.
I have one issue though, how do I access the Media Kind attribute of the track? I looked through the help file provided in the SDK and saw no sign of it. If you go into iTunes, you can modify the track's media kind. This way if you have an audiobook that is showing up in your music library, you can set the Media Kind to Audiobook and it will appear in the Books section in iTunes. Pretty nifty.
The reason I ask is because I have a whole crap load of audiobooks that are showing up in my LibraryPlaylist.
Here is my code thus far.
import win32com.client
iTunes = win32com.client.gencache.EnsureDispatch('iTunes.Application')
track = win32com.client.CastTo(iTunes.LibraryPlaylist.Tracks.Item(1), 'IITFileOrCDTrack')
print track.Artist, '-', track.Name
print
print 'Is this track an audiobook?'
print 'How the hell should I know?'
Thanks in advance.

One reason why you may not be able to find it is that the atom structure the com object references may be out of date. The most popular list of atoms from the MP4 structure is here: http://atomicparsley.sourceforge.net/mpeg-4files.html I don't see a media kind atom. I suppose you could try to parse the structure through atomicparsley but to my knowledge it only finds atoms that it knows about.
Short Answer: The COM object may not know about the MediaKind Attribute.

The only reference I can find to that "Media Kind" attribute is the ITUserPlaylistSpecialKind enum. The only place that is used is a getter method IITUserPlaylist::SpecialKind. So it seems that is a read-only playlist-level attribute. I would guess that in order to read it you need to get the playlist of the track and then get the playlists's SpecialKind attribute. In order to write it, you probably have to move the track to the appropriate playlist.

Well, the Media Kind is in interface IITTrack.Kind, but that probably isn't what you want - the answer will be one of:
public enum ITTrackKind
{
ITTrackKindUnknown = 0,
ITTrackKindFile = 1,
ITTrackKindCD = 2,
ITTrackKindURL = 3,
ITTrackKindDevice = 4,
ITTrackKindSharedLibrary = 5,
}
Probably you need to look at IITTrack.Genre, which gives the string form of the ID3 tag
Genre, so you can find "Audiobook" or Apple's "Books & Spoken". (Some genres are treated specially by iTunes/iPods).
Tip: the compiled help file in the ITunes SDK I downloaded seemed to be broken - I had to convert it back to HTML files and use Firefox/grep to find the information I needed.

It is actually quite easy, use the IITFileOrCDTrack.Podcast :
yourTrack.Podcast
If it is a podcast, it will return True, otherwise it will return False.
You can of course set it through
yourTrack.Podcast(bool)
Glad I could help.

Related

How to use libchamplain to load local tiles or OSM XML in Python?

I am trying to use GTK and libchamplain to display a map from local map data. The application is to be run on computers with no internet connection.
After taking a look at the mapbox.py example it seems like one should replace the NetworkTileSource source with FileTileSource. So I used the minimal.py example to work with FileTileSource:
widget = GtkChamplain.Embed()
widget.set_size_request(640, 480)
tile_source = Champlain.FileTileSource.new_full(
ID,
NAME,
LICENSE_TEXT,
LICENSE_URL,
MIN_ZOOM,
MAX_ZOOM,
TILE_SIZE,
Champlain.MapProjection.MERCATOR,
Champlain.ImageRenderer())
tile_source.load_map_data("map.osm")
widget.get_view().set_map_source(tile_source)
Unfortunately, when running the application, the map is not being displayed, and I receive the following error message:
(minimal.py:26308): libchamplain-WARNING **: NULL pixbuf
Based on some C examples (one, two), I assume that there is a so-called renderer missing, called Memphis, which is C only. It seems like Champlain's ImageRenderer, despite its similar name, is not the right tool for this.
So my question is how I would continue at this point. There are quite a few OSM renderers besides Memphis, probably some written in Python as well, but which one(s) would integrate well with Champlain?
It doesn't have to be be OSM, either. A collection of pre-rendered PNG would be fine with me, too. It's just that the FileTileSource documentation specifically mentions "Loads the OpenStreetMap XML file at the given path", which is why I assume better support for OSM.
Any help is much appreciated.
I managed to do it in a very simplistic way: just organize your tiles respecting the #z#/#x/#y# logic as described in the docs, then don't change anything in that mapbox.py example except the URI. your case, for a file structure on the /tmp directory could look like:
MAX_ZOOM,
TILE_SIZE,
Champlain.MapProjection.MERCATOR,
"file:///tmp/tiles-#Z#/#X#/#Y#.png",
#"https://a.tiles.mapbox.com/v4/mapbox.streets/#Z#/#X#/#Y#.png?access_token=" + ACCESS_TOKEN,
Champlain.ImageRenderer())
still according to the docs, the FileTileSource is meant for using a single osm file. I'm not so sure how that works, I didn't yet try it.

Get spotify currently playing track

EDIT : Let's try to clarify all this.
I'm writing a python script, and I want it to tell me the song that Spotify is currently playing.
I've tried looking for libraries that could help me but didn't find any that are still maintained and working.
I've also looked through Spotify's web API, but it does not provide any way to get that information.
The only potential solution I found would be to grab the title of my Spotify (desktop app) window. But I didn't manage to do that so far.
So basically, what I'm asking is whether anyone knows :
How to apply the method I'm already trying to use (get the window's title from a program), either in pure python or using an intermediary shell script.
OR
Any other way to extract that information from Spotify's desktop app or web client.
Original post :
I'm fiddling with the idea of a python status bar for a linux environment, nothing fancy, just a script tailored to my own usage. What I'm trying to do right now is to display the currently playing track from spotify (namely, the artist and title).
There does not seem to be anything like that in their official web API. I haven't found any third party library that would do that either. Most libraries I found are either deprecated since spotify released their current API, or they are based on said API which does not do what I want.
I've also read a bunch of similar question in here, most of which had no answers, or a deprecated solution.
I thought about grabbing the window title, since it does diplay the information I need. But not only does that seem really convoluted, I also have difficulties making this happen. I was trying to get it by running a combination of the linux commands xdotools and xprop inside my script.
It's worth mentionning that since I'm already using the psutil lib for other informations, I already have access to spotify's PID.
Any idea how I could do that ?
And in case my method was the only one you can think of, any idea how to actually make it work ?
Your help will be appreciated.
The Spotify client on Linux implements a D-Bus interface called MPRIS - Media Player Remote Interfacing Specification.
http://specifications.freedesktop.org/mpris-spec/latest/index.html
You could access the title (and other metadata) from python like this:
import dbus
session_bus = dbus.SessionBus()
spotify_bus = session_bus.get_object("org.mpris.MediaPlayer2.spotify",
"/org/mpris/MediaPlayer2")
spotify_properties = dbus.Interface(spotify_bus,
"org.freedesktop.DBus.Properties")
metadata = spotify_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")
# The property Metadata behaves like a python dict
for key, value in metadata.items():
print(key, value)
# To just print the title
print(metadata['xesam:title'])
For windows:
The library can be found at github: https://github.com/XanderMJ/spotilib. Keep in mind that this is still work in progress.
Just copy the file and place it in your Python/Lib directory.
import spotilib
spotilib.artist() #returns the artist of the current playing song
spotilib.song() #returns the song title of the current playing song
spotilib.artist() returns only the first artist. I started working on an other library spotimeta.py to solve this issue. However, this is not working at 100% yet.
import spotimeta
spotimeta.artists() #returns a list of all the collaborating artists of the track
If an error occurs, spotimeta.artists() will return only the first artist (found with spotilib.artist())

Microsoft Speech Recognition Custom Training

I have been wanting to create an application using the Microsoft Speech Recognition.
My application's users are expected to often say abbreviated things, such as 'LHC' for 'Large Hadron Collider' or 'CERN'. Given that exact order, my application will return
You said: At age C.
You said: Cern
While it did work for 'CERN', it failed very badly for 'LHC'.
However, if I could make my own custom training files, I could easily place the term 'LHC' somewhere in there. Then, I could make the user access the Speech Control Panel and run my training file.
All the links I have found for this have been frustratingly useless, as they just say things like 'This is ----, you should try going to the ---- forum instead'.
If it does help, here is a list of the links:
http://compgroups.net/comp.speech.users/add-my-own-training/153194
https://groups.google.com/forum/#!topic/microsoft.public.speech.server/v58SH1ov22s
http://social.msdn.microsoft.com/Forums/en/servercorefordevelopers/thread/f7a35f3f-b352-464a-b264-e16eb4afd049
Is my problem even possible? Or are the training files themselves in a special format? If so, can that format be reproduced?
A solution that can also work on Windows XP would be ideal.
Thanks in advance!
P.S. If there are any libraries or modules out there already for this, could anyone point me to some? A Python or C/C++ solution would be splendid. Also, since I'd rather not post another question regarding this, is it possible to utilize the train utilities from command prompt (or without the GUI visible, but still having total command of all controls)?
Okay, pulling this from a thing I wrote three or four years ago now, but I believe you want to do something like this.
The grammar library is a trained system which can recognize words. You can create your own grammar library cued to specific words.
C#, sorry
using System.Speech
using System.Speech.Recognition
using System.Speech.AudioFormat
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
string[] words = {"L H C", "CERN"};
Choices choices = new Choices(words);
GrammarBuilder gb = new GrammarBuilder(choices);
Grammar grammar = new Grammar(gb);
sre.LoadGrammar(grammar);
That is as far as I can get you. From docs it looks like you can define the pronunciations somehow. So perhaps that way you could have LHC map directly to a single word. Here are the docs on the grammar class - http://msdn.microsoft.com/en-us/library/system.speech.recognition.grammar.aspx
Small update - see example in their docs here http://msdn.microsoft.com/en-us/library/ms554228.aspx

Python pefile member question

Gang,
I apologize if this is a really dumb question... I am wanting to use the super convenient python script pefile (http://code.google.com/p/pefile/) that parses an executable and lists particular information about the PE structure. My question is where can I find information about how to access particular members of the executable? I've scoured the wiki and read the usage examples but that documentation only covered 4-5 members. What I am wondering is if you guys have a list of members I can access to display the information I care about. So specifically, if I wanted to list the Stack Commit Size of an executable, does it look like this: pe.FILE_HEADER.StackCommitSize, obviously I can run this code and figure it out but have you guys seen API DOC floating around that I find the members i need?
THANKS!
From the PE docstring:
Basic headers information will be available in the attributes:
DOS_HEADER
NT_HEADERS
FILE_HEADER
OPTIONAL_HEADER
All of them will contain among their attributes the members of the
corresponding structures as defined in WINNT.H
So, look at winnt.h and you'll see which attributes are available.
Or just read the source code for the module. It's big, but everything you need to know is in there.
You can generally find everything that is in a PE file in the Microsoft PE/COFF specification.
Once you've looked there, you know that the StackCommitSize is in the optional image header. Then all you have to do is to look for the corresponding structure in pefile, which usually bears a similar name, if not indeed the very same name. In this case:
pe = pefile.PE("C:\\Windows\\Notepad.exe")
print pe.OPTIONAL_HEADER.SizeOfStackCommit
Will give you what you want.
If you have trouble finding SizeOfStackCommit (after you've found it in the specification), just use your quick find on the source code. It's as easy to read as you can get, and I don't think you'll have any trouble finding the required structure.
Now, there probably aren't any API docs for pefile itself, but as you can see there's really no need for it, since it's just a nice Pythonic wrapper around the PE specification itself.

Plural of words using Open Office API for Python (UNO)

I would like to retrieve the plural words in different languages in Python.
I know that openoffice has an API called uno (import uno) and it should give me this ability using openoffice's language dictionaries, but I could not find any reference to it.
As a concrete example, I would something like this:
>>> print getPluralOf('table')
tables
One possibility is to download the dictionary files though this link and write a method to read the dictionary and form the plurals. But i can't believe that this is not available already using uno.
I appreciate any help
You can introspect the module with dir(uno) and then try dir() on uno.XXX, with whatever looks helpful. You can also use help() on uno and its members. I've never used it and I don't have access to OO on this computer so I can't help more than that...
Nodebox Linguistics includes a convenient function for pluralizing nouns, albeit only in English.
>>> import en
>>> en.noun.plural('table')
'tables'

Categories

Resources