import win32gui, cgi, time, ftplib, sys # Simple Python script to generate a "track currently playing" file for # my weblog http://www.cix.co.uk/~jimh/weblog/blogger.html # # Does this by monitoring the window title (and hence track name & artist) for my MP3 player # (Zinf - http://www.zinf.org), and if a track has been playing for over 30 seconds (I tend # to skip some tracks depending on my mood) it squirts the track name into a file and then # ftps the file (playlist.html) to my web server. # # Also does psuedo presence indication with Javascript, based on the premise that if I'm # active at the computer I'm likely to be listening to music or moving the mouse. # # Expected params: # # 1 - ftp user # 2 - ftp password # 3 - ftp directory # # e.g. >ZinfBlog.py jim passwd /blog/files # # Nasty bit is that the output file is (trivial) Javascript as I don't want to have to rely # on SSI, so the main html file needs something like the following to pull the playlist in: # # # # Jim Hughes - Email: jim@fineway.cx - Jabber: jimh@amessage.de or jimh@jabber.at # # 2002-08-30 JMH 0.01 Initial release # 2002-09-01 JMH 0.02 Minor typos fixed and constants for use with FreeAmp added # 2002-09-02 JMH 0.03 Added 30 minute "not listening" output and Javascript presence indication # 2002-09-03 JMH 0.04 Minor tweaks to presence stuff # 2002-09-04 JMH 0.05 Removed all
s from output # 2002-09-05 JMH 0.06 Added clas name for RealPlayer and mouse move detection # def GetWindowTitle( classname ): """" Returns the title of a Windows app given the class name, if more than one instance of an app is running you'll get just one title, on error (e.g. app not running) returns None """ try: hWin = win32gui.FindWindow( classname, None ) return win32gui.GetWindowText( hWin ) except: return None def FtpPut( site, user, pwd, path, srcfile, destfile ): """ Trivial function to put an Ascii file onto an ftp site """ ftp = ftplib.FTP( site ) # connect to host # ftp.set_debuglevel(1) # comment out this line if you're not testing ftp.login( user, pwd ) ftp.cwd( path ) if path == ftp.pwd(): fileA = open( srcfile, "r" ) ftp.storlines("STOR " + destfile, fileA) fileA.close() ftp.quit def CreateOutputFile( fileName, introText, midText, trackInfo, offset ): """"Create a file to ftp to the web site, has a load of Javascript which compares the current time with the time the script was created to roughly work out when I was last doing something at the PC""" now = time.gmtime() # UTC values for Hr, Min and day of month when this script was created presenceIntro = str( "stampDate = %d; stampHr = %d; stampMin = %d; offset = %d;" % (now[2], now[3], now[4], offset) ) presenceStuff = """ now = new Date(); if ((now.getUTCDate() != stampDate) || (now.getUTCHours() < stampHr)) { document.write("yesterday (or even longer ago)"); } else { stampAbs = stampHr*60 + stampMin; nowAbs = now.getUTCHours()*60 + now.getUTCMinutes(); diffAbs = nowAbs - stampAbs; diffAbs = diffAbs + offset if (diffAbs > 120) document.write("at least a couple of hours ago"); else if (diffAbs > 60) document.write("over an hour ago"); else if (diffAbs > 30) document.write("in the last hour"); else if (diffAbs > 15) document.write("in the last half hour"); else if (diffAbs > 5) document.write("in the last fifteen minutes"); else document.write("just now"); } """ opFile = open( fileName, "w" ) opFile.write( 'document.write("%s");' % introText ) opFile.write( presenceIntro ) opFile.write( presenceStuff ) opFile.write( 'document.write("%s");' % midText ) opFile.write( trackInfo ) opFile.write( "\n" ) opFile.close() # allow the mouse to move 16 pixels in any direction without it counting # as being moved def MouseMoved( prevPos, newPos ): diff = prevPos[0] - newPos[0] if -16 < diff < 16: diff = prevPos[1] - newPos[1] if -16 < diff < 16: return 0 return 1 # Dumb dummy init value, to force activity on the first loop prevTrackName = "BlahBlahBlah" trackName = prevTrackName ftpSite = "www.cix.co.uk" fileName = "playlist.html" introText = "I saw Jim " midText = ", he was " # Values for FreeAmp # appName = "FreeAmp" # cutAt = 9 # Values for Zinf appName = "Zinf" cutAt = 6 # Values for Real Player (window title is "RealOne Player:Stream name") # appName = "GeminiWindowClass" # cutAt = 16 while 1: trackName = GetWindowTitle( appName ) # Look for change if trackName != prevTrackName: prevTrackName = trackName prevMousePos = win32gui.GetCursorPos() doneFtp = 0 else: if doneFtp == 0: inactiveCount = 0 playTime = 0 if trackName != None: print( trackName[cutAt:] ) tempStr = str( 'document.write("listening to: %s");' % cgi.escape( trackName[cutAt:] ) ) else: print( "app not running" ) tempStr = str( 'document.write("not listening to anything");' ) CreateOutputFile( fileName, introText, midText, tempStr, 0 ) try: FtpPut( ftpSite, sys.argv[1], sys.argv[2], sys.argv[3], fileName, fileName ) except: print( "FtpPut failed" ) doneFtp = 1 else: mousePos = win32gui.GetCursorPos() if MouseMoved( prevMousePos, mousePos ): prevMousePos = mousePos inactiveCount = 0 playTime = playTime + 1 inactiveCount = inactiveCount + 1 if playTime == 60: print( "same track for 30 mins" ) tempStr = str( 'document.write("not listening to anything");' ) CreateOutputFile( fileName, introText, midText, tempStr, (inactiveCount / 2) ) try: FtpPut( ftpSite, sys.argv[1], sys.argv[2], sys.argv[3], fileName, fileName ) except: print( "FtpPut failed" ) time.sleep(30)