Search code examples
pythonsystemwindow-managers

Good ways to monitor user "behavior" in Python?


I'm looking for easy APIs to get informations about the user use of his computer in Python.

What would be relevant:

  • Keypresses, mouse events [PyKeylogger?] # I know this has been debated in SO already
  • Know information about the processes that are running
  • Harder, know information about the window that has focus (its name?) # that would be really interesting
  • Even harder, can I ask the system for even more information? (for instance, may I query about the text displayed in a particular graphical element this window? say it's a browser, can I get the current url?)

I'd like the answers to be about a Linux system, but I am also interested in Windows alternatives if it is easier. I don't care about portability that much for now.

I imagine that under Linux there is the fallback of running a shell script and retrieving the output, but I want to know if this is the common way to do it or if some APIs wrap this nicely already.

I'm sorry for the vagueness of the question, but it's really me trying to assess the extent of information I can get easily under certain APIs and to know about what tools are "usual" for this kind of work.

Thank you for every bit of information you might bring.


Solution

  • The question is not so much vague as it is rather broad. Most of the information you're looking for is (as you indicated yourself) spread out over various questions on SO.

    I've done some research on this subject for a productivity tracking application I was thinking about writing. Here's some general advice I can give you:

    • For monitoring and querying information on windows (title, state etc.) you'll want to take a look at libwnck (see: python-wnck). Beware that at its creation a WnckScreen object will not have fetched information from the X server and it will look like there are no workspaces nor windows on the screen (more on this here).

    • In some cases you might be able to get some useful information on a running process by querying its entry in /proc (you might want to look at the psutil module). However, it's more likely that you will not (it depends on what you're looking for).

    • For querying very specific information on an application you'll have to look at what that specific application supports.

      On Linux applications tend to expose relevant internal functionality using a D-Bus service.

      Totem for example has a D-Bus service that essentially exposes the main Totem object which you can use to monitor what's playing etc.. Geany, a text editor (or lightweight IDE) I use, has a pretty much feature complete D-Bus plugin you can use to query the currently active tab etc.

      Firefox is a bit of an odd duck in this respect. There have been various efforts over the years to expose a D-Bus service but for some reason those efforts never seem to have gone anywhere. Your best bet is to look at MozRepl which "lets you program Firefox and other Mozilla-based applications from the inside" (it exposes an interactive shell over telnet).

    • As for a generic approach to querying individuals controls/widgets. On Linux I know only of Parasite. If you've used Firebug, it's sort of like that, but for GTK+ applications. However, I have my doubts it's going to be of much use in your use case scenario.

      You might also be able to (ab)use the assistive technologies support (for screen readers and such). See this answer. I don't have any experience with this myself.

    If you have any questions, feel free to leave a comment on this answer.